-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 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,70 @@ | ||
import React from 'react' | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { AuthProvider } from 'src/utils/AuthContext' | ||
import { asMock, userIsSignedIn } from 'src/testHelpers' | ||
import { AuthedFetch, useAuthedFetch } from '../useAuthedFetch' | ||
import { useDestroyEmailTemplate } from '../useDestroyEmailTemplate' | ||
import { QUERY_KEY } from '../useEmailTemplates' | ||
import { randomUUID } from 'crypto' | ||
|
||
jest.mock('../useAuthedFetch') | ||
|
||
describe('useDestroyEmailTemplate', () => { | ||
let mockAuthedFetch: AuthedFetch | ||
|
||
beforeEach(() => { | ||
userIsSignedIn() | ||
mockAuthedFetch = jest.fn() | ||
asMock(useAuthedFetch).mockReturnValue(mockAuthedFetch) | ||
}) | ||
|
||
it('destroys an email template', async () => { | ||
const client = new QueryClient() | ||
const id = randomUUID() | ||
asMock(mockAuthedFetch).mockResolvedValue({ | ||
statusCode: 200, | ||
json: { emailTemplate: { id } }, | ||
}) | ||
|
||
const { result } = renderHook(() => useDestroyEmailTemplate(), { | ||
wrapper: ({ children }) => { | ||
return ( | ||
<QueryClientProvider client={client}> | ||
<AuthProvider>{children}</AuthProvider> | ||
</QueryClientProvider> | ||
) | ||
}, | ||
}) | ||
expect(mockAuthedFetch).not.toHaveBeenCalled() | ||
await result.current.mutateAsync(id) | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)) | ||
expect(mockAuthedFetch).toHaveBeenCalledWith({ | ||
path: `/email-templates/${id}`, | ||
method: 'DELETE', | ||
}) | ||
expect(result.current.data).toEqual({ emailTemplate: { id } }) | ||
}) | ||
|
||
it('invalidates the useEmailTemplates query', async () => { | ||
const client = new QueryClient() | ||
const id = randomUUID() | ||
asMock(mockAuthedFetch).mockResolvedValue({ statusCode: 200, json: { emailTemplate: { id } } }) | ||
|
||
jest.spyOn(client, 'invalidateQueries') | ||
|
||
const { result } = renderHook(() => useDestroyEmailTemplate(), { | ||
wrapper: ({ children }) => { | ||
return ( | ||
<QueryClientProvider client={client}> | ||
<AuthProvider>{children}</AuthProvider> | ||
</QueryClientProvider> | ||
) | ||
}, | ||
}) | ||
expect(client.invalidateQueries).not.toHaveBeenCalled() | ||
await result.current.mutateAsync(id) | ||
await waitFor(() => expect(result.current.isSuccess).toEqual(true)) | ||
expect(client.invalidateQueries).toHaveBeenCalledWith({ queryKey: [QUERY_KEY] }) | ||
}) | ||
}) |
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,21 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import { useAuthedFetch } from './useAuthedFetch' | ||
import { QUERY_KEY as USE_EMAIL_TEMPLATES_QUERY_KEY } from './useEmailTemplates' | ||
|
||
export const useDestroyEmailTemplate = () => { | ||
const client = useQueryClient() | ||
const authedFetch = useAuthedFetch() | ||
|
||
return useMutation({ | ||
mutationFn: async (id: string) => { | ||
const result = await authedFetch({ | ||
path: `/email-templates/${id}`, | ||
method: 'DELETE', | ||
}) | ||
return result.json | ||
}, | ||
onSuccess: () => { | ||
client.invalidateQueries({ queryKey: [USE_EMAIL_TEMPLATES_QUERY_KEY] }) | ||
}, | ||
}) | ||
} |