-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathuseCreateOrgCodeListMutation.test.ts
60 lines (50 loc) · 2.03 KB
/
useCreateOrgCodeListMutation.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
59
60
import { renderHookWithProviders } from '../../mocks/renderHookWithProviders';
import { org } from '@studio/testing/testids';
import { queriesMock } from '../../mocks/queriesMock';
import { useCreateOrgCodeListMutation } from './useCreateOrgCodeListMutation';
import type { CodeList } from '../../types/CodeList';
import type { CodeListData } from '../../types/CodeListData';
import { createQueryClientMock } from '../../mocks/queryClientMock';
import { QueryKey } from '../../types/QueryKey';
import type { CodeListsResponse } from '../../types/api/CodeListsResponse';
// Test data:
const codeList: CodeList = [
{
value: 'test-value',
label: 'test-label',
},
];
const newCodeList: CodeListData = {
title: 'new-title',
data: codeList,
};
const existingCodeList: CodeListData = {
title: 'existing-title',
data: codeList,
};
describe('useCreateOrgCodeListMutation', () => {
beforeEach(jest.clearAllMocks);
it('Calls createCodeListForOrg with correct parameters', async () => {
const { result } = renderHookWithProviders(() => useCreateOrgCodeListMutation(org));
await result.current.mutateAsync(newCodeList);
expect(queriesMock.createCodeListForOrg).toHaveBeenCalledTimes(1);
expect(queriesMock.createCodeListForOrg).toHaveBeenCalledWith(
org,
newCodeList.title,
newCodeList.data,
);
});
it('Replaces cache with api response', async () => {
const queryClient = createQueryClientMock();
queryClient.setQueryData([QueryKey.OrgCodeLists, org], [existingCodeList]);
const createCodeListForOrg = jest.fn(() => Promise.resolve([existingCodeList, newCodeList]));
const { result } = renderHookWithProviders(() => useCreateOrgCodeListMutation(org), {
queryClient,
queries: { createCodeListForOrg },
});
await result.current.mutateAsync(newCodeList);
const expectedUpdatedData: CodeListsResponse = [existingCodeList, newCodeList];
const updatedData = queryClient.getQueryData([QueryKey.OrgCodeLists, org]);
expect(updatedData).toEqual(expectedUpdatedData);
});
});