-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from psteinroe/feat/delete-many
feat: add delete many mutation
- Loading branch information
Showing
16 changed files
with
772 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@supabase-cache-helpers/postgrest-react-query": minor | ||
"@supabase-cache-helpers/postgrest-core": minor | ||
"@supabase-cache-helpers/postgrest-swr": minor | ||
--- | ||
|
||
feat: add delete many mutation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...ages/postgrest-react-query/__tests__/mutate/use-delete-many-mutation.integration.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { createClient, SupabaseClient } from '@supabase/supabase-js'; | ||
import { QueryClient } from '@tanstack/react-query'; | ||
import { fireEvent, screen } from '@testing-library/react'; | ||
import React, { useState } from 'react'; | ||
|
||
import { useDeleteManyMutation, useQuery } from '../../src'; | ||
import type { Database } from '../database.types'; | ||
import { renderWithConfig } from '../utils'; | ||
|
||
const TEST_PREFIX = 'postgrest-react-query-delmany'; | ||
|
||
describe('useDeleteManyMutation', () => { | ||
let client: SupabaseClient<Database>; | ||
let testRunPrefix: string; | ||
|
||
let contacts: Database['public']['Tables']['contact']['Row'][]; | ||
|
||
beforeAll(async () => { | ||
testRunPrefix = `${TEST_PREFIX}-${Math.floor(Math.random() * 100)}`; | ||
client = createClient( | ||
process.env.SUPABASE_URL as string, | ||
process.env.SUPABASE_ANON_KEY as string, | ||
); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await client.from('contact').delete().ilike('username', `${TEST_PREFIX}%`); | ||
|
||
const { data } = await client | ||
.from('contact') | ||
.insert( | ||
new Array(3) | ||
.fill(0) | ||
.map((idx) => ({ username: `${testRunPrefix}-${idx}` })), | ||
) | ||
.select('*'); | ||
contacts = data as Database['public']['Tables']['contact']['Row'][]; | ||
}); | ||
|
||
it('should delete existing cache item and reduce count', async () => { | ||
const queryClient = new QueryClient(); | ||
function Page() { | ||
const [success, setSuccess] = useState<boolean>(false); | ||
const { data, count } = useQuery( | ||
client | ||
.from('contact') | ||
.select('id,username', { count: 'exact' }) | ||
.eq('username', contacts[0].username ?? ''), | ||
); | ||
const { mutateAsync: deleteContact } = useDeleteManyMutation( | ||
client.from('contact'), | ||
['id'], | ||
null, | ||
{ onSuccess: () => setSuccess(true) }, | ||
); | ||
const { mutateAsync: deleteWithEmptyOptions } = useDeleteManyMutation( | ||
client.from('contact'), | ||
['id'], | ||
null, | ||
{}, | ||
); | ||
const { mutateAsync: deleteWithoutOptions } = useDeleteManyMutation( | ||
client.from('contact'), | ||
['id'], | ||
); | ||
return ( | ||
<div> | ||
<div | ||
data-testid="delete" | ||
onClick={async () => | ||
await deleteContact([ | ||
{ | ||
id: (data ?? []).find((c) => c)?.id, | ||
}, | ||
]) | ||
} | ||
/> | ||
<div | ||
data-testid="deleteWithEmptyOptions" | ||
onClick={async () => | ||
await deleteWithEmptyOptions([ | ||
{ | ||
id: (data ?? []).find((c) => c)?.id, | ||
}, | ||
]) | ||
} | ||
/> | ||
<div | ||
data-testid="deleteWithoutOptions" | ||
onClick={async () => | ||
await deleteWithoutOptions([ | ||
{ | ||
id: (data ?? []).find((c) => c)?.id, | ||
}, | ||
]) | ||
} | ||
/> | ||
{(data ?? []).map((d) => ( | ||
<span key={d.id}>{d.username}</span> | ||
))} | ||
<span data-testid="count">{`count: ${count}`}</span> | ||
<span data-testid="success">{`success: ${success}`}</span> | ||
</div> | ||
); | ||
} | ||
|
||
renderWithConfig(<Page />, queryClient); | ||
await screen.findByText( | ||
`count: ${contacts.length}`, | ||
{}, | ||
{ timeout: 10000 }, | ||
); | ||
fireEvent.click(screen.getByTestId('deleteWithEmptyOptions')); | ||
await screen.findByText( | ||
`count: ${contacts.length - 1}`, | ||
{}, | ||
{ timeout: 10000 }, | ||
); | ||
fireEvent.click(screen.getByTestId('deleteWithoutOptions')); | ||
await screen.findByText( | ||
`count: ${contacts.length - 2}`, | ||
{}, | ||
{ timeout: 10000 }, | ||
); | ||
fireEvent.click(screen.getByTestId('delete')); | ||
await screen.findByText('success: true', {}, { timeout: 10000 }); | ||
await screen.findByText( | ||
`count: ${contacts.length - 3}`, | ||
{}, | ||
{ timeout: 10000 }, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.