Skip to content

Commit 361ad57

Browse files
committed
fix: update sorting tests
1 parent 59ca72c commit 361ad57

1 file changed

Lines changed: 84 additions & 13 deletions

File tree

src/app/dashboard/events/[id]/participants/table/__tests__/sorting.test.tsx

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { cleanup, screen } from "@testing-library/react";
2+
import { format } from "date-fns";
23
import { describe, expect, it } from "vitest";
34

4-
import { stringLikeDataTestCases, textCaseData } from "./mocks/test-cases-data";
5+
import type { Attribute } from "@/types/attributes";
6+
7+
import {
8+
numberCaseData,
9+
stringLikeDataTestCases,
10+
textCaseData,
11+
} from "./mocks/test-cases-data";
512
import { renderTable } from "./utils";
613

714
/**
@@ -10,6 +17,53 @@ import { renderTable } from "./utils";
1017
* But maybe it will be useful in the future, maybe...
1118
*/
1219

20+
interface ParticipantForSortingTest {
21+
createdAt: string;
22+
attributes: {
23+
value?: string | string[] | number | Date | null | undefined;
24+
}[];
25+
}
26+
27+
function getDisplayedAttributeValue(
28+
participant: ParticipantForSortingTest,
29+
attributeType?: string,
30+
) {
31+
const value = participant.attributes[0]?.value;
32+
33+
if (value == null) {
34+
return "";
35+
}
36+
37+
if (Array.isArray(value)) {
38+
return attributeType === "multiselect" ? value.join(", ") : value.join(",");
39+
}
40+
41+
if (attributeType === "date") {
42+
return format(new Date(value), "dd-MM-yyyy");
43+
}
44+
45+
if (attributeType === "multiselect") {
46+
return typeof value === "string" ? value.split(",").join(", ") : "";
47+
}
48+
49+
return String(value);
50+
}
51+
52+
function getCreatedAtSortedOrder(
53+
participants: ParticipantForSortingTest[],
54+
attributeType?: string,
55+
) {
56+
return participants
57+
.toSorted((left, right) => {
58+
return (
59+
new Date(left.createdAt).getTime() - new Date(right.createdAt).getTime()
60+
);
61+
})
62+
.map((participant) => {
63+
return getDisplayedAttributeValue(participant, attributeType);
64+
});
65+
}
66+
1367
describe("Sorting", () => {
1468
// In current implementation first 4 columns are fixed:
1569
// Select checkbox | No. | Registration date | Email
@@ -19,18 +73,19 @@ describe("Sorting", () => {
1973
cleanup();
2074
});
2175

22-
// Default sorting state cycle - 'none' -> 'asc' -> 'desc' -> 'none'
76+
// Default view starts at createdAt asc, then the column cycles asc -> desc -> none.
2377
it.each([...stringLikeDataTestCases])(
2478
"should correctly cycle through each sorting state when sorting by $attributeType type",
25-
async ({ participants, attributes }) => {
79+
async ({ participants, attributes, attributeType }) => {
80+
const typedAttributes = attributes as unknown as Attribute[];
2681
const { user, getDisplayedValuesFromColumn } = renderTable(
2782
participants,
28-
attributes,
83+
typedAttributes,
2984
);
3085

3186
const getSortHeader = () =>
3287
screen.getByRole("button", {
33-
name: attributes[0].name,
88+
name: typedAttributes[0].name,
3489
});
3590

3691
const initialOrder = getDisplayedValuesFromColumn(TESTED_COLUMN_INDEX);
@@ -44,18 +99,26 @@ describe("Sorting", () => {
4499
await user.click(getSortHeader());
45100
const finalOrder = getDisplayedValuesFromColumn(TESTED_COLUMN_INDEX);
46101

102+
expect(initialOrder).toEqual(
103+
getCreatedAtSortedOrder(participants, attributeType),
104+
);
47105
expect(descendingOrder).not.toEqual(ascendingOrder);
48-
expect(finalOrder).toEqual(initialOrder);
106+
expect(finalOrder).toEqual(
107+
participants.map((participant) => {
108+
return getDisplayedAttributeValue(participant, attributeType);
109+
}),
110+
);
49111
},
50112
);
51113

52114
it("should reset any sorting", async () => {
53-
const { participants, attributes } = textCaseData;
115+
const { participants, attributes, attributeType } = numberCaseData;
116+
const typedAttributes = attributes as unknown as Attribute[];
54117
const { user, getDisplayedValuesFromColumn, resetSortingButton } =
55-
renderTable(participants, attributes);
118+
renderTable(participants, typedAttributes);
56119

57120
const sortHeader = screen.getByRole("button", {
58-
name: attributes[0].name,
121+
name: typedAttributes[0].name,
59122
});
60123

61124
// Step 1: Capture initial order
@@ -69,21 +132,29 @@ describe("Sorting", () => {
69132
await user.click(resetSortingButton);
70133
const finalOrder = getDisplayedValuesFromColumn(TESTED_COLUMN_INDEX);
71134

135+
expect(initialOrder).toEqual(
136+
getCreatedAtSortedOrder(participants, attributeType),
137+
);
72138
expect(ascendingOrder).not.toEqual(initialOrder);
73-
expect(finalOrder).toEqual(initialOrder);
139+
expect(finalOrder).toEqual(
140+
participants.map((participant) => {
141+
return getDisplayedAttributeValue(participant, attributeType);
142+
}),
143+
);
74144
});
75145

76146
it("should properly apply multisort", async () => {
77147
const { participants, attributes } = textCaseData;
78-
const { user } = renderTable(participants, attributes);
148+
const typedAttributes = attributes as unknown as Attribute[];
149+
const { user } = renderTable(participants, typedAttributes);
79150

80151
const getTextSortHeader = () =>
81152
screen.getByRole("columnheader", {
82-
name: attributes[0].name,
153+
name: typedAttributes[0].name,
83154
});
84155
const getTextSortHeaderButton = () =>
85156
screen.getByRole("button", {
86-
name: attributes[0].name,
157+
name: typedAttributes[0].name,
87158
});
88159

89160
// eslint-disable-next-line unicorn/consistent-function-scoping

0 commit comments

Comments
 (0)