Skip to content

Commit

Permalink
ref: Use Repository instead of RepositoryDeprecated in useCommitYaml (#…
Browse files Browse the repository at this point in the history
…2844)

* Convert UseCommitYaml to Repository from RepositoryDeprecated

* Update query key
  • Loading branch information
spalmurray-codecov authored May 7, 2024
1 parent 9002190 commit e0d67da
Show file tree
Hide file tree
Showing 7 changed files with 345 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ describe('UploadsCard', () => {
graphql.query('CommitYaml', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.data({ owner: { repository: { commit: { yaml: 'yada yada' } } } })
ctx.data({
owner: {
repository: {
__typename: 'Repository',
commit: {
commitid: 'asdf',
yaml: 'yada yada',
},
},
},
})
)
})
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export const commitOneCarriedForward = graphql.query(
repository: {
__typename: 'Repository',
commit: {
branchName: null,
...commitDataError,
uploads: {
edges: [
Expand Down
35 changes: 0 additions & 35 deletions src/services/commit/useCommitYaml.js

This file was deleted.

89 changes: 0 additions & 89 deletions src/services/commit/useCommitYaml.spec.js

This file was deleted.

211 changes: 211 additions & 0 deletions src/services/commit/useCommitYaml.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { useCommitYaml } from './index'

const mockCommitYaml = (yaml: string) => ({
owner: {
repository: {
__typename: 'Repository',
commit: {
commitid: 'asdf',
yaml,
},
},
},
})

const mockCommitYamlBadSchema = {
owner: {
repository: {
asdf: 'asdf',
},
},
}

const mockCommitYamlNotFound = {
owner: {
repository: {
__typename: 'NotFoundError',
message: 'Repository not found',
},
},
}

const mockCommitYamlOwnerNotActivated = {
owner: {
repository: {
__typename: 'OwnerNotActivatedError',
message: 'Owner not activated',
},
},
}

const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<MemoryRouter initialEntries={['/gh']}>
<Route path="/:provider">
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</Route>
</MemoryRouter>
)

const server = setupServer()

console.error = () => null

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

beforeEach(() => {
jest.useRealTimers()
server.resetHandlers()
queryClient.clear()
})

afterAll(() => {
server.close()
})

interface SetupArgs {
badSchema?: boolean
notFoundError?: boolean
ownerNotActivatedError?: boolean
}

describe('useCommitYaml', () => {
const yaml =
'codecov:\n max_report_age: false\n require_ci_to_pass: true\ncomment:\n behavior: default\n layout: reach,diff,flags,tree,reach\n show_carryforward_flags: false\ncoverage:\n precision: 2\n range:\n - 70.0\n - 100.0\n round: down\n status:\n changes: false\n default_rules:\n flag_coverage_not_uploaded_behavior: include\n patch:\n default:\n target: 80.0\n project:\n library:\n paths:\n - src/path1/.*\n target: auto\n threshold: 0.1\n tests:\n paths:\n - src/path2/.*\n target: 100.0\ngithub_checks:\n annotations: true\n'

function setup({
badSchema = false,
notFoundError = false,
ownerNotActivatedError = false,
}: SetupArgs) {
server.use(
graphql.query(`CommitYaml`, (req, res, ctx) => {
if (badSchema) {
return res(ctx.status(200), ctx.data(mockCommitYamlBadSchema))
}
if (notFoundError) {
return res(ctx.status(200), ctx.data(mockCommitYamlNotFound))
}
if (ownerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockCommitYamlOwnerNotActivated))
}
return res(ctx.status(200), ctx.data(mockCommitYaml(yaml)))
})
)
}

describe('when called and user is authenticated', () => {
beforeEach(() => {
setup({})
})

it('returns commit info', async () => {
const { result } = renderHook(
() =>
useCommitYaml({
provider: 'gh',
owner: 'febg',
repo: 'repo-test',
commitid: 'a23sda3',
}),
{
wrapper,
}
)

await waitFor(() => result.current.isLoading)
await waitFor(() => !result.current.isLoading)

await waitFor(() => expect(result.current.data).toEqual(yaml))
})
})

describe('when bad response', () => {
it('returns 404 failed to parse', async () => {
setup({ badSchema: true })
const { result } = renderHook(
() =>
useCommitYaml({
provider: 'gh',
owner: 'febg',
repo: 'repo-test',
commitid: 'a23sda3',
}),
{
wrapper,
}
)

await waitFor(() => expect(result.current.isError).toBeTruthy())
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
})
)
})
})

describe('when repository not found error', () => {
it('returns 404 not found', async () => {
setup({ notFoundError: true })
const { result } = renderHook(
() =>
useCommitYaml({
provider: 'gh',
owner: 'febg',
repo: 'repo-test',
commitid: 'a23sda3',
}),
{
wrapper,
}
)

await waitFor(() => expect(result.current.isError).toBeTruthy())
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
})
)
})
})

describe('when owner not activated error', () => {
it('returns 403 owner not activated', async () => {
setup({ ownerNotActivatedError: true })
const { result } = renderHook(
() =>
useCommitYaml({
provider: 'gh',
owner: 'febg',
repo: 'repo-test',
commitid: 'a23sda3',
}),
{
wrapper,
}
)

await waitFor(() => expect(result.current.isError).toBeTruthy())
expect(result.current.error).toEqual(
expect.objectContaining({
status: 403,
})
)
})
})
})
Loading

0 comments on commit e0d67da

Please sign in to comment.