@42-central/ui-react 0.26.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/dist/{chunk-YZJUPYPO.js → chunk-2W7H43WL.js} +3 -3
  2. package/dist/{chunk-YZJUPYPO.js.map → chunk-2W7H43WL.js.map} +1 -1
  3. package/dist/{chunk-OVT2KZHY.js → chunk-3KARNPW4.js} +3 -3
  4. package/dist/{chunk-OVT2KZHY.js.map → chunk-3KARNPW4.js.map} +1 -1
  5. package/dist/chunk-6B2GNPJP.js +43 -0
  6. package/dist/chunk-6B2GNPJP.js.map +1 -0
  7. package/dist/{chunk-E52Z2FKU.js → chunk-7BY5I2RB.js} +3 -3
  8. package/dist/{chunk-E52Z2FKU.js.map → chunk-7BY5I2RB.js.map} +1 -1
  9. package/dist/{chunk-JFB6O7IY.js → chunk-7KFUFAVP.js} +4 -4
  10. package/dist/{chunk-JFB6O7IY.js.map → chunk-7KFUFAVP.js.map} +1 -1
  11. package/dist/{chunk-X3YIHN2U.js → chunk-ASKQ6N5V.js} +3 -3
  12. package/dist/{chunk-X3YIHN2U.js.map → chunk-ASKQ6N5V.js.map} +1 -1
  13. package/dist/{chunk-6IRMKF5T.js → chunk-B4GNSSHU.js} +4 -4
  14. package/dist/{chunk-6IRMKF5T.js.map → chunk-B4GNSSHU.js.map} +1 -1
  15. package/dist/{chunk-BMEJBY6H.js → chunk-B5YHYI7E.js} +22 -27
  16. package/dist/chunk-B5YHYI7E.js.map +1 -0
  17. package/dist/{chunk-PT22KEVD.js → chunk-BGDVUC3B.js} +5 -5
  18. package/dist/{chunk-PT22KEVD.js.map → chunk-BGDVUC3B.js.map} +1 -1
  19. package/dist/{chunk-7247WBTV.js → chunk-DJ72KP32.js} +3 -3
  20. package/dist/{chunk-7247WBTV.js.map → chunk-DJ72KP32.js.map} +1 -1
  21. package/dist/{chunk-7OGZT7U2.js → chunk-G3AD3NJG.js} +3 -3
  22. package/dist/{chunk-7OGZT7U2.js.map → chunk-G3AD3NJG.js.map} +1 -1
  23. package/dist/{chunk-276RL7FX.js → chunk-GJFCIG34.js} +8 -8
  24. package/dist/{chunk-276RL7FX.js.map → chunk-GJFCIG34.js.map} +1 -1
  25. package/dist/{chunk-JDPVD2YE.js → chunk-HCHGERDB.js} +3 -3
  26. package/dist/{chunk-JDPVD2YE.js.map → chunk-HCHGERDB.js.map} +1 -1
  27. package/dist/{chunk-H7WOMZXO.js → chunk-HWBFRDX7.js} +16 -8
  28. package/dist/chunk-HWBFRDX7.js.map +1 -0
  29. package/dist/{chunk-LYGOFMXJ.js → chunk-I4IJOLNA.js} +4 -4
  30. package/dist/{chunk-LYGOFMXJ.js.map → chunk-I4IJOLNA.js.map} +1 -1
  31. package/dist/{chunk-7B44J3CF.js → chunk-I53TMAV2.js} +6 -6
  32. package/dist/{chunk-7B44J3CF.js.map → chunk-I53TMAV2.js.map} +1 -1
  33. package/dist/{chunk-DSZF2TW6.js → chunk-IA6DCC3N.js} +6 -6
  34. package/dist/{chunk-DSZF2TW6.js.map → chunk-IA6DCC3N.js.map} +1 -1
  35. package/dist/{chunk-QEAF57ZQ.js → chunk-ISX3AM32.js} +3 -3
  36. package/dist/{chunk-QEAF57ZQ.js.map → chunk-ISX3AM32.js.map} +1 -1
  37. package/dist/{chunk-N42EFOML.js → chunk-K3FZHYJL.js} +6 -6
  38. package/dist/chunk-K3FZHYJL.js.map +1 -0
  39. package/dist/{chunk-ZVECQBGG.js → chunk-KNGSO7A3.js} +41 -13
  40. package/dist/chunk-KNGSO7A3.js.map +1 -0
  41. package/dist/{chunk-NLSJ6T7V.js → chunk-KYVXPSNW.js} +4 -4
  42. package/dist/{chunk-NLSJ6T7V.js.map → chunk-KYVXPSNW.js.map} +1 -1
  43. package/dist/{chunk-J2NW7JID.js → chunk-L5GH7LLD.js} +3 -3
  44. package/dist/{chunk-J2NW7JID.js.map → chunk-L5GH7LLD.js.map} +1 -1
  45. package/dist/{chunk-THGJLR5S.js → chunk-NNPSBC7O.js} +5 -5
  46. package/dist/{chunk-THGJLR5S.js.map → chunk-NNPSBC7O.js.map} +1 -1
  47. package/dist/{chunk-YMVUQ2L7.js → chunk-P7SH3XWY.js} +6 -6
  48. package/dist/chunk-P7SH3XWY.js.map +1 -0
  49. package/dist/{chunk-2CL3FKEY.js → chunk-QEMXXQQO.js} +3 -3
  50. package/dist/{chunk-2CL3FKEY.js.map → chunk-QEMXXQQO.js.map} +1 -1
  51. package/dist/{chunk-2NTCQIRP.js → chunk-QRNQQIQM.js} +5 -5
  52. package/dist/{chunk-2NTCQIRP.js.map → chunk-QRNQQIQM.js.map} +1 -1
  53. package/dist/{chunk-LKFHUDEW.js → chunk-QVOBLRCM.js} +3 -3
  54. package/dist/{chunk-LKFHUDEW.js.map → chunk-QVOBLRCM.js.map} +1 -1
  55. package/dist/{chunk-E3XLWMEB.js → chunk-RKCUXOU2.js} +4 -4
  56. package/dist/{chunk-E3XLWMEB.js.map → chunk-RKCUXOU2.js.map} +1 -1
  57. package/dist/{chunk-DMI4KS32.js → chunk-SBZJGFZ6.js} +3 -3
  58. package/dist/{chunk-DMI4KS32.js.map → chunk-SBZJGFZ6.js.map} +1 -1
  59. package/dist/{chunk-7P32XCEU.js → chunk-SLUOF7XW.js} +2 -2
  60. package/dist/chunk-SLUOF7XW.js.map +1 -0
  61. package/dist/{chunk-SCXXJDKQ.js → chunk-SP6LCCTZ.js} +21 -28
  62. package/dist/chunk-SP6LCCTZ.js.map +1 -0
  63. package/dist/{chunk-Y2G47ARQ.js → chunk-TGGNSSOJ.js} +3 -3
  64. package/dist/{chunk-Y2G47ARQ.js.map → chunk-TGGNSSOJ.js.map} +1 -1
  65. package/dist/{chunk-2TZU4LSX.js → chunk-ULF5LOBS.js} +3 -3
  66. package/dist/{chunk-2TZU4LSX.js.map → chunk-ULF5LOBS.js.map} +1 -1
  67. package/dist/{chunk-D2QIPQ74.js → chunk-W3XX7BYT.js} +2 -2
  68. package/dist/chunk-W3XX7BYT.js.map +1 -0
  69. package/dist/{chunk-F374BNL4.js → chunk-YVTM2KRR.js} +12 -8
  70. package/dist/chunk-YVTM2KRR.js.map +1 -0
  71. package/dist/colors.css +328 -0
  72. package/dist/components/action-icon/action-icon.d.ts +6 -5
  73. package/dist/components/action-icon/action-icon.d.ts.map +1 -1
  74. package/dist/components/action-icon/index.js +3 -2
  75. package/dist/components/alert/index.js +2 -2
  76. package/dist/components/app-shell/index.js +7 -6
  77. package/dist/components/app-shell/index.js.map +1 -1
  78. package/dist/components/autocomplete/index.js +4 -4
  79. package/dist/components/badge/badge.d.ts +1 -1
  80. package/dist/components/badge/badge.d.ts.map +1 -1
  81. package/dist/components/badge/index.js +2 -1
  82. package/dist/components/button/button.d.ts +11 -9
  83. package/dist/components/button/button.d.ts.map +1 -1
  84. package/dist/components/button/index.js +3 -2
  85. package/dist/components/button-multi-select/index.js +4 -3
  86. package/dist/components/button-multi-select/index.js.map +1 -1
  87. package/dist/components/button-select/index.js +4 -3
  88. package/dist/components/button-select/index.js.map +1 -1
  89. package/dist/components/calendar/index.js +4 -3
  90. package/dist/components/calendar-heatmap/index.js +2 -2
  91. package/dist/components/camera/index.js +11 -10
  92. package/dist/components/clipboard/index.js +3 -2
  93. package/dist/components/clipboard/index.js.map +1 -1
  94. package/dist/components/code-block/chrome.js +5 -5
  95. package/dist/components/code-block/index.js +6 -6
  96. package/dist/components/combobox/index.js +4 -4
  97. package/dist/components/combobox-list/index.js +6 -6
  98. package/dist/components/data-table/data-table-features.d.ts +8 -0
  99. package/dist/components/data-table/data-table-features.d.ts.map +1 -1
  100. package/dist/components/data-table/data-table-group.d.ts +128 -0
  101. package/dist/components/data-table/data-table-group.d.ts.map +1 -0
  102. package/dist/components/data-table/data-table-grouping.d.ts +72 -0
  103. package/dist/components/data-table/data-table-grouping.d.ts.map +1 -0
  104. package/dist/components/data-table/data-table-icons.d.ts +4 -2
  105. package/dist/components/data-table/data-table-icons.d.ts.map +1 -1
  106. package/dist/components/data-table/data-table-meta.d.ts +9 -0
  107. package/dist/components/data-table/data-table-meta.d.ts.map +1 -1
  108. package/dist/components/data-table/data-table-pagination.d.ts +4 -1
  109. package/dist/components/data-table/data-table-pagination.d.ts.map +1 -1
  110. package/dist/components/data-table/data-table-schema.d.ts +4 -0
  111. package/dist/components/data-table/data-table-schema.d.ts.map +1 -1
  112. package/dist/components/data-table/data-table-stack.d.ts +16 -3
  113. package/dist/components/data-table/data-table-stack.d.ts.map +1 -1
  114. package/dist/components/data-table/data-table-toolbar.d.ts +33 -2
  115. package/dist/components/data-table/data-table-toolbar.d.ts.map +1 -1
  116. package/dist/components/data-table/data-table-translations.d.ts +35 -0
  117. package/dist/components/data-table/data-table-translations.d.ts.map +1 -1
  118. package/dist/components/data-table/data-table.d.ts +55 -3
  119. package/dist/components/data-table/data-table.d.ts.map +1 -1
  120. package/dist/components/data-table/index.d.ts +3 -1
  121. package/dist/components/data-table/index.d.ts.map +1 -1
  122. package/dist/components/data-table/index.js +883 -234
  123. package/dist/components/data-table/index.js.map +1 -1
  124. package/dist/components/date-picker/index.js +12 -11
  125. package/dist/components/date-picker/index.js.map +1 -1
  126. package/dist/components/download-button/index.js +4 -3
  127. package/dist/components/drawer/index.js +7 -6
  128. package/dist/components/file-item/index.js +4 -3
  129. package/dist/components/file-preview/index.js +4 -3
  130. package/dist/components/file-preview/index.js.map +1 -1
  131. package/dist/components/file-trigger/index.js +4 -3
  132. package/dist/components/file-upload/index.js +4 -3
  133. package/dist/components/file-upload/index.js.map +1 -1
  134. package/dist/components/hover-card/index.js +3 -3
  135. package/dist/components/image-cropper/index.js +4 -3
  136. package/dist/components/kbd/index.js +3 -3
  137. package/dist/components/lightbox/index.js +9 -8
  138. package/dist/components/lightbox/index.js.map +1 -1
  139. package/dist/components/menu/index.js +4 -4
  140. package/dist/components/modal/index.js +7 -6
  141. package/dist/components/multi-combobox-list/index.js +6 -6
  142. package/dist/components/multi-select/index.js +4 -4
  143. package/dist/components/nav-link/index.js +3 -3
  144. package/dist/components/nav-link/tanstack.js +3 -3
  145. package/dist/components/notification/create-notifier.d.ts.map +1 -1
  146. package/dist/components/notification/index.js +18 -6
  147. package/dist/components/notification/index.js.map +1 -1
  148. package/dist/components/notification/types.d.ts +5 -0
  149. package/dist/components/notification/types.d.ts.map +1 -1
  150. package/dist/components/number-input/index.js +4 -4
  151. package/dist/components/overlays/index.js +9 -8
  152. package/dist/components/overlays/index.js.map +1 -1
  153. package/dist/components/photo-capture/index.js +13 -12
  154. package/dist/components/photo-capture/index.js.map +1 -1
  155. package/dist/components/popover/index.js +3 -3
  156. package/dist/components/schedule/index.js +7 -6
  157. package/dist/components/schedule/index.js.map +1 -1
  158. package/dist/components/select/index.js +5 -5
  159. package/dist/components/spinner/index.js +1 -1
  160. package/dist/components/spinner/spinner.d.ts +1 -1
  161. package/dist/components/spinner/spinner.d.ts.map +1 -1
  162. package/dist/components/spotlight/index.js +11 -10
  163. package/dist/components/spotlight/index.js.map +1 -1
  164. package/dist/components/tags-input/index.js +3 -3
  165. package/dist/components/theme-icon/index.js +1 -1
  166. package/dist/components/theme-icon/theme-icon.d.ts.map +1 -1
  167. package/dist/components/time-input/index.js +5 -5
  168. package/dist/components/tooltip/index.js +2 -2
  169. package/dist/components/tree-multi-select/index.js +4 -4
  170. package/dist/components/tree-select/index.js +4 -4
  171. package/dist/index.js +2 -2
  172. package/dist/lib/button-colors.d.ts +25 -0
  173. package/dist/lib/button-colors.d.ts.map +1 -0
  174. package/dist/lib/overlay-container.d.ts.map +1 -1
  175. package/dist/lib/overlay-layer.d.ts +10 -3
  176. package/dist/lib/overlay-layer.d.ts.map +1 -1
  177. package/dist/theme.css +14 -6
  178. package/package.json +1 -1
  179. package/src/colors.css +328 -0
  180. package/src/components/action-icon/action-icon.test.tsx +22 -0
  181. package/src/components/action-icon/action-icon.tsx +24 -28
  182. package/src/components/badge/badge.test.tsx +53 -10
  183. package/src/components/badge/badge.tsx +64 -11
  184. package/src/components/button/button.test.tsx +41 -5
  185. package/src/components/button/button.tsx +28 -33
  186. package/src/components/data-table/data-table-features.ts +11 -0
  187. package/src/components/data-table/data-table-group.test.tsx +1007 -0
  188. package/src/components/data-table/data-table-group.tsx +474 -0
  189. package/src/components/data-table/data-table-grouping.ts +160 -0
  190. package/src/components/data-table/data-table-icons.tsx +8 -1
  191. package/src/components/data-table/data-table-meta.ts +9 -0
  192. package/src/components/data-table/data-table-pagination.tsx +5 -1
  193. package/src/components/data-table/data-table-stack.tsx +165 -43
  194. package/src/components/data-table/data-table-toolbar.tsx +110 -2
  195. package/src/components/data-table/data-table-translations.ts +44 -0
  196. package/src/components/data-table/data-table.tsx +420 -150
  197. package/src/components/data-table/index.ts +4 -0
  198. package/src/components/drawer/drawer.tsx +1 -1
  199. package/src/components/modal/modal.tsx +1 -1
  200. package/src/components/notification/create-notifier.tsx +18 -2
  201. package/src/components/notification/notify-in-modal.test.tsx +128 -0
  202. package/src/components/notification/types.ts +5 -0
  203. package/src/components/spinner/spinner.tsx +3 -3
  204. package/src/components/theme-icon/theme-icon.test.tsx +7 -0
  205. package/src/components/theme-icon/theme-icon.tsx +10 -6
  206. package/src/lib/button-colors.ts +55 -0
  207. package/src/lib/overlay-container.tsx +2 -1
  208. package/src/lib/overlay-layer.test.tsx +63 -1
  209. package/src/lib/overlay-layer.tsx +29 -6
  210. package/src/theme.css +14 -6
  211. package/src/theme.test.ts +14 -10
  212. package/dist/chunk-7P32XCEU.js.map +0 -1
  213. package/dist/chunk-BMEJBY6H.js.map +0 -1
  214. package/dist/chunk-D2QIPQ74.js.map +0 -1
  215. package/dist/chunk-F374BNL4.js.map +0 -1
  216. package/dist/chunk-H7WOMZXO.js.map +0 -1
  217. package/dist/chunk-N42EFOML.js.map +0 -1
  218. package/dist/chunk-SCXXJDKQ.js.map +0 -1
  219. package/dist/chunk-YMVUQ2L7.js.map +0 -1
  220. package/dist/chunk-ZVECQBGG.js.map +0 -1
