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

ref: Move useRepoConfig off RepositoryDeprecated #2860

Merged
merged 3 commits into from
May 14, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ComponentsTable from './ComponentsTable'
const mockRepoConfig = {
owner: {
repository: {
__typename: 'Repository',
repositoryConfig: {
indicationRange: { upperRange: 80, lowerRange: 60 },
},
Expand Down
15 changes: 7 additions & 8 deletions src/pages/RepoPage/CoverageTab/Summary/Summary.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ const mockRepoCoverage = {
}

const mockRepoConfig = {
repositoryConfig: {
indicationRange: {
upperRange: 80,
lowerRange: 60,
owner: {
repository: {
__typename: 'Repository',
repositoryConfig: {
indicationRange: { upperRange: 80, lowerRange: 60 },
},
},
},
}
Expand Down Expand Up @@ -210,10 +212,7 @@ describe('Summary', () => {
)
),
graphql.query('RepoConfig', (req, res, ctx) =>
res(
ctx.status(200),
ctx.data({ owner: { repository: mockRepoConfig } })
)
res(ctx.status(200), ctx.data(mockRepoConfig))
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ const overviewMock = {
},
}
const repoConfigMock = {
indicationRange: {
lowerRange: 60,
upperRange: 80,
owner: {
repository: {
__typename: 'Repository',
repositoryConfig: {
indicationRange: { upperRange: 80, lowerRange: 60 },
},
},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import FlagsTable, { FlagTable } from './FlagsTable'
const mockRepoConfig = {
owner: {
repository: {
__typename: 'Repository',
repositoryConfig: {
indicationRange: { upperRange: 80, lowerRange: 60 },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RepoNotFoundErrorSchema,
RepoOwnerNotActivatedErrorSchema,
} from 'services/repo'
import { RepoConfig } from 'services/repo/useRepoConfig'
import { RepositoryConfigSchema } from 'services/repo/useRepoConfig'
import Api from 'shared/api'
import { NetworkErrorObject } from 'shared/api/helpers'
import A from 'ui/A'
Expand Down Expand Up @@ -68,7 +68,7 @@ const PathContentsUnionSchema = z.discriminatedUnion('__typename', [

const RepositorySchema = z.object({
__typename: z.literal('Repository'),
repositoryConfig: RepoConfig,
repositoryConfig: RepositoryConfigSchema,
branch: z.object({
head: z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
RepoNotFoundErrorSchema,
RepoOwnerNotActivatedErrorSchema,
} from 'services/repo'
import { RepoConfig } from 'services/repo/useRepoConfig'
import { RepositoryConfigSchema } from 'services/repo/useRepoConfig'
import Api from 'shared/api'
import { NetworkErrorObject } from 'shared/api/helpers'
import A from 'ui/A'
Expand Down Expand Up @@ -71,7 +71,7 @@ export type PathContentResultType = z.infer<typeof PathContentsResultSchema>

const RepositorySchema = z.object({
__typename: z.literal('Repository'),
repositoryConfig: RepoConfig,
repositoryConfig: RepositoryConfigSchema,
branch: z.object({
head: z
.object({
Expand Down
131 changes: 127 additions & 4 deletions src/services/repo/useRepoConfig.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useRepoConfig } from './useRepoConfig'
const mockRepoConfig = {
owner: {
repository: {
__typename: 'Repository',
repositoryConfig: {
indicationRange: {
lowerRange: 60,
Expand Down Expand Up @@ -41,20 +42,57 @@ afterAll(() => {
server.close()
})

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

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

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

const mockUnsuccessfulParseError = {}

describe('useRepoConfig', () => {
function setup() {
function setup({
isNotFoundError = false,
isOwnerNotActivatedError = false,
isUnsuccessfulParseError = false,
}: SetupArgs) {
server.use(
graphql.query('RepoConfig', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(mockRepoConfig))
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(mockRepoConfig))
}
})
)
}

describe('calling hook', () => {
beforeEach(() => setup())

describe('no options are passed', () => {
it('returns the repository config', async () => {
setup({})
const { result } = renderHook(
() =>
useRepoConfig({
Expand All @@ -77,6 +115,7 @@ describe('useRepoConfig', () => {

describe('options are passed', () => {
it('returns the repository config', async () => {
setup({})
const { result } = renderHook(
() =>
useRepoConfig({
Expand All @@ -100,4 +139,88 @@ describe('useRepoConfig', () => {
})
})
})

describe('hook errors', () => {
beforeAll(() => {
console.error = () => {}
})
afterAll(() => {
jest.resetAllMocks()
})
it('can return unsuccessful parse error', async () => {
setup({ isUnsuccessfulParseError: true })

const { result } = renderHook(
() =>
useRepoConfig({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
opts: {
onSuccess: () => {},
},
}),
{ 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(
() =>
useRepoConfig({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
opts: {
onSuccess: () => {},
},
}),
{ wrapper }
)

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

const { result } = renderHook(
() =>
useRepoConfig({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
opts: {
onSuccess: () => {},
},
}),
{ wrapper }
)

await waitFor(() => expect(result.current.isError).toBeTruthy())
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
})
)
)
})
})
})
66 changes: 0 additions & 66 deletions src/services/repo/useRepoConfig.ts

This file was deleted.