Skip to content

Commit 45308ba

Browse files
committed
Adding basic sorting tests
1 parent da991eb commit 45308ba

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

src/hooks/useTableSort.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1-
import { useState, useEffect } from 'react';
1+
import { useEffect, useState } from 'react';
22

3-
const useSortedData = (getFieldValue, initialFieldType, initialData = []) => {
4-
const [fieldType, setFieldType] = useState(initialFieldType);
3+
const useTableSort = (initialData, initialSortValue) => {
54
const [sortedData, setSortedData] = useState([]);
5+
const [sortValue, setSortValue] = useState(initialSortValue);
6+
7+
const sortData = (value) => {
8+
const sorted = [...initialData].sort((a, b) => {
9+
if (value === 'First Name') {
10+
return a.givenName.localeCompare(b.givenName);
11+
}
12+
if (value === 'Last Name') {
13+
return a.familyName.localeCompare(b.familyName);
14+
}
15+
if (value === 'Web ID') {
16+
return a.webId.localeCompare(b.webId);
17+
}
18+
return 0;
19+
});
20+
setSortedData(sorted);
21+
};
622

723
useEffect(() => {
8-
const sortData = (field, dataToSort = []) => {
9-
if (!Array.isArray(dataToSort)) return [];
10-
return [...dataToSort].sort((a, b) => {
11-
const fieldA = getFieldValue(a, field) || '';
12-
const fieldB = getFieldValue(b, field) || '';
13-
return fieldA.localeCompare(fieldB);
14-
});
15-
};
24+
setSortedData([...initialData]);
25+
}, [initialData]);
1626

17-
setSortedData(sortData(initialData, fieldType));
18-
}, [fieldType, initialData, getFieldValue]);
27+
useEffect(() => {
28+
if (initialData.length > 0) {
29+
sortData(sortValue, initialData);
30+
}
31+
}, [sortValue, initialData]);
1932

20-
return { fieldType, setFieldType, sortedData };
33+
return { sortedData, sortValue, setSortValue };
2134
};
2235

23-
export default useSortedData;
36+
export default useTableSort;

test/components/Contacts/ContactListTableDesktop.test.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ const contacts = [
4848
givenName: 'Bob',
4949
person: 'Bob Builder',
5050
webId: 'https://example.com/Builder'
51+
},
52+
{
53+
familyName: 'Carl',
54+
givenName: 'Carson',
55+
person: 'Carl Carson',
56+
webId: 'https://example.com/Carson'
5157
}
5258
];
5359

test/components/Contacts/ContactListTableMobile.test.jsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ afterEach(() => {
1313

1414
const queryClient = new QueryClient();
1515

16+
const sortData = (data, key) =>
17+
[...data].sort((a, b) => {
18+
if (key === 'First Name') {
19+
return a.givenName.localeCompare(b.givenName);
20+
}
21+
if (key === 'Last Name') {
22+
return a.familyName.localeCompare(b.familyName);
23+
}
24+
if (key === 'Web ID') {
25+
return a.webId.localeCompare(b.webId);
26+
}
27+
return 0;
28+
});
29+
1630
const MockTableComponent = ({
1731
contacts,
1832
deleteContact = vi.fn(),
@@ -48,6 +62,12 @@ const contacts = [
4862
givenName: 'Bob',
4963
person: 'Bob Builder',
5064
webId: 'https://example.com/Builder'
65+
},
66+
{
67+
familyName: 'Carson',
68+
givenName: 'Carl',
69+
person: 'Carl Carson',
70+
webId: 'https://example.com/Carson'
5171
}
5272
];
5373

@@ -82,4 +102,31 @@ describe('contacts list table mobile tests', () => {
82102
expect(webIdElement).not.toBeNull();
83103
});
84104
});
105+
106+
it('sorts by first name correctly', () => {
107+
render(<MockTableComponent contacts={contacts} session={sessionObj} />);
108+
109+
const sortedData = sortData(contacts, 'First Name');
110+
expect(sortedData[0].givenName).toBe('Aaron');
111+
expect(sortedData[1].givenName).toBe('Bob');
112+
expect(sortedData[2].givenName).toBe('Carl');
113+
});
114+
115+
it('sorts by last name correctly', () => {
116+
render(<MockTableComponent contacts={contacts} session={sessionObj} />);
117+
118+
const sortedData = sortData(contacts, 'Last Name');
119+
expect(sortedData[0].familyName).toBe('Abby');
120+
expect(sortedData[1].familyName).toBe('Builder');
121+
expect(sortedData[2].familyName).toBe('Carson');
122+
});
123+
124+
it('sorts by web ID correctly', () => {
125+
render(<MockTableComponent contacts={contacts} session={sessionObj} />);
126+
127+
const sortedData = sortData(contacts, 'Web ID');
128+
expect(sortedData[0].webId).toBe('https://example.com/Abby');
129+
expect(sortedData[1].webId).toBe('https://example.com/Builder');
130+
expect(sortedData[2].webId).toBe('https://example.com/Carson');
131+
});
85132
});

test/pages/Contacts.test.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('Contacts Page', () => {
7373
const contacts = getByRole('grid');
7474
expect(contacts).not.toBeNull();
7575
});
76+
7677
it('displays empty list message when there are no contacts', () => {
7778
useContactsList.mockReturnValue({ data: [], refetch: vi.fn() });
7879
const { getByLabelText } = render(

0 commit comments

Comments
 (0)