Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suejung-sentry committed Jan 2, 2025
1 parent 9030cf0 commit 8fb2df0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('OrgUploadToken', () => {
const user = userEvent.setup()
const mutate = vi.fn()
const addNotification = vi.fn()
vi.mocked(useFlags).mockReturnValue({ tokenlessSection: true })

vi.mocked(useFlags).mockReturnValue({ tokenlessSection: true })
vi.mocked(useAddNotification).mockReturnValue(addNotification)

server.use(
Expand All @@ -92,8 +92,8 @@ describe('OrgUploadToken', () => {
},
})
}),
graphql.mutation('regenerateOrgUploadToken', () => {
mutate('regenerateOrgUploadToken')
graphql.mutation('RegenerateOrgUploadToken', () => {
mutate('RegenerateOrgUploadToken')
return HttpResponse.json({
data: {
regenerateOrgUploadToken: {
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('OrgUploadToken', () => {

describe('when user clicks on Generate button', () => {
it('calls the mutation', async () => {
const { mutate, user } = setup({ orgUploadToken: 'token' })
const { mutate, user } = setup({ orgUploadToken: '' })

render(<OrgUploadToken />, { wrapper })

Expand All @@ -197,7 +197,7 @@ describe('OrgUploadToken', () => {
await waitFor(() => expect(mutate).toHaveBeenCalled())
})

it.only('adds an error notification', async () => {
it('adds an error notification', async () => {
const { addNotification, user } = setup({
orgUploadToken: '',
error: 'UnauthenticatedError',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function RegenerateOrgUploadToken({
orgUploadToken: string
}) {
const { owner } = useParams<URLParams>()
const { regenerateToken, isLoading } = useGenerateOrgUploadToken()
const { regenerateTokenAsync, isLoading } = useGenerateOrgUploadToken()
const [showModal, setShowModal] = useState(false)
const isAdmin = useIsCurrentUserAnAdmin({ owner })

Expand Down Expand Up @@ -87,7 +87,7 @@ function RegenerateOrgUploadToken({
{showModal && (
<RegenerateTokenModal
closeModal={() => setShowModal(false)}
regenerateToken={() => Promise.resolve(regenerateToken())}
regenerateToken={regenerateTokenAsync}
isLoading={isLoading}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export default function useGenerateOrgUploadToken() {
const addToast = useAddNotification()
const {
mutate,
mutateAsync,
data: res,
...rest
} = useRegenerateOrgUploadToken({
onSuccess: () => {
console.log("MY DEBUG 2", res)
const errString = res?.error?.__typename
onSuccess: (result) => {
const errString = result?.error?.__typename

if (errString) {
addToast({
Expand All @@ -26,5 +26,10 @@ export default function useGenerateOrgUploadToken() {
},
})

return { regenerateToken: mutate, res, ...rest }
return {
regenerateToken: mutate,
regenerateTokenAsync: mutateAsync,
res,
...rest,
}
}
21 changes: 6 additions & 15 deletions src/services/orgUploadToken/useRegenerateOrgUploadToken.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { useRegenerateOrgUploadToken } from './useRegenerateOrgUploadToken'
import { rejectNetworkError } from 'shared/api/helpers'
import * as apiHelpers from 'shared/api/helpers'

vi.mock('shared/api/helpers', () => ({
rejectNetworkError: vi.fn(),
}))
import { useRegenerateOrgUploadToken } from './useRegenerateOrgUploadToken'

vi.mock(import('shared/api/helpers'), async (importOriginal) => {
const actual = await importOriginal()
return {
...actual,
rejectNetworkError: vi.fn(),
}
})
vi.mock('shared/api/helpers')

const mocks = {
rejectNetworkError: vi.mocked(rejectNetworkError),
rejectNetworkError: vi.spyOn(apiHelpers, 'rejectNetworkError'),
}

const mockData = {
Expand Down Expand Up @@ -59,7 +50,7 @@ describe('useRegenerateOrgUploadToken', () => {

function setup({ data = mockData }: { data?: SetupArgs } = {}) {
server.use(
graphql.mutation('regenerateOrgUploadToken', () => {
graphql.mutation('RegenerateOrgUploadToken', () => {
return HttpResponse.json({ data })
})
)
Expand Down
14 changes: 6 additions & 8 deletions src/services/orgUploadToken/useRegenerateOrgUploadToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Api from 'shared/api'
import { Provider, rejectNetworkError } from 'shared/api/helpers'

const query = `
mutation regenerateOrgUploadToken(
mutation RegenerateOrgUploadToken(
$input: RegenerateOrgUploadTokenInput!
) {
regenerateOrgUploadToken(input: $input) {
Expand All @@ -33,7 +33,6 @@ const ResponseSchema = z.object({
__typename: z.literal('UnauthenticatedError'),
}),
])
.optional()
.nullish(),
orgUploadToken: z.string().nullish(),
})
Expand All @@ -45,10 +44,10 @@ interface URLParams {
owner: string
}

type RegenerateOrgUploadTokenData = z.infer<typeof ResponseSchema>

interface UseRegenerateOrgUploadTokenProps {
onSuccess?: (data: RegenerateOrgUploadTokenData) => void
onSuccess?: (
data: z.infer<typeof ResponseSchema>['regenerateOrgUploadToken']
) => void
}

export function useRegenerateOrgUploadToken({
Expand All @@ -63,7 +62,7 @@ export function useRegenerateOrgUploadToken({
provider,
query,
variables: { input: { owner } },
mutationPath: 'regenerateOrgUploadToken',
mutationPath: 'RegenerateOrgUploadToken',
})
},
useErrorBoundary: true,
Expand All @@ -78,8 +77,7 @@ export function useRegenerateOrgUploadToken({
})
}

console.log("MY DEBUG 3", parsedRes.data)
onSuccess(parsedRes.data)
onSuccess(parsedRes.data.regenerateOrgUploadToken)
},
onSettled: () => {
queryClient.invalidateQueries(['DetailOwner'])
Expand Down

0 comments on commit 8fb2df0

Please sign in to comment.