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