Skip to content

Commit

Permalink
Added useDestroyEmailTemplate hook
Browse files Browse the repository at this point in the history
  • Loading branch information
crismali committed May 24, 2024
1 parent 9fd0290 commit 4764cbc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/network/__tests__/useDestroyEmailTemplate.test.tsx
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] })
})
})
21 changes: 21 additions & 0 deletions src/network/useDestroyEmailTemplate.ts
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] })
},
})
}

0 comments on commit 4764cbc

Please sign in to comment.