-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathuseUpdateOrgCodeListMutation.test.ts
58 lines (50 loc) · 2.02 KB
/
useUpdateOrgCodeListMutation.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { renderHookWithProviders } from '../../mocks/renderHookWithProviders';
import { org } from '@studio/testing/testids';
import { queriesMock } from '../../mocks/queriesMock';
import type { CodeList } from '../../types/CodeList';
import { useUpdateOrgCodeListMutation } from './useUpdateOrgCodeListMutation';
import { createQueryClientMock } from '../../mocks/queryClientMock';
import { QueryKey } from '../../types/QueryKey';
import type { CodeListsResponse } from '../../types/api/CodeListsResponse';
import type { CodeListData } from '../../types/CodeListData';
// Test data:
const codeList: CodeList = [
{
value: 'test-value',
label: 'test-label',
},
];
const oldCodeList: CodeListData = {
title: 'old-title',
data: codeList,
};
const updatedCodeList: CodeListData = {
title: 'updated-title',
data: codeList,
};
describe('useUpdateOrgCodeListMutation', () => {
beforeEach(jest.clearAllMocks);
it('Calls updateCodeListForOrg with correct parameters', async () => {
const { result } = renderHookWithProviders(() => useUpdateOrgCodeListMutation(org));
await result.current.mutateAsync({ ...updatedCodeList });
expect(queriesMock.updateCodeListForOrg).toHaveBeenCalledTimes(1);
expect(queriesMock.updateCodeListForOrg).toHaveBeenCalledWith(
org,
updatedCodeList.title,
updatedCodeList.data,
);
});
it('Replaces cache with api response', async () => {
const queryClient = createQueryClientMock();
queryClient.setQueryData([QueryKey.OrgCodeLists, org], [oldCodeList]);
const updateCodeListForOrg = jest.fn(() => Promise.resolve([updatedCodeList]));
const { result } = renderHookWithProviders(() => useUpdateOrgCodeListMutation(org), {
queryClient,
queries: { updateCodeListForOrg },
});
await result.current.mutateAsync({ ...updatedCodeList });
const expectedUpdatedData: CodeListsResponse = [updatedCodeList];
const updatedData = queryClient.getQueryData([QueryKey.OrgCodeLists, org]);
expect(updatedData).toEqual(expectedUpdatedData);
});
});