1 import { act, renderHook } from '@testing-library/react-hooks';
3 import { SORT_DIRECTION } from '@proton/shared/lib/constants';
5 import useSortedList from './useSortedList';
7 describe('useSortedList hook', () => {
8 it('should return sorted list initially if config is provided', () => {
9 const list = [{ t: 2 }, { t: 3 }, { t: 1 }];
10 const { result } = renderHook(() => useSortedList(list, { key: 't', direction: SORT_DIRECTION.DESC }));
11 expect(result.current.sortedList).toEqual([{ t: 3 }, { t: 2 }, { t: 1 }]);
12 expect(result.current.sortConfig).toEqual({ key: 't', direction: SORT_DIRECTION.DESC });
15 it('should not sort initially if no config is provided', () => {
16 const list = [{ t: 2 }, { t: 3 }, { t: 1 }];
17 const { result } = renderHook(() => useSortedList(list));
18 expect(result.current.sortedList).toEqual(list);
19 expect(result.current.sortConfig).toBeUndefined();
22 it('should set initialize sorting config on sort when none was provided', () => {
23 const list = [{ t: 2 }, { t: 3 }, { t: 1 }];
24 const { result } = renderHook(() => useSortedList(list));
25 act(() => result.current.toggleSort('t'));
26 expect(result.current.sortedList).toEqual([{ t: 1 }, { t: 2 }, { t: 3 }]);
27 expect(result.current.sortConfig).toEqual({ key: 't', direction: SORT_DIRECTION.ASC });
30 it('should toggle sort direction for the same key', () => {
31 const list = [{ t: 2 }, { t: 3 }, { t: 1 }];
32 const { result } = renderHook(() => useSortedList(list, { key: 't', direction: SORT_DIRECTION.ASC }));
33 expect(result.current.sortedList).toEqual([{ t: 1 }, { t: 2 }, { t: 3 }]);
34 expect(result.current.sortConfig).toEqual({ key: 't', direction: SORT_DIRECTION.ASC });
36 act(() => result.current.toggleSort('t'));
38 expect(result.current.sortedList).toEqual([{ t: 3 }, { t: 2 }, { t: 1 }]);
39 expect(result.current.sortConfig).toEqual({ key: 't', direction: SORT_DIRECTION.DESC });
42 it('should change sort key and set direction to ascending for another key', () => {
48 const { result } = renderHook(() => useSortedList(list, { key: 't', direction: SORT_DIRECTION.ASC }));
49 expect(result.current.sortedList).toEqual([
54 expect(result.current.sortConfig).toEqual({ key: 't', direction: SORT_DIRECTION.ASC });
56 act(() => result.current.toggleSort('k'));
58 expect(result.current.sortedList).toEqual([
63 expect(result.current.sortConfig).toEqual({ key: 'k', direction: SORT_DIRECTION.ASC });