Skip to content

Commit

Permalink
ref: Convert useCommitErrors to TS and remove repositoryDeprecated (#…
Browse files Browse the repository at this point in the history
…2847)

* init conversion and remove deprecated, testing

* fix tests for useCommitErrors

* add additional spec

* add dev stuff

* update test coverage
  • Loading branch information
ajay-sentry authored and RulaKhaled committed May 13, 2024
1 parent 8a27276 commit e0b313b
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 137 deletions.
6 changes: 3 additions & 3 deletions src/pages/CommitDetailPage/CommitCoverage/CommitCoverage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,16 @@ function CommitRoutes() {
function CommitErrorBanners() {
const { owner } = useParams()
const { data: ownerData } = useOwner({ username: owner })
const { data: commitErrorData } = useCommitErrors()
const { data } = useCommitErrors()

const invalidYaml = commitErrorData?.yamlErrors?.find(
const invalidYaml = data?.yamlErrors?.find(
(err) => err?.errorCode === 'invalid_yaml'
)

return (
<>
{ownerData?.isCurrentUserPartOfOrg && (
<BotErrorBanner botErrorsCount={commitErrorData?.botErrors?.length} />
<BotErrorBanner botErrorsCount={data?.botErrors?.length} />
)}
{invalidYaml && <YamlErrorBanner />}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const mockCommitErrors = (hasErrors = false) => {
return {
owner: {
repository: {
__typename: 'Repository',
commit: {
yamlErrors: {
edges: yamlErrors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import YamlModalErrorBanner from './YamlModalErrorBanner'
const YAMLViewer = lazy(() => import('./YAMLViewer'))

function YamlModal({ showYAMLModal, setShowYAMLModal }) {
const { data: commitErrors } = useCommitErrors()
const invalidYaml = commitErrors?.yamlErrors?.find(
const { data } = useCommitErrors()

const invalidYaml = data?.yamlErrors?.find(
(err) => err?.errorCode === 'invalid_yaml'
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const wrapper = ({ children }) => (
const mockCommitNoYamlErrors = {
owner: {
repository: {
__typename: 'Repository',
commit: {
yamlErrors: {
edges: [],
Expand All @@ -40,6 +41,7 @@ const mockCommitNoYamlErrors = {
const mockCommitYamlErrors = {
owner: {
repository: {
__typename: 'Repository',
commit: {
yamlErrors: {
edges: [{ node: { errorCode: 'invalid_yaml' } }],
Expand Down
File renamed without changes.
55 changes: 0 additions & 55 deletions src/services/commitErrors/useCommitErrors.js

This file was deleted.

77 changes: 0 additions & 77 deletions src/services/commitErrors/useCommitErrors.spec.js

This file was deleted.

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

import { useCommitErrors } from './useCommitErrors'

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

const dataReturned = {
owner: {
repository: {
__typename: 'Repository',
commit: {
yamlErrors: {
edges: [{ node: { errorCode: 'invalid_yaml' } }],
},
botErrors: {
edges: [{ node: { errorCode: 'repo_bot_invalid' } }],
},
},
},
},
}

const mockNotFoundError = {
owner: {
repository: {
__typename: 'NotFoundError',
message: 'commit not found',
},
},
}

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

const mockUnsuccessfulParseError = {}

const server = setupServer()

beforeAll(() => server.listen())
beforeEach(() => {
server.resetHandlers()
queryClient.clear()
})
afterAll(() => server.close())

describe('useCommitErrors', () => {
function setup({
isNotFoundError = false,
isOwnerNotActivatedError = false,
isUnsuccessfulParseError = false,
}) {
server.use(
graphql.query(`CommitErrors`, (req, res, ctx) => {
if (isNotFoundError) {
return res(ctx.status(200), ctx.data(mockNotFoundError))
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivatedError))
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
} else {
return res(ctx.status(200), ctx.data(dataReturned))
}
})
)
}

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

it('returns commit info', async () => {
const { result } = renderHook(() => useCommitErrors(), {
wrapper,
})

await waitFor(() => result.current.isSuccess)

await waitFor(() =>
expect(result.current.data).toEqual({
botErrors: [{ errorCode: 'repo_bot_invalid' }],
yamlErrors: [{ errorCode: 'invalid_yaml' }],
})
)
})
})
describe('when called but repository errors', () => {
it('can return unsuccessful parse error', async () => {
setup({ isUnsuccessfulParseError: true })
const { result } = renderHook(() => useCommitErrors(), {
wrapper,
})

await waitFor(() => expect(result.current.isError).toBeTruthy())
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
})
)
)
})
it('can return not found error', async () => {
setup({ isNotFoundError: true })
const { result } = renderHook(() => useCommitErrors(), {
wrapper,
})

await waitFor(() => expect(result.current.isError).toBeTruthy())
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
})
)
)
})
it('can return owner not activated error', async () => {
setup({ isOwnerNotActivatedError: true })
const { result } = renderHook(() => useCommitErrors(), {
wrapper,
})

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

0 comments on commit e0b313b

Please sign in to comment.