Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create hook to fetch bundle analysis summary for a pull #2494

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/services/commit/useCommitBADropdownSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ query CommitBADropdownSummary(
}
}`

interface UseCommitDropdownSummaryArgs {
interface UseCommitBADropdownSummaryArgs {
provider: string
owner: string
repo: string
Expand All @@ -116,9 +116,9 @@ export function useCommitBADropdownSummary({
owner,
repo,
commitid,
}: UseCommitDropdownSummaryArgs) {
}: UseCommitBADropdownSummaryArgs) {
return useQuery({
queryKey: ['CommitSummary', provider, owner, repo, commitid],
queryKey: ['CommitBADropdownSummary', provider, owner, repo, commitid],
queryFn: ({ signal }) =>
Api.graphql({
provider,
Expand Down
265 changes: 265 additions & 0 deletions src/services/pull/usePullBADropdownSummary.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'

import { usePullBADropdownSummary } from './usePullBADropdownSummary'

const mockPullBASummaryData = {
owner: {
repository: {
__typename: 'Repository',
pull: {
bundleAnalysisCompareWithBase: {
__typename: 'BundleAnalysisComparison',
sizeDelta: 1,
loadTimeDelta: 2,
},
},
},
},
}

const mockNullOwner = {
owner: null,
}

const mockUnsuccessfulParseError = {}

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

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

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

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

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

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

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

interface SetupArgs {
isUnsuccessfulParseError?: boolean
isNullOwner?: boolean
isOwnerNotActivatedError?: boolean
isNotFoundError?: boolean
}

describe('usePullBADropdownSummary', () => {
function setup({
isUnsuccessfulParseError = false,
isNullOwner = false,
isNotFoundError = false,
isOwnerNotActivatedError = false,
}: SetupArgs = {}) {
server.use(
graphql.query('PullBADropdownSummary', (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 if (isNullOwner) {
return res(ctx.status(200), ctx.data(mockNullOwner))
} else {
return res(ctx.status(200), ctx.data(mockPullBASummaryData))
}
})
)
}

describe('api returns valid response', () => {
it('returns pull summary data', async () => {
setup()
const { result } = renderHook(
() =>
usePullBADropdownSummary({
provider: 'github',
owner: 'codecov',
repo: 'test-repo',
pullId: 123,
}),
{ wrapper }
)

const expectedResult = {
owner: {
repository: {
__typename: 'Repository',
pull: {
bundleAnalysisCompareWithBase: {
__typename: 'BundleAnalysisComparison',
sizeDelta: 1,
loadTimeDelta: 2,
},
},
},
},
}

await waitFor(() =>
expect(result.current.data).toStrictEqual(expectedResult)
)
})
})

describe('there is a null owner', () => {
it('returns a null value', async () => {
setup({ isNullOwner: true })
const { result } = renderHook(
() =>
usePullBADropdownSummary({
provider: 'github',
owner: 'codecov',
repo: 'test-repo',
pullId: 123,
}),
{ wrapper }
)

await waitFor(() =>
expect(result.current.data).toStrictEqual({ owner: null })
)
})
})

describe('unsuccessful parse of zod schema', () => {
let oldConsoleError = console.error

beforeEach(() => {
console.error = () => null
})

afterEach(() => {
console.error = oldConsoleError
})

it('throws a 404', async () => {
setup({ isUnsuccessfulParseError: true })
const { result } = renderHook(
() =>
usePullBADropdownSummary({
provider: 'github',
owner: 'codecov',
repo: 'test-repo',
pullId: 123,
}),
{ wrapper }
)

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

describe('returns NotFoundError __typename', () => {
let oldConsoleError = console.error

beforeEach(() => {
console.error = () => null
})

afterEach(() => {
console.error = oldConsoleError
})

it('throws a 404', async () => {
setup({ isNotFoundError: true })
const { result } = renderHook(
() =>
usePullBADropdownSummary({
provider: 'github',
owner: 'codecov',
repo: 'test-repo',
pullId: 123,
}),
{ wrapper }
)

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

describe('returns OwnerNotActivatedError __typename', () => {
let oldConsoleError = console.error

beforeEach(() => {
console.error = () => null
})

afterEach(() => {
console.error = oldConsoleError
})

it('throws a 403', async () => {
setup({ isOwnerNotActivatedError: true })
const { result } = renderHook(
() =>
usePullBADropdownSummary({
provider: 'github',
owner: 'codecov',
repo: 'test-repo',
pullId: 123,
}),
{ wrapper }
)

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