|
1 | 1 | import * as React from 'react'; |
2 | | -import { waitFor, render } from '@testing-library/react'; |
| 2 | +import { waitFor, render, screen } from '@testing-library/react'; |
3 | 3 | import expect from 'expect'; |
4 | 4 |
|
5 | 5 | import { CoreAdminContext } from '../core'; |
6 | 6 | import { testDataProvider } from './testDataProvider'; |
7 | 7 | import { useDeleteMany } from './useDeleteMany'; |
8 | | -import { QueryClient } from '@tanstack/react-query'; |
| 8 | +import { QueryClient, useMutationState } from '@tanstack/react-query'; |
9 | 9 |
|
10 | 10 | describe('useDeleteMany', () => { |
11 | 11 | it('returns a callback that can be used with update arguments', async () => { |
@@ -80,6 +80,64 @@ describe('useDeleteMany', () => { |
80 | 80 | }); |
81 | 81 | }); |
82 | 82 |
|
| 83 | + it('accepts a meta parameter', async () => { |
| 84 | + const dataProvider = testDataProvider({ |
| 85 | + deleteMany: jest.fn(() => Promise.resolve({ data: [1, 2] } as any)), |
| 86 | + }); |
| 87 | + let localDeleteMany; |
| 88 | + const Dummy = () => { |
| 89 | + const [deleteMany] = useDeleteMany(); |
| 90 | + localDeleteMany = deleteMany; |
| 91 | + return <span />; |
| 92 | + }; |
| 93 | + |
| 94 | + render( |
| 95 | + <CoreAdminContext dataProvider={dataProvider}> |
| 96 | + <Dummy /> |
| 97 | + </CoreAdminContext> |
| 98 | + ); |
| 99 | + localDeleteMany('foo', { ids: [1, 2], meta: { hello: 'world' } }); |
| 100 | + await waitFor(() => { |
| 101 | + expect(dataProvider.deleteMany).toHaveBeenCalledWith('foo', { |
| 102 | + ids: [1, 2], |
| 103 | + meta: { hello: 'world' }, |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | + it('sets the mutationKey', async () => { |
| 108 | + const dataProvider = testDataProvider({ |
| 109 | + deleteMany: jest.fn(() => Promise.resolve({ data: [1, 2] } as any)), |
| 110 | + }); |
| 111 | + let localDeleteMany; |
| 112 | + const Dummy = () => { |
| 113 | + const [deleteMany] = useDeleteMany('foo'); |
| 114 | + localDeleteMany = deleteMany; |
| 115 | + return <span />; |
| 116 | + }; |
| 117 | + const Observe = () => { |
| 118 | + const mutation = useMutationState({ |
| 119 | + filters: { |
| 120 | + mutationKey: ['foo', 'deleteMany'], |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + return <span>mutations: {mutation.length}</span>; |
| 125 | + }; |
| 126 | + |
| 127 | + render( |
| 128 | + <CoreAdminContext dataProvider={dataProvider}> |
| 129 | + <Dummy /> |
| 130 | + <Observe /> |
| 131 | + </CoreAdminContext> |
| 132 | + ); |
| 133 | + localDeleteMany('foo', { ids: [1, 2] }); |
| 134 | + await waitFor(() => { |
| 135 | + expect(dataProvider.deleteMany).toHaveBeenCalledWith('foo', { |
| 136 | + ids: [1, 2], |
| 137 | + }); |
| 138 | + }); |
| 139 | + await screen.findByText('mutations: 1'); |
| 140 | + }); |
83 | 141 | it('accepts a meta parameter', async () => { |
84 | 142 | const dataProvider = testDataProvider({ |
85 | 143 | deleteMany: jest.fn(() => Promise.resolve({ data: [1, 2] } as any)), |
|
0 commit comments