|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | + |
| 3 | +import type { SortingState } from "@tanstack/react-table"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +describe("multi-column sorting logic", () => { |
| 7 | + // Extract the core sorting logic to test in isolation |
| 8 | + const handleSort = ( |
| 9 | + columnId: string, |
| 10 | + desc: boolean, |
| 11 | + sortingState: SortingState, |
| 12 | + setSorting: (state: SortingState) => void, |
| 13 | + clearSorting: () => void, |
| 14 | + ) => { |
| 15 | + const currentSort = sortingState.find((s) => s.id === columnId); |
| 16 | + |
| 17 | + if (currentSort && currentSort.desc === desc) { |
| 18 | + // Clicking the same sort again - remove it |
| 19 | + clearSorting(); |
| 20 | + } else { |
| 21 | + // New sort or different direction - move to end of stack |
| 22 | + const otherSorts = sortingState.filter((s) => s.id !== columnId); |
| 23 | + const newSort = { id: columnId, desc }; |
| 24 | + setSorting([...otherSorts, newSort]); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + it("implements stack-based sorting: moves re-clicked column to end", () => { |
| 29 | + const sortingState: SortingState = [ |
| 30 | + { id: "name", desc: false }, |
| 31 | + { id: "age", desc: false }, |
| 32 | + ]; |
| 33 | + const setSorting = vi.fn(); |
| 34 | + const clearSorting = vi.fn(); |
| 35 | + |
| 36 | + // Click Desc on age - should move age to end with desc=true |
| 37 | + handleSort("age", true, sortingState, setSorting, clearSorting); |
| 38 | + |
| 39 | + expect(setSorting).toHaveBeenCalledWith([ |
| 40 | + { id: "name", desc: false }, |
| 41 | + { id: "age", desc: true }, |
| 42 | + ]); |
| 43 | + expect(clearSorting).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("removes sort when clicking same direction twice", () => { |
| 47 | + const sortingState: SortingState = [{ id: "age", desc: false }]; |
| 48 | + const setSorting = vi.fn(); |
| 49 | + const clearSorting = vi.fn(); |
| 50 | + |
| 51 | + // Click Asc on age again - should remove the sort |
| 52 | + handleSort("age", false, sortingState, setSorting, clearSorting); |
| 53 | + |
| 54 | + expect(clearSorting).toHaveBeenCalled(); |
| 55 | + expect(setSorting).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("adds new column to end of stack", () => { |
| 59 | + const sortingState: SortingState = [{ id: "name", desc: false }]; |
| 60 | + const setSorting = vi.fn(); |
| 61 | + const clearSorting = vi.fn(); |
| 62 | + |
| 63 | + // Click Asc on age - should add age to end |
| 64 | + handleSort("age", false, sortingState, setSorting, clearSorting); |
| 65 | + |
| 66 | + expect(setSorting).toHaveBeenCalledWith([ |
| 67 | + { id: "name", desc: false }, |
| 68 | + { id: "age", desc: false }, |
| 69 | + ]); |
| 70 | + expect(clearSorting).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("toggles sort direction when clicking opposite", () => { |
| 74 | + const sortingState: SortingState = [{ id: "age", desc: false }]; |
| 75 | + const setSorting = vi.fn(); |
| 76 | + const clearSorting = vi.fn(); |
| 77 | + |
| 78 | + // Click Desc on age - should toggle to descending |
| 79 | + handleSort("age", true, sortingState, setSorting, clearSorting); |
| 80 | + |
| 81 | + expect(setSorting).toHaveBeenCalledWith([{ id: "age", desc: true }]); |
| 82 | + expect(clearSorting).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("correctly calculates priority numbers", () => { |
| 86 | + const sortingState: SortingState = [ |
| 87 | + { id: "name", desc: false }, |
| 88 | + { id: "age", desc: true }, |
| 89 | + { id: "dept", desc: false }, |
| 90 | + ]; |
| 91 | + |
| 92 | + // Priority is index + 1 |
| 93 | + const nameSort = sortingState.find((s) => s.id === "name"); |
| 94 | + const namePriority = nameSort ? sortingState.indexOf(nameSort) + 1 : null; |
| 95 | + expect(namePriority).toBe(1); |
| 96 | + |
| 97 | + const deptSort = sortingState.find((s) => s.id === "dept"); |
| 98 | + const deptPriority = deptSort ? sortingState.indexOf(deptSort) + 1 : null; |
| 99 | + expect(deptPriority).toBe(3); |
| 100 | + }); |
| 101 | + |
| 102 | + it("handles removing column from middle of stack", () => { |
| 103 | + const sortingState: SortingState = [ |
| 104 | + { id: "name", desc: false }, |
| 105 | + { id: "age", desc: true }, |
| 106 | + { id: "dept", desc: false }, |
| 107 | + ]; |
| 108 | + const setSorting = vi.fn(); |
| 109 | + const clearSorting = vi.fn(); |
| 110 | + |
| 111 | + // Click Desc on age again - should remove it |
| 112 | + handleSort("age", true, sortingState, setSorting, clearSorting); |
| 113 | + |
| 114 | + expect(clearSorting).toHaveBeenCalled(); |
| 115 | + // After removal, dept should move from priority 3 to priority 2 |
| 116 | + }); |
| 117 | +}); |
0 commit comments