Skip to content

Commit

Permalink
migrate hook to the query options api
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov committed Dec 3, 2024
1 parent a77d790 commit 19c7450
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 47 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClientProvider as QueryClientProviderV5,
QueryClient as QueryClientV5,
useQuery as useQueryV5,
} from '@tanstack/react-queryV5'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'

import { useCommitHeaderDataTeam } from './useCommitHeaderDataTeam'
import { CommitHeaderDataTeamQueryOpts } from './CommitHeaderDataTeamQueryOpts'

const mockRepository = {
owner: {
Expand Down Expand Up @@ -54,21 +58,23 @@ const mockNullOwner = {

const mockUnsuccessfulParseError = {}

const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, useErrorBoundary: false } },
})
const server = setupServer()
const queryClientV5 = new QueryClientV5({
defaultOptions: { queries: { retry: false } },
})

const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
<QueryClientProviderV5 client={queryClientV5}>
{children}
</QueryClientProviderV5>
)

beforeAll(() => {
server.listen()
})

afterEach(() => {
queryClient.clear()
queryClientV5.clear()
server.resetHandlers()
})

Expand All @@ -83,7 +89,7 @@ interface SetupArgs {
isNullOwner?: boolean
}

describe('useCommitHeaderDataTeam', () => {
describe('CommitHeaderDataTeamQueryOpts', () => {
function setup({
isNotFoundError = false,
isOwnerNotActivatedError = false,
Expand Down Expand Up @@ -115,12 +121,14 @@ describe('useCommitHeaderDataTeam', () => {

const { result } = renderHook(
() =>
useCommitHeaderDataTeam({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
}),
useQueryV5(
CommitHeaderDataTeamQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
})
),
{ wrapper }
)

Expand Down Expand Up @@ -158,12 +166,14 @@ describe('useCommitHeaderDataTeam', () => {

const { result } = renderHook(
() =>
useCommitHeaderDataTeam({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
}),
useQueryV5(
CommitHeaderDataTeamQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
})
),
{ wrapper }
)

Expand Down Expand Up @@ -197,12 +207,14 @@ describe('useCommitHeaderDataTeam', () => {

const { result } = renderHook(
() =>
useCommitHeaderDataTeam({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
}),
useQueryV5(
CommitHeaderDataTeamQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
})
),
{ wrapper }
)

Expand Down Expand Up @@ -233,12 +245,14 @@ describe('useCommitHeaderDataTeam', () => {

const { result } = renderHook(
() =>
useCommitHeaderDataTeam({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
}),
useQueryV5(
CommitHeaderDataTeamQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
})
),
{ wrapper }
)

Expand Down Expand Up @@ -269,12 +283,14 @@ describe('useCommitHeaderDataTeam', () => {

const { result } = renderHook(
() =>
useCommitHeaderDataTeam({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
}),
useQueryV5(
CommitHeaderDataTeamQueryOpts({
provider: 'gh',
owner: 'codecov',
repo: 'test-repo',
commitId: 'id-1',
})
),
{ wrapper }
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from '@tanstack/react-query'
import { queryOptions as queryOptionsV5 } from '@tanstack/react-queryV5'
import { z } from 'zod'

import {
Expand All @@ -14,6 +14,7 @@ import {
RepoOwnerNotActivatedErrorSchema,
} from 'services/repo'
import Api from 'shared/api'
import { rejectNetworkError } from 'shared/api/helpers'
import A from 'ui/A'

const CoverageObjSchema = z.object({
Expand Down Expand Up @@ -130,20 +131,20 @@ const query = `
}
`

interface UseCommitHeaderDataTeamArgs {
interface CommitHeaderDataTeamQueryArgs {
provider: string
owner: string
repo: string
commitId: string
}

export const useCommitHeaderDataTeam = ({
export const CommitHeaderDataTeamQueryOpts = ({
provider,
owner,
repo,
commitId,
}: UseCommitHeaderDataTeamArgs) =>
useQuery({
}: CommitHeaderDataTeamQueryArgs) =>
queryOptionsV5({
queryKey: [
'CommitPageHeaderDataTeam',
provider,
Expand All @@ -167,23 +168,26 @@ export const useCommitHeaderDataTeam = ({
const parsedData = CommitHeaderDataTeamSchema.safeParse(res?.data)

if (!parsedData.success) {
return Promise.reject({
return rejectNetworkError({
status: 404,
data: {},
dev: 'CommitHeaderDataTeamQueryOpts - 404 Failed to parse schema',
error: parsedData.error,
})
}

const data = parsedData.data

if (data?.owner?.repository?.__typename === 'NotFoundError') {
return Promise.reject({
return rejectNetworkError({
status: 404,
data: {},
dev: 'CommitHeaderDataTeamQueryOpts - 404 Not Found',
})
}

if (data?.owner?.repository?.__typename === 'OwnerNotActivatedError') {
return Promise.reject({
return rejectNetworkError({
status: 403,
data: {
detail: (
Expand All @@ -195,6 +199,7 @@ export const useCommitHeaderDataTeam = ({
</p>
),
},
dev: 'CommitHeaderDataTeamQueryOpts - 403 Owner Not Activated',
})
}

Expand Down

0 comments on commit 19c7450

Please sign in to comment.