Skip to content

Commit

Permalink
update tests and move repoBackfilled off repodepre
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-sentry committed May 8, 2024
1 parent ddb20fb commit 1d857db
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const mockBackfillData = {
},
owner: {
repository: {
__typename: 'Repository',
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
flagsCount: 4,
Expand Down Expand Up @@ -313,6 +314,9 @@ describe('FileExplorer', () => {
}),
graphql.query('GetRepoSettingsTeam', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(mockRepoSettings))
}),
graphql.query('GetRepoOverview', (req, res, ctx) => {
return res(ctx.status(200), ctx.data({}))
})
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const mockBackfillHasFlagsAndActive = {
},
owner: {
repository: {
__typename: 'Repository',
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
flagsCount: 4,
Expand All @@ -88,6 +89,7 @@ const mockBackfillTimeScaleDisabled = {
},
owner: {
repository: {
__typename: 'Repository',
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
flagsCount: 4,
Expand All @@ -101,6 +103,7 @@ const mockBackfillNoFlagsPresent = {
},
owner: {
repository: {
__typename: 'Repository',
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
flagsCount: 0,
Expand All @@ -114,6 +117,7 @@ const mockBackfillFlagMeasureNotActive = {
},
owner: {
repository: {
__typename: 'Repository',
flagsMeasurementsActive: false,
flagsMeasurementsBackfilled: true,
flagsCount: 4,
Expand Down
127 changes: 104 additions & 23 deletions src/services/repo/hooks.spec.js → src/services/repo/hooks.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql, rest } from 'msw'
import { setupServer } from 'msw/node'
import React from 'react'
import { MemoryRouter, Route } from 'react-router-dom'

import {
Expand All @@ -16,7 +17,7 @@ const queryClient = new QueryClient({
})

const wrapper =
(initialEntries = '/gh/codecov/test') =>
(initialEntries = '/gh/codecov/test'): React.FC<React.PropsWithChildren> =>
({ children }) =>
(
<MemoryRouter initialEntries={[initialEntries]}>
Expand All @@ -34,12 +35,14 @@ beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

console.error = () => {}

const provider = 'gh'
const owner = 'RulaKhaled'
const repo = 'test'
const owner = 'cool-guy'
const repo = 'cool-repo'

describe('useRepo', () => {
function setup(apiData) {
function setup(apiData: any) {
server.use(
graphql.query('GetRepo', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(apiData))
Expand Down Expand Up @@ -137,7 +140,7 @@ describe('useEraseRepoContent', () => {
rest.patch(
`internal/github/codecov/repos/test/erase/`,
(req, res, ctx) => {
return res(ctx.status(200), ctx.json())
return res(ctx.status(200), ctx.json({}))
}
)
)
Expand All @@ -154,7 +157,7 @@ describe('useEraseRepoContent', () => {
wrapper: wrapper(),
})

result.current.mutate(null)
result.current.mutate()

await waitFor(() => expect(result.current.isSuccess).toBeTruthy())
})
Expand Down Expand Up @@ -212,6 +215,7 @@ describe('useUpdateRepo', () => {
wrapper: wrapper(),
})

// @ts-expect-error
result.current.mutate({})

await waitFor(() => expect(result.current.isSuccess).toBeTruthy())
Expand All @@ -224,40 +228,69 @@ describe('useRepoBackfilled', () => {
const dataReturned = {
owner: {
repository: {
__typename: 'Repository',
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
},
},
}

function setup() {
const mockUnsuccessfulParseError = {}

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

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

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

function setup({
isNotFoundError = false,
isOwnerNotActivatedError = false,
isUnsuccessfulParseError = false,
}: SetupArgs) {
server.use(
graphql.query('BackfillFlagMemberships', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(dataReturned))
if (isNotFoundError) {
return res(ctx.status(200), ctx.data(mockRepoNotFound))
} else if (isOwnerNotActivatedError) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivated))
} else if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
} else {
return res(ctx.status(200), ctx.data(dataReturned))
}
})
)
}

describe('when called', () => {
beforeEach(() => {
setup()
})

describe('when data is loaded', () => {
it('returns the data', async () => {
const { result } = renderHook(
() =>
useRepoBackfilled({
provider: 'gh',
owner: 'owner',
repo: 'another-test',
}),
{
wrapper: wrapper(),
}
)
setup({})
const { result } = renderHook(() => useRepoBackfilled(), {
wrapper: wrapper(),
})

const expectedResponse = {
__typename: 'Repository',
flagsMeasurementsActive: true,
flagsMeasurementsBackfilled: true,
}
Expand All @@ -266,5 +299,53 @@ describe('useRepoBackfilled', () => {
)
})
})

describe('can throw errors', () => {
it('can return unsuccessful parse error', async () => {
setup({ isUnsuccessfulParseError: true })
const { result } = renderHook(() => useRepoBackfilled(), {
wrapper: 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(() => useRepoBackfilled(), {
wrapper: 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(() => useRepoBackfilled(), {
wrapper: wrapper(),
})

await waitFor(() => expect(result.current.isError).toBeTruthy())
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 403,
})
)
)
})
})
})
})
File renamed without changes.
73 changes: 0 additions & 73 deletions src/services/repo/useRepoBackfilled.ts

This file was deleted.

0 comments on commit 1d857db

Please sign in to comment.