@@ -0,0 +1,1007 @@
1
+ import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"
2
+ import { describe, expect, it, vi } from "vitest"
3
+ import { DataTable, type DataTableProps } from "./data-table"
4
+ import { createColumnHelper } from "./data-table-features"
5
+ import { isGroupExpanded, toggleGroupExpanded } from "./data-table-grouping"
6
+ import { pickGrouping } from "./data-table-toolbar"
7
+
8
+ type Issue = {
9
+ id: string
10
+ title: string
11
+ status: "open" | "confirmed" | "fixed" | "dismissed"
12
+ kind: "seat" | "room"
13
+ building: string
14
+ }
15
+
16
+ // Deliberately not in status order: the groups must not follow the data.
17
+ const ISSUES: Issue[] = [
18
+ { id: "1", title: "Broken screen", status: "open", kind: "seat", building: "B1" },
19
+ { id: "2", title: "No power", status: "fixed", kind: "room", building: "B2" },
20
+ { id: "3", title: "Sticky keys", status: "confirmed", kind: "seat", building: "B2" },
21
+ { id: "4", title: "Door jammed", status: "open", kind: "room", building: "B1" },
22
+ { id: "5", title: "Wobbly chair", status: "fixed", kind: "room", building: "B10" },
23
+ { id: "6", title: "Dead pixel", status: "open", kind: "seat", building: "B2" },
24
+ ]
25
+
26
+ const STATUS_OPTIONS = [
27
+ { value: "open", label: "Reported", color: "orange" },
28
+ { value: "confirmed", label: "In hand", color: "blue" },
29
+ { value: "fixed", label: "Fixed", color: "green" },
30
+ { value: "dismissed", label: "Dismissed", color: "neutral" },
31
+ ] as const
32
+
33
+ const STATUS_LABEL = Object.fromEntries(STATUS_OPTIONS.map((o) => [o.value, o.label]))
34
+
35
+ const col = createColumnHelper<Issue>()
36
+ const columns = [
37
+ col.accessor("title", { header: "Title" }),
38
+ col.accessor("status", {
39
+ header: "Status",
40
+ cell: ({ getValue }) => <span data-testid="status-cell">{STATUS_LABEL[getValue()]}</span>,
41
+ meta: { groupable: true, filter: { type: "select", options: STATUS_OPTIONS } },
42
+ }),
43
+ col.accessor("kind", { header: "Kind", meta: { groupable: true } }),
44
+ col.accessor("building", { header: "Building", meta: { groupable: true } }),
45
+ ]
46
+
47
+ const renderTable = (props: Partial<DataTableProps<Issue>> = {}) =>
48
+ render(<DataTable columns={columns} data={ISSUES} getRowId={(issue) => issue.id} {...props} />)
49
+
50
+ const byStatus = { initialState: { grouping: ["status"] } }
51
+
52
+ /** The group header rows, in document order, as their test ids. */
53
+ const groupIds = (container: HTMLElement) =>
54
+ Array.from(container.querySelectorAll("tr[data-group]")).map((row) =>
55
+ row.getAttribute("data-testid"),
56
+ )
57
+
58
+ /** The data rows under a group's `<tbody>`, as their test ids. */
59
+ const rowIds = (container: HTMLElement) =>
60
+ Array.from(container.querySelectorAll("tbody tr:not([data-placeholder])")).map((row) =>
61
+ row.getAttribute("data-testid"),
62
+ )
63
+
64
+ const flush = () => new Promise((resolve) => setTimeout(resolve, 0))
65
+
66
+ describe("expanded state helpers", () => {
67
+ it("reads a group as open unless it is explicitly folded", () => {
68
+ expect(isGroupExpanded(true, "status:open")).toBe(true)
69
+ expect(isGroupExpanded({}, "status:open")).toBe(true)
70
+ expect(isGroupExpanded({ "status:open": false }, "status:open")).toBe(false)
71
+ expect(isGroupExpanded({ "status:open": false }, "status:fixed")).toBe(true)
72
+ })
73
+
74
+ it("folds by writing false and opens by dropping the key", () => {
75
+ expect(toggleGroupExpanded(true, "status:open")).toEqual({ "status:open": false })
76
+ expect(toggleGroupExpanded({ "status:open": false }, "status:open")).toEqual({})
77
+ expect(toggleGroupExpanded({ "status:open": false }, "status:fixed")).toEqual({
78
+ "status:open": false,
79
+ "status:fixed": false,
80
+ })
81
+ })
82
+ })
83
+
84
+ describe("pickGrouping", () => {
85
+ it("sets, replaces and clears a level", () => {
86
+ expect(pickGrouping([], 0, "status")).toEqual(["status"])
87
+ expect(pickGrouping(["status"], 1, "building")).toEqual(["status", "building"])
88
+ expect(pickGrouping(["status", "building"], 0, "kind")).toEqual(["kind", "building"])
89
+ expect(pickGrouping(["status", "building"], 1, "__none__")).toEqual(["status"])
90
+ expect(pickGrouping(["status", "building"], 0, "__none__")).toEqual([])
91
+ })
92
+
93
+ it("drops a second level that the first level now takes", () => {
94
+ expect(pickGrouping(["status", "building"], 0, "building")).toEqual(["building"])
95
+ })
96
+ })
97
+
98
+ describe("DataTable grouping", () => {
99
+ it("renders a flat table until a grouping is set", () => {
100
+ const { container } = renderTable()
101
+ expect(container.querySelectorAll("tbody")).toHaveLength(1)
102
+ expect(groupIds(container)).toEqual([])
103
+ expect(rowIds(container)).toEqual(ISSUES.map((issue) => `row-${issue.id}`))
104
+ })
105
+
106
+ it("groups rows under one header per value, and ungroups again", () => {
107
+ const { container, rerender } = render(
108
+ <DataTable columns={columns} data={ISSUES} getRowId={(i) => i.id} grouping={["status"]} />,
109
+ )
110
+ expect(groupIds(container)).toEqual([
111
+ "group-status:open",
112
+ "group-status:confirmed",
113
+ "group-status:fixed",
114
+ ])
115
+ // Every row is still there, now in group order.
116
+ expect(rowIds(container)).toEqual(["row-1", "row-4", "row-6", "row-3", "row-2", "row-5"])
117
+ // The column header row appears once, above the groups.
118
+ expect(container.querySelectorAll("thead tr")).toHaveLength(1)
119
+
120
+ rerender(<DataTable columns={columns} data={ISSUES} getRowId={(i) => i.id} grouping={[]} />)
121
+ expect(groupIds(container)).toEqual([])
122
+ expect(rowIds(container)).toEqual(ISSUES.map((issue) => `row-${issue.id}`))
123
+ })
124
+
125
+ it("reports a grouping change through onGroupingChange", async () => {
126
+ const onGroupingChange = vi.fn()
127
+ renderTable({ enableGrouping: true, onGroupingChange })
128
+ fireEvent.click(screen.getByRole("button", { name: "Group rows" }))
129
+ fireEvent.click(await screen.findByRole("menuitemradio", { name: "Kind" }))
130
+ await waitFor(() => expect(onGroupingChange).toHaveBeenCalledWith(["kind"]))
131
+ })
132
+
133
+ it("ignores a column that didn't opt in", () => {
134
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
135
+ const { container } = renderTable({ grouping: ["title"] })
136
+ expect(groupIds(container)).toEqual([])
137
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("meta.groupable"))
138
+ warn.mockRestore()
139
+ })
140
+
141
+ describe("group order", () => {
142
+ it("follows the column's select options", () => {
143
+ const { container } = renderTable(byStatus)
144
+ expect(groupIds(container)).toEqual([
145
+ "group-status:open",
146
+ "group-status:confirmed",
147
+ "group-status:fixed",
148
+ ])
149
+ })
150
+
151
+ it("falls back to ascending by value, numbers in order", () => {
152
+ const { container } = renderTable({ initialState: { grouping: ["building"] } })
153
+ expect(groupIds(container)).toEqual([
154
+ "group-building:B1",
155
+ "group-building:B2",
156
+ "group-building:B10",
157
+ ])
158
+ })
159
+
160
+ it("takes a custom compare from meta.groupOrder", () => {
161
+ const reversed = columns.map((column) =>
162
+ column.id === "status" || ("accessorKey" in column && column.accessorKey === "status")
163
+ ? {
164
+ ...column,
165
+ meta: { ...column.meta, groupOrder: (a: string, b: string) => b.localeCompare(a) },
166
+ }
167
+ : column,
168
+ )
169
+ const { container } = renderTable({ ...byStatus, columns: reversed })
170
+ expect(groupIds(container)).toEqual([
171
+ "group-status:open",
172
+ "group-status:fixed",
173
+ "group-status:confirmed",
174
+ ])
175
+ })
176
+
177
+ it("keeps the group order when the rows are sorted, and sorts inside each group", () => {
178
+ const { container } = renderTable({
179
+ initialState: { grouping: ["status"], sorting: [{ id: "title", desc: true }] },
180
+ })
181
+ expect(groupIds(container)).toEqual([
182
+ "group-status:open",
183
+ "group-status:confirmed",
184
+ "group-status:fixed",
185
+ ])
186
+ // Door jammed, Dead pixel, Broken screen | Sticky keys | Wobbly chair, No power
187
+ expect(rowIds(container)).toEqual(["row-4", "row-6", "row-1", "row-3", "row-5", "row-2"])
188
+ })
189
+ })
190
+
191
+ describe("group header", () => {
192
+ it("draws the group the way its cells draw it, with its row count", () => {
193
+ renderTable(byStatus)
194
+ const header = screen.getByTestId("group-status:open")
195
+ // The column's own cell renderer, not the raw value.
196
+ expect(within(header).getByTestId("status-cell").textContent).toBe("Reported")
197
+ expect(within(header).getByText("3 rows")).toBeTruthy()
198
+ expect(within(screen.getByTestId("group-status:confirmed")).getByText("1 row")).toBeTruthy()
199
+ })
200
+
201
+ it("replaces the value through renderGroupHeader", () => {
202
+ renderTable({
203
+ ...byStatus,
204
+ renderGroupHeader: ({ label, count, rows, column }) =>
205
+ `${column.id}=${label} (${rows.length}/${count})`,
206
+ })
207
+ expect(screen.getByTestId("group-status:open").textContent).toContain("status=Reported (3/3)")
208
+ })
209
+
210
+ it("names a group with no value", () => {
211
+ const withBlank = [...ISSUES, { ...ISSUES[0], id: "7", building: "" } as Issue]
212
+ renderTable({ data: withBlank, initialState: { grouping: ["building"] } })
213
+ expect(screen.getByRole("button", { name: "No value" })).toBeTruthy()
214
+ })
215
+ })
216
+
217
+ describe("grouped column visibility", () => {
218
+ it("hides the grouped column by default and brings it back on ungroup", () => {
219
+ const props = { columns, data: ISSUES, getRowId: (issue: Issue) => issue.id }
220
+ const { rerender } = render(<DataTable {...props} grouping={["status"]} />)
221
+ expect(screen.queryByTestId("header-status")).toBeNull()
222
+ expect(screen.queryByTestId("row-1-cell-status")).toBeNull()
223
+ // The group header still draws the column's own cell.
224
+ expect(
225
+ within(screen.getByTestId("group-status:open")).getByTestId("status-cell").textContent,
226
+ ).toBe("Reported")
227
+
228
+ rerender(<DataTable {...props} grouping={["kind"]} />)
229
+ expect(screen.getByTestId("header-status")).toBeTruthy()
230
+ expect(screen.queryByTestId("header-kind")).toBeNull()
231
+
232
+ rerender(<DataTable {...props} grouping={[]} />)
233
+ expect(screen.getByTestId("header-status")).toBeTruthy()
234
+ expect(screen.getByTestId("header-kind")).toBeTruthy()
235
+ })
236
+
237
+ it("hides both columns of a two-level grouping", () => {
238
+ renderTable({ initialState: { grouping: ["status", "building"] } })
239
+ expect(screen.queryByTestId("header-status")).toBeNull()
240
+ expect(screen.queryByTestId("header-building")).toBeNull()
241
+ expect(screen.getByTestId("header-kind")).toBeTruthy()
242
+ })
243
+
244
+ it("shows it again from the column menu, with an explicit entry", async () => {
245
+ const onColumnVisibilityChange = vi.fn()
246
+ renderTable({ ...byStatus, enableColumnVisibility: true, onColumnVisibilityChange })
247
+ fireEvent.click(screen.getByRole("button", { name: "Toggle column visibility" }))
248
+ const item = await screen.findByRole("menuitemcheckbox", { name: "Status" })
249
+ expect(item.getAttribute("aria-checked")).toBe("false")
250
+ fireEvent.click(item)
251
+ // Only the user's choice reaches the app: the default hiding is never written.
252
+ await waitFor(() => expect(onColumnVisibilityChange).toHaveBeenCalledWith({ status: true }))
253
+ await waitFor(() => expect(screen.getByTestId("header-status")).toBeTruthy())
254
+ })
255
+
256
+ it("keeps it shown when columnVisibility says so", () => {
257
+ renderTable({ ...byStatus, columnVisibility: { status: true } })
258
+ expect(screen.getByTestId("header-status")).toBeTruthy()
259
+ expect(screen.getByTestId("row-1-cell-status")).toBeTruthy()
260
+ })
261
+
262
+ it("keeps every grouped column with hideGroupedColumns off", () => {
263
+ renderTable({
264
+ initialState: { grouping: ["status", "building"] },
265
+ hideGroupedColumns: false,
266
+ })
267
+ expect(screen.getByTestId("header-status")).toBeTruthy()
268
+ expect(screen.getByTestId("header-building")).toBeTruthy()
269
+ expect(screen.getByTestId("row-1-cell-status")).toBeTruthy()
270
+ })
271
+
272
+ it("still lets the user hide one with hideGroupedColumns off", () => {
273
+ renderTable({
274
+ ...byStatus,
275
+ hideGroupedColumns: false,
276
+ columnVisibility: { status: false },
277
+ })
278
+ expect(screen.queryByTestId("header-status")).toBeNull()
279
+ })
280
+
281
+ it("leaves a column that can't be hidden", () => {
282
+ const pinned = columns.map((column) =>
283
+ "accessorKey" in column && column.accessorKey === "status"
284
+ ? { ...column, enableHiding: false }
285
+ : column,
286
+ )
287
+ renderTable({ ...byStatus, columns: pinned })
288
+ expect(screen.getByTestId("header-status")).toBeTruthy()
289
+ })
290
+ })
291
+
292
+ describe("folding", () => {
293
+ it("hides a group's rows and keeps its header (uncontrolled)", () => {
294
+ const { container } = renderTable(byStatus)
295
+ const toggle = screen.getByRole("button", { name: "Reported" })
296
+ expect(toggle.getAttribute("aria-expanded")).toBe("true")
297
+
298
+ fireEvent.click(toggle)
299
+ expect(toggle.getAttribute("aria-expanded")).toBe("false")
300
+ expect(groupIds(container)).toHaveLength(3)
301
+ expect(rowIds(container)).toEqual(["row-3", "row-2", "row-5"])
302
+ // Folding moved no focus and unmounted nothing the user was on.
303
+ expect(document.body.contains(toggle)).toBe(true)
304
+
305
+ fireEvent.click(toggle)
306
+ expect(rowIds(container)).toHaveLength(6)
307
+ })
308
+
309
+ it("follows a controlled expanded state", () => {
310
+ const onExpandedChange = vi.fn()
311
+ const { container } = renderTable({
312
+ ...byStatus,
313
+ expanded: { "status:fixed": false },
314
+ onExpandedChange,
315
+ })
316
+ expect(rowIds(container)).toEqual(["row-1", "row-4", "row-6", "row-3"])
317
+
318
+ fireEvent.click(screen.getByRole("button", { name: "Reported" }))
319
+ expect(onExpandedChange).toHaveBeenLastCalledWith({
320
+ "status:fixed": false,
321
+ "status:open": false,
322
+ })
323
+ // Controlled: nothing changes until the prop does.
324
+ expect(rowIds(container)).toHaveLength(4)
325
+
326
+ fireEvent.click(screen.getByRole("button", { name: "Fixed" }))
327
+ expect(onExpandedChange).toHaveBeenLastCalledWith({})
328
+ })
329
+
330
+ it("opens a group that appears after the fold state was written", () => {
331
+ const props = {
332
+ columns,
333
+ getRowId: (issue: Issue) => issue.id,
334
+ grouping: ["status"],
335
+ expanded: { "status:open": false },
336
+ }
337
+ const { container, rerender } = render(
338
+ <DataTable {...props} data={ISSUES.filter((issue) => issue.status === "open")} />,
339
+ )
340
+ expect(rowIds(container)).toEqual([])
341
+ rerender(<DataTable {...props} data={ISSUES} />)
342
+ expect(rowIds(container)).toEqual(["row-3", "row-2", "row-5"])
343
+ })
344
+ })
345
+
346
+ describe("empty groups", () => {
347
+ it("hides a group that has options but no rows by default", () => {
348
+ const { container } = renderTable(byStatus)
349
+ expect(groupIds(container)).not.toContain("group-status:dismissed")
350
+ })
351
+
352
+ it("shows it, in option order, when hideEmptyGroups is off", () => {
353
+ const { container } = renderTable({ ...byStatus, hideEmptyGroups: false })
354
+ expect(groupIds(container)).toEqual([
355
+ "group-status:open",
356
+ "group-status:confirmed",
357
+ "group-status:fixed",
358
+ "group-status:dismissed",
359
+ ])
360
+ const header = screen.getByTestId("group-status:dismissed")
361
+ expect(within(header).getByText("0 rows")).toBeTruthy()
362
+ // Nothing to fold: the button is there for its name, not as a control.
363
+ const toggle = within(header).getByRole("button", { name: "Dismissed" })
364
+ expect(toggle.hasAttribute("disabled")).toBe(true)
365
+ expect(toggle.hasAttribute("aria-expanded")).toBe(false)
366
+ })
367
+
368
+ it("toggles from the group menu", async () => {
369
+ const onHideEmptyGroupsChange = vi.fn()
370
+ renderTable({ ...byStatus, enableGrouping: true, onHideEmptyGroupsChange })
371
+ fireEvent.click(screen.getByRole("button", { name: "Group rows" }))
372
+ const item = await screen.findByRole("menuitemcheckbox", { name: "Hide empty groups" })
373
+ expect(item.getAttribute("aria-checked")).toBe("true")
374
+ fireEvent.click(item)
375
+ await waitFor(() => expect(onHideEmptyGroupsChange).toHaveBeenCalledWith(false))
376
+ })
377
+ })
378
+
379
+ describe("filtering", () => {
380
+ it("applies the global search across groups and drops emptied ones", () => {
381
+ const { container } = renderTable({ ...byStatus, globalFilter: "B1" })
382
+ // B1: Broken screen, Door jammed (open). B10: Wobbly chair (fixed). "In hand" is out.
383
+ expect(groupIds(container)).toEqual(["group-status:open", "group-status:fixed"])
384
+ expect(rowIds(container)).toEqual(["row-1", "row-4", "row-5"])
385
+ expect(within(screen.getByTestId("group-status:open")).getByText("2 rows")).toBeTruthy()
386
+ })
387
+
388
+ it("applies a column filter across groups", () => {
389
+ const { container } = renderTable({
390
+ ...byStatus,
391
+ filters: [{ id: "status", value: "fixed" }],
392
+ })
393
+ expect(groupIds(container)).toEqual(["group-status:fixed"])
394
+ })
395
+
396
+ it("shows the empty state when no group is left", () => {
397
+ renderTable({ ...byStatus, globalFilter: "zzz" })
398
+ expect(screen.getByText("No results found")).toBeTruthy()
399
+ })
400
+ })
401
+
402
+ describe("selection", () => {
403
+ const selecting = { ...byStatus, enableRowSelection: true }
404
+
405
+ it("selects a whole group from its header checkbox", async () => {
406
+ const onRowSelectionChange = vi.fn()
407
+ renderTable({ ...selecting, onRowSelectionChange })
408
+ await flush()
409
+ fireEvent.click(screen.getByRole("checkbox", { name: "Select group Reported" }))
410
+ // Real row ids only: the group row itself is never selected.
411
+ await waitFor(() =>
412
+ expect(onRowSelectionChange).toHaveBeenCalledWith({ "1": true, "4": true, "6": true }),
413
+ )
414
+ })
415
+
416
+ it("reads all, some and none of a group's rows", async () => {
417
+ const { rerender } = render(
418
+ <DataTable
419
+ columns={columns}
420
+ data={ISSUES}
421
+ getRowId={(issue) => issue.id}
422
+ {...selecting}
423
+ rowSelection={{}}
424
+ />,
425
+ )
426
+ const state = () =>
427
+ (screen.getByRole("checkbox", { name: "Select group Reported" }) as HTMLInputElement)
428
+ .closest("[data-part=root]")
429
+ ?.getAttribute("data-state")
430
+ await flush()
431
+ expect(state()).toBe("unchecked")
432
+
433
+ const withSelection = (rowSelection: Record<string, true>) =>
434
+ rerender(
435
+ <DataTable
436
+ columns={columns}
437
+ data={ISSUES}
438
+ getRowId={(issue) => issue.id}
439
+ {...selecting}
440
+ rowSelection={rowSelection}
441
+ />,
442
+ )
443
+ withSelection({ "1": true })
444
+ await waitFor(() => expect(state()).toBe("indeterminate"))
445
+ withSelection({ "1": true, "4": true, "6": true })
446
+ await waitFor(() => expect(state()).toBe("checked"))
447
+ })
448
+
449
+ it("clears a fully selected group and leaves the others alone", async () => {
450
+ const onRowSelectionChange = vi.fn()
451
+ renderTable({
452
+ ...selecting,
453
+ rowSelection: { "1": true, "4": true, "6": true, "3": true },
454
+ onRowSelectionChange,
455
+ })
456
+ await flush()
457
+ fireEvent.click(screen.getByRole("checkbox", { name: "Select group Reported" }))
458
+ await waitFor(() => expect(onRowSelectionChange).toHaveBeenCalledWith({ "3": true }))
459
+ })
460
+
461
+ it("selects every row from the column header, groups folded or not", async () => {
462
+ const onRowSelectionChange = vi.fn()
463
+ renderTable({
464
+ ...selecting,
465
+ expanded: { "status:fixed": false },
466
+ onRowSelectionChange,
467
+ })
468
+ await flush()
469
+ fireEvent.click(screen.getByRole("checkbox", { name: "Select all rows" }))
470
+ await waitFor(() =>
471
+ expect(onRowSelectionChange).toHaveBeenCalledWith({
472
+ "1": true,
473
+ "2": true,
474
+ "3": true,
475
+ "4": true,
476
+ "5": true,
477
+ "6": true,
478
+ }),
479
+ )
480
+ })
481
+
482
+ it("leaves a group with no selectable row without a checkbox", async () => {
483
+ renderTable({
484
+ ...byStatus,
485
+ enableRowSelection: (row) => row.original.status !== "confirmed",
486
+ })
487
+ await flush()
488
+ expect(screen.queryByRole("checkbox", { name: "Select group In hand" })).toBeNull()
489
+ expect(screen.getByRole("checkbox", { name: "Select group Fixed" })).toBeTruthy()
490
+ })
491
+ })
492
+
493
+ describe("group pagination", () => {
494
+ it("shows groupPageSize rows per group, then more on demand", () => {
495
+ const { container } = renderTable({ ...byStatus, groupPageSize: 2 })
496
+ // Reported holds 3 rows: two shown and a "Show 1 more"; no other group overflows.
497
+ expect(rowIds(container)).toEqual(["row-1", "row-4", "row-3", "row-2", "row-5"])
498
+ const more = screen.getByRole("button", { name: "Show 1 more" })
499
+ expect(screen.getAllByRole("button", { name: /^Show \d+ more$/ })).toHaveLength(1)
500
+
501
+ fireEvent.click(more)
502
+ expect(rowIds(container)).toHaveLength(6)
503
+ expect(screen.queryByRole("button", { name: /^Show \d+ more$/ })).toBeNull()
504
+ })
505
+
506
+ it("counts the whole group in its header, not the rows shown", () => {
507
+ renderTable({ ...byStatus, groupPageSize: 1 })
508
+ expect(within(screen.getByTestId("group-status:open")).getByText("3 rows")).toBeTruthy()
509
+ expect(screen.getAllByRole("button", { name: "Show 1 more" })).toHaveLength(2)
510
+ })
511
+
512
+ it("starts the limits over when the row set changes", () => {
513
+ const { container, rerender } = renderTable({ ...byStatus, groupPageSize: 2 })
514
+ fireEvent.click(screen.getByRole("button", { name: "Show 1 more" }))
515
+ expect(rowIds(container)).toHaveLength(6)
516
+ rerender(
517
+ <DataTable
518
+ columns={columns}
519
+ data={ISSUES}
520
+ getRowId={(issue) => issue.id}
521
+ {...byStatus}
522
+ groupPageSize={2}
523
+ sorting={[{ id: "title", desc: false }]}
524
+ />,
525
+ )
526
+ expect(screen.getByRole("button", { name: "Show 1 more" })).toBeTruthy()
527
+ })
528
+
529
+ it("shows every row when pagination is off", () => {
530
+ const { container } = renderTable({
531
+ ...byStatus,
532
+ groupPageSize: 1,
533
+ enablePagination: false,
534
+ })
535
+ expect(rowIds(container)).toHaveLength(6)
536
+ expect(screen.queryByRole("button", { name: /^Show \d+ more$/ })).toBeNull()
537
+ })
538
+
539
+ it("replaces the pager with a total while grouped", () => {
540
+ const { rerender } = render(
541
+ <DataTable columns={columns} data={ISSUES} getRowId={(i) => i.id} grouping={["status"]} />,
542
+ )
543
+ expect(screen.getByRole("status").textContent).toBe("6 rows in 3 groups")
544
+ expect(screen.queryByRole("button", { name: "Next page" })).toBeNull()
545
+ expect(screen.queryByText("Rows per page")).toBeNull()
546
+
547
+ rerender(<DataTable columns={columns} data={ISSUES} getRowId={(i) => i.id} grouping={[]} />)
548
+ expect(screen.getByRole("button", { name: "Next page" })).toBeTruthy()
549
+ })
550
+
551
+ it("is not cut by the table's own page size", () => {
552
+ const { container } = renderTable({
553
+ ...byStatus,
554
+ initialState: { grouping: ["status"], pagination: { pageIndex: 0, pageSize: 2 } },
555
+ })
556
+ expect(rowIds(container)).toHaveLength(6)
557
+ })
558
+ })
559
+
560
+ describe("nested grouping", () => {
561
+ const nested = { initialState: { grouping: ["status", "building"] } }
562
+
563
+ it("draws sub-group headers inside the parent's row group", () => {
564
+ const { container } = renderTable(nested)
565
+ expect(container.querySelectorAll("tbody")).toHaveLength(3)
566
+ expect(groupIds(container)).toEqual([
567
+ "group-status:open",
568
+ "group-status:open>building:B1",
569
+ "group-status:open>building:B2",
570
+ "group-status:confirmed",
571
+ "group-status:confirmed>building:B2",
572
+ "group-status:fixed",
573
+ "group-status:fixed>building:B2",
574
+ "group-status:fixed>building:B10",
575
+ ])
576
+ const sub = screen.getByTestId("group-status:open>building:B1")
577
+ expect(sub.getAttribute("data-depth")).toBe("1")
578
+ expect(sub.closest("tbody")).toBe(screen.getByTestId("group-status:open").closest("tbody"))
579
+ expect(sub.querySelector("th")?.getAttribute("scope")).toBe("row")
580
+ })
581
+
582
+ it("indents the rows under a sub-group, and only there", () => {
583
+ const indentOf = (testId: string) =>
584
+ screen.getByTestId(testId).style.getPropertyValue("--data-table-leaf-indent")
585
+ const { unmount } = renderTable({ ...nested, enableRowSelection: true })
586
+ // The first data cell steps in; the select column and the other cells don't.
587
+ expect(indentOf("row-1-cell-title")).toBe("40px")
588
+ expect(indentOf("row-1-cell-__select__")).toBe("")
589
+ expect(indentOf("row-1-cell-kind")).toBe("")
590
+ // The sub-group's checkbox stays in line: only its fold button steps in.
591
+ const sub = screen.getByTestId("group-status:open>building:B1")
592
+ expect(within(sub).getByRole("button").className).toContain("ms-6")
593
+ unmount()
594
+
595
+ // One level: nothing to step under.
596
+ renderTable(byStatus)
597
+ expect(indentOf("row-1-cell-title")).toBe("")
598
+ })
599
+
600
+ it("counts every leaf under a parent", () => {
601
+ renderTable(nested)
602
+ const parent = screen.getByTestId("group-status:open")
603
+ expect(within(parent).getByText("3 rows")).toBeTruthy()
604
+ expect(
605
+ within(screen.getByTestId("group-status:open>building:B1")).getByText("2 rows"),
606
+ ).toBeTruthy()
607
+ })
608
+
609
+ it("folds a parent with everything under it, and a sub-group on its own", () => {
610
+ const { container } = renderTable(nested)
611
+ fireEvent.click(
612
+ within(screen.getByTestId("group-status:open>building:B1")).getByRole("button"),
613
+ )
614
+ expect(rowIds(container)).toEqual(["row-6", "row-3", "row-2", "row-5"])
615
+
616
+ fireEvent.click(within(screen.getByTestId("group-status:open")).getByRole("button"))
617
+ expect(groupIds(container)).not.toContain("group-status:open>building:B2")
618
+ expect(rowIds(container)).toEqual(["row-3", "row-2", "row-5"])
619
+
620
+ // Reopening the parent brings the sub-group back as it was left: folded.
621
+ fireEvent.click(within(screen.getByTestId("group-status:open")).getByRole("button"))
622
+ expect(rowIds(container)).toEqual(["row-6", "row-3", "row-2", "row-5"])
623
+ })
624
+
625
+ it("pages the innermost groups", () => {
626
+ renderTable({ ...nested, groupPageSize: 1 })
627
+ // Only status:open > B1 holds two rows.
628
+ expect(screen.getAllByRole("button", { name: "Show 1 more" })).toHaveLength(1)
629
+ expect(
630
+ within(screen.getByTestId("group-status:open>building:B1-more")).getByRole("button"),
631
+ ).toBeTruthy()
632
+ })
633
+
634
+ it("selects every leaf from a parent's checkbox", async () => {
635
+ const onRowSelectionChange = vi.fn()
636
+ renderTable({ ...nested, enableRowSelection: true, onRowSelectionChange })
637
+ await flush()
638
+ const parent = screen.getByTestId("group-status:open")
639
+ fireEvent.click(within(parent).getByRole("checkbox"))
640
+ await waitFor(() =>
641
+ expect(onRowSelectionChange).toHaveBeenCalledWith({ "1": true, "4": true, "6": true }),
642
+ )
643
+ })
644
+
645
+ it("only adds empty groups at the first level", () => {
646
+ const { container } = renderTable({
647
+ initialState: { grouping: ["building", "status"] },
648
+ hideEmptyGroups: false,
649
+ })
650
+ expect(groupIds(container).filter((id) => id?.includes("dismissed"))).toEqual([])
651
+ })
652
+
653
+ it("offers a second level in the menu once the first is set", async () => {
654
+ const onGroupingChange = vi.fn()
655
+ renderTable({ ...byStatus, enableGrouping: true, onGroupingChange })
656
+ fireEvent.click(screen.getByRole("button", { name: "Group rows" }))
657
+ await screen.findByText("Then by")
658
+ const groups = screen.getAllByRole("group")
659
+ const thenBy = groups[groups.length - 1] as HTMLElement
660
+ // The first level's column is not offered again.
661
+ expect(
662
+ within(thenBy)
663
+ .getAllByRole("menuitemradio")
664
+ .map((item) => item.textContent),
665
+ ).toEqual(["None", "Kind", "Building"])
666
+ fireEvent.click(within(thenBy).getByRole("menuitemradio", { name: "Building" }))
667
+ await waitFor(() => expect(onGroupingChange).toHaveBeenCalledWith(["status", "building"]))
668
+ })
669
+
670
+ // The menu highlights by item value: a value shared by both levels lit both items.
671
+ it("gives every menu item its own value across the two levels", async () => {
672
+ renderTable({ ...byStatus, enableGrouping: true })
673
+ fireEvent.click(screen.getByRole("button", { name: "Group rows" }))
674
+ await screen.findByText("Then by")
675
+ const ids = screen.getAllByRole("menuitemradio").map((item) => item.id)
676
+ expect(ids).toHaveLength(7)
677
+ expect(new Set(ids).size).toBe(ids.length)
678
+ })
679
+
680
+ it("stops at two levels", () => {
681
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
682
+ const { container } = renderTable({ grouping: ["status", "building", "kind"] })
683
+ expect(groupIds(container).some((id) => id?.includes("kind:"))).toBe(false)
684
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("at most 2 levels"))
685
+ warn.mockRestore()
686
+ })
687
+ })
688
+
689
+ describe("group menu", () => {
690
+ it("lists None and the groupable columns as radios", async () => {
691
+ renderTable({ enableGrouping: true })
692
+ fireEvent.click(screen.getByRole("button", { name: "Group rows" }))
693
+ await screen.findByRole("menuitemradio", { name: "None" })
694
+ expect(screen.getAllByRole("menuitemradio").map((item) => item.textContent)).toEqual([
695
+ "None",
696
+ "Status",
697
+ "Kind",
698
+ "Building",
699
+ ])
700
+ expect(screen.getByRole("menuitemradio", { name: "None" }).getAttribute("aria-checked")).toBe(
701
+ "true",
702
+ )
703
+ // No second level until a first one is picked.
704
+ expect(screen.queryByText("Then by")).toBeNull()
705
+ })
706
+
707
+ it("is absent without enableGrouping, and when no column is groupable", () => {
708
+ const { unmount } = renderTable()
709
+ expect(screen.queryByRole("button", { name: "Group rows" })).toBeNull()
710
+ unmount()
711
+ renderTable({
712
+ enableGrouping: true,
713
+ columns: [col.accessor("title", { header: "Title" })],
714
+ })
715
+ expect(screen.queryByRole("button", { name: "Group rows" })).toBeNull()
716
+ })
717
+
718
+ it("accepts enableGrouping on the column as the opt-in", async () => {
719
+ renderTable({
720
+ enableGrouping: true,
721
+ columns: [col.accessor("title", { header: "Title", enableGrouping: true })],
722
+ })
723
+ fireEvent.click(screen.getByRole("button", { name: "Group rows" }))
724
+ expect(await screen.findByRole("menuitemradio", { name: "Title" })).toBeTruthy()
725
+ })
726
+ })
727
+
728
+ describe("manual grouping", () => {
729
+ const manual = {
730
+ grouping: ["status"],
731
+ manualGrouping: true,
732
+ groups: [
733
+ { value: "fixed", count: 12 },
734
+ { value: "open", count: 3 },
735
+ { value: "dismissed", count: 0 },
736
+ { value: "confirmed", count: 1 },
737
+ ],
738
+ }
739
+
740
+ it("takes the order and the counts from the server's groups", () => {
741
+ const { container } = renderTable(manual)
742
+ expect(groupIds(container)).toEqual([
743
+ "group-status:fixed",
744
+ "group-status:open",
745
+ "group-status:confirmed",
746
+ ])
747
+ expect(within(screen.getByTestId("group-status:fixed")).getByText("12 rows")).toBeTruthy()
748
+ expect(screen.getByRole("status").textContent).toBe("16 rows in 3 groups")
749
+ })
750
+
751
+ it("asks for more while a group is short of its count", () => {
752
+ const onGroupShowMore = vi.fn()
753
+ renderTable({ ...manual, groupPageSize: 5, onGroupShowMore })
754
+ // Only "fixed" has fewer rows loaded (2) than its count (12).
755
+ const more = screen.getByRole("button", { name: "Show 5 more" })
756
+ expect(screen.getAllByRole("button", { name: /^Show \d+ more$/ })).toHaveLength(1)
757
+ fireEvent.click(more)
758
+ expect(onGroupShowMore).toHaveBeenCalledWith({
759
+ columnId: "status",
760
+ value: "fixed",
761
+ loaded: 2,
762
+ })
763
+ })
764
+
765
+ it("draws a group the server lists before any of its rows is loaded", () => {
766
+ const { container } = renderTable({
767
+ ...manual,
768
+ data: ISSUES.filter((issue) => issue.status !== "confirmed"),
769
+ })
770
+ expect(groupIds(container)).toContain("group-status:confirmed")
771
+ expect(
772
+ within(screen.getByTestId("group-status:confirmed-more")).getByRole("button", {
773
+ name: "Show 1 more",
774
+ }),
775
+ ).toBeTruthy()
776
+ })
777
+
778
+ it("shows the server's empty groups on demand", () => {
779
+ const { container } = renderTable({ ...manual, hideEmptyGroups: false })
780
+ expect(groupIds(container)).toContain("group-status:dismissed")
781
+ })
782
+
783
+ it("never reads a partly loaded group as fully selected, and still clears it", async () => {
784
+ const onRowSelectionChange = vi.fn()
785
+ renderTable({
786
+ ...manual,
787
+ enableRowSelection: true,
788
+ // Both loaded "fixed" rows, of the 12 the server counts.
789
+ rowSelection: { "2": true, "5": true },
790
+ onRowSelectionChange,
791
+ })
792
+ await flush()
793
+ const checkbox = screen.getByRole("checkbox", { name: "Select group Fixed" })
794
+ await waitFor(() =>
795
+ expect(checkbox.closest("[data-part=root]")?.getAttribute("data-state")).toBe(
796
+ "indeterminate",
797
+ ),
798
+ )
799
+ fireEvent.click(checkbox)
800
+ await waitFor(() => expect(onRowSelectionChange).toHaveBeenCalledWith({}))
801
+ })
802
+
803
+ it("groups one level deep", () => {
804
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
805
+ const { container } = renderTable({ ...manual, grouping: ["status", "kind"] })
806
+ expect(container.querySelectorAll("tr[data-depth='1']")).toHaveLength(0)
807
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("one level deep"))
808
+ warn.mockRestore()
809
+ })
810
+ })
811
+
812
+ describe("server pages, client grouping", () => {
813
+ it("keeps the page controls and warns that it groups one page", () => {
814
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
815
+ const { container } = renderTable({
816
+ ...byStatus,
817
+ manualPagination: true,
818
+ rowCount: 60,
819
+ groupPageSize: 1,
820
+ })
821
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("manualPagination"))
822
+ // The other pages stay reachable, and the range counts rows, not group headers.
823
+ expect(screen.getByRole("button", { name: "Next page" })).toBeTruthy()
824
+ expect(screen.queryByText("6 rows in 3 groups")).toBeNull()
825
+ // The page is drawn whole: no "Show more" inside a server page.
826
+ expect(rowIds(container)).toHaveLength(6)
827
+ expect(screen.queryByRole("button", { name: /^Show \d+ more$/ })).toBeNull()
828
+ warn.mockRestore()
829
+ })
830
+
831
+ it("counts the page's real rows in the range", () => {
832
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {})
833
+ renderTable({
834
+ ...byStatus,
835
+ manualPagination: true,
836
+ rowCount: 60,
837
+ paginationMode: "rows",
838
+ })
839
+ expect(screen.getByRole("status").textContent).toBe("1 to 6 of 60")
840
+ warn.mockRestore()
841
+ })
842
+
843
+ it("hands the footer to the groups once the server owns them", () => {
844
+ renderTable({
845
+ grouping: ["status"],
846
+ manualPagination: true,
847
+ manualGrouping: true,
848
+ groups: [{ value: "open", count: 30 }],
849
+ })
850
+ expect(screen.queryByRole("button", { name: "Next page" })).toBeNull()
851
+ })
852
+ })
853
+
854
+ describe("stacked mode", () => {
855
+ const stacked = { ...byStatus, responsive: "stack" as const }
856
+ const sections = (container: HTMLElement) =>
857
+ Array.from(container.querySelectorAll("section[data-group]"))
858
+
859
+ it("draws each group as a section heading above its cards", () => {
860
+ const { container } = renderTable(stacked)
861
+ expect(sections(container)).toHaveLength(3)
862
+ const [reported] = sections(container) as [HTMLElement]
863
+ expect(within(reported).getByRole("button", { name: "Reported" })).toBeTruthy()
864
+ expect(within(reported).getByText("3 rows")).toBeTruthy()
865
+ expect(within(reported).getByText("Broken screen")).toBeTruthy()
866
+ expect(within(reported).queryByText("Sticky keys")).toBeNull()
867
+ })
868
+
869
+ it("folds a section with the same state as the table", () => {
870
+ const { container } = renderTable(stacked)
871
+ const [reported] = sections(container) as [HTMLElement]
872
+ fireEvent.click(within(reported).getByRole("button", { name: "Reported" }))
873
+ expect(within(reported).queryByText("Broken screen")).toBeNull()
874
+ // One state for both renderings: the table's group folded too.
875
+ expect(rowIds(container)).toEqual(["row-3", "row-2", "row-5"])
876
+ })
877
+
878
+ it("pages a section's cards", () => {
879
+ const { container } = renderTable({ ...stacked, groupPageSize: 2 })
880
+ const [reported] = sections(container) as [HTMLElement]
881
+ expect(within(reported).queryByText("Dead pixel")).toBeNull()
882
+ fireEvent.click(within(reported).getByRole("button", { name: "Show 1 more" }))
883
+ expect(within(reported).getByText("Dead pixel")).toBeTruthy()
884
+ })
885
+
886
+ it("nests sub-group sections", () => {
887
+ const { container } = renderTable({
888
+ responsive: "stack",
889
+ initialState: { grouping: ["status", "building"] },
890
+ })
891
+ expect(container.querySelectorAll("section[data-depth='1']")).toHaveLength(5)
892
+ })
893
+
894
+ it("names a section whose value holds a space", () => {
895
+ const spaced = ISSUES.map((issue) => ({ ...issue, building: `Building ${issue.building}` }))
896
+ const { container } = renderTable({
897
+ data: spaced,
898
+ responsive: "stack",
899
+ initialState: { grouping: ["building"] },
900
+ })
901
+ for (const section of sections(container)) {
902
+ const labelledBy = section.getAttribute("aria-labelledby") as string
903
+ expect(labelledBy).not.toMatch(/\s/)
904
+ expect(container.ownerDocument.getElementById(labelledBy)).toBeTruthy()
905
+ }
906
+ })
907
+
908
+ it("offers the grouping in the options drawer", async () => {
909
+ renderTable({ ...stacked, enableGrouping: true })
910
+ fireEvent.click(screen.getByRole("button", { name: "Table options" }))
911
+ expect(await screen.findByText("Then by")).toBeTruthy()
912
+ expect(screen.getByRole("checkbox", { name: "Hide empty groups" })).toBeTruthy()
913
+ })
914
+ })
915
+
916
+ describe("accessibility", () => {
917
+ it("makes each group a row group headed by a spanning header cell", () => {
918
+ const { container } = renderTable({ ...byStatus, enableRowSelection: true })
919
+ const bodies = Array.from(container.querySelectorAll("tbody"))
920
+ expect(bodies).toHaveLength(3)
921
+ for (const body of bodies) {
922
+ const cell = body.querySelector("tr:first-child > th")
923
+ expect(cell?.getAttribute("scope")).toBe("rowgroup")
924
+ // Title, Kind, Building and the select column; the grouped Status is hidden.
925
+ expect(cell?.getAttribute("colspan")).toBe("4")
926
+ }
927
+ })
928
+
929
+ it("wires the fold button to the rows it controls", () => {
930
+ renderTable(byStatus)
931
+ const toggle = screen.getByRole("button", { name: "Reported" })
932
+ const body = toggle.closest("tbody") as HTMLElement
933
+ expect(toggle.getAttribute("aria-controls")).toBe(body.id)
934
+ fireEvent.click(toggle)
935
+ expect(toggle.getAttribute("aria-expanded")).toBe("false")
936
+ })
937
+
938
+ it("keeps the fold button and the group checkbox in the tab order", async () => {
939
+ renderTable({ ...byStatus, enableRowSelection: true })
940
+ await flush()
941
+ const header = screen.getByTestId("group-status:open")
942
+ expect(within(header).getByRole("button").tabIndex).toBe(0)
943
+ expect(within(header).getByRole("checkbox", { name: "Select group Reported" })).toBeTruthy()
944
+ })
945
+
946
+ it("never fires onRowClick from a group header", () => {
947
+ const onRowClick = vi.fn()
948
+ renderTable({ ...byStatus, onRowClick })
949
+ fireEvent.click(screen.getByTestId("group-status:open"))
950
+ fireEvent.click(screen.getByText("3 rows"))
951
+ expect(onRowClick).not.toHaveBeenCalled()
952
+ fireEvent.click(screen.getByText("Broken screen"))
953
+ expect(onRowClick).toHaveBeenCalledWith(ISSUES[0])
954
+ })
955
+ })
956
+
957
+ describe("translations and icons", () => {
958
+ it("routes every grouping key", async () => {
959
+ renderTable({
960
+ ...byStatus,
961
+ enableGrouping: true,
962
+ enableRowSelection: true,
963
+ groupPageSize: 2,
964
+ translations: {
965
+ group: "Grouper",
966
+ groupBy: "Grouper par",
967
+ groupThenBy: "Puis par",
968
+ groupNone: "Aucun",
969
+ hideEmptyGroups: "Masquer les groupes vides",
970
+ groupToggle: (label) => `Groupe ${label}`,
971
+ groupCount: (count) => `${count} ligne(s)`,
972
+ selectGroup: (label) => `Sélectionner ${label}`,
973
+ showMore: (count) => `Encore ${count}`,
974
+ groupSummary: ({ rows, groups }) => `${rows} lignes, ${groups} groupes`,
975
+ },
976
+ })
977
+ await flush()
978
+ expect(screen.getByRole("button", { name: "Groupe Reported" })).toBeTruthy()
979
+ expect(screen.getByRole("checkbox", { name: "Sélectionner Reported" })).toBeTruthy()
980
+ expect(screen.getByText("3 ligne(s)")).toBeTruthy()
981
+ expect(screen.getByRole("button", { name: "Encore 1" })).toBeTruthy()
982
+ expect(screen.getByRole("status").textContent).toBe("6 lignes, 3 groupes")
983
+
984
+ fireEvent.click(screen.getByRole("button", { name: "Grouper" }))
985
+ expect(await screen.findByText("Grouper par")).toBeTruthy()
986
+ expect(screen.getByText("Puis par")).toBeTruthy()
987
+ expect(screen.getAllByRole("menuitemradio", { name: "Aucun" })).toHaveLength(2)
988
+ expect(
989
+ screen.getByRole("menuitemcheckbox", { name: "Masquer les groupes vides" }),
990
+ ).toBeTruthy()
991
+ })
992
+
993
+ it("draws the group and fold icons from the icons prop", () => {
994
+ renderTable({
995
+ ...byStatus,
996
+ enableGrouping: true,
997
+ icons: {
998
+ group: <i data-testid="icon-group" />,
999
+ fold: <i data-testid="icon-fold" />,
1000
+ },
1001
+ })
1002
+ expect(screen.getByTestId("icon-group")).toBeTruthy()
1003
+ // One per group header, in the table and nowhere else (not stacked).
1004
+ expect(screen.getAllByTestId("icon-fold")).toHaveLength(3)
1005
+ })
1006
+ })
1007
+ })