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: Add hook for get components of a repo/branch with filtering (#827) #2439

Merged
merged 4 commits into from
Dec 11, 2023
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
1 change: 1 addition & 0 deletions src/services/branches/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useBranch'
export * from './useBranches'
export * from './useBranchComponents'
328 changes: 328 additions & 0 deletions src/services/branches/useBranchComponents.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'

import { useBranchComponents } from './useBranchComponents'

const mockBranchComponents = {
owner: {
repository: {
__typename: 'Repository',
branch: {
name: 'main',
head: {
commitid: 'commit-123',
components: [
{
id: 'compOneId',
name: 'compOneName',
},
{
id: 'compTwoId',
name: 'compTwoName',
},
],
},
},
},
},
}

const mockBranchComponentsFiltered = {
owner: {
repository: {
__typename: 'Repository',
branch: {
name: 'main',
head: {
commitid: 'commit-123',
components: [
{
id: 'compOneId',
name: 'compOneName',
},
],
},
},
},
},
}

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

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

const mockNullOwner = {
owner: null,
}

const mockUnsuccessfulParseError = {}

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

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 {
isNotFoundError?: boolean
isOwnerNotActivatedError?: boolean
isUnsuccessfulParseError?: boolean
isNullOwner?: boolean
isFiltered?: boolean
}

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

describe('calling hook', () => {
describe('returns repository typename of Repository', () => {
describe('there is valid data', () => {
it('fetches the branch data without filtering', async () => {
setup({})
const { result } = renderHook(
() =>
useBranchComponents({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
branch: 'main',
}),
{ wrapper }
)

await waitFor(() =>
expect(result.current.data).toStrictEqual({
branch: {
head: {
components: [
{
id: 'compOneId',
name: 'compOneName',
},
{
id: 'compTwoId',
name: 'compTwoName',
},
],
},
},
})
)
})

it('fetches the branch data filtering', async () => {
setup({ isFiltered: true })
const { result } = renderHook(
() =>
useBranchComponents({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
branch: 'main',
filters: { components: ['componename'] },
}),
{ wrapper }
)

await waitFor(() =>
expect(result.current.data).toStrictEqual({
branch: {
head: {
components: [
{
id: 'compOneId',
name: 'compOneName',
},
],
},
},
})
)
})
})

describe('there is a null owner', () => {
it('returns a null value', async () => {
setup({ isNullOwner: true })
const { result } = renderHook(
() =>
useBranchComponents({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
branch: 'main',
}),
{ wrapper }
)

await waitFor(() =>
expect(result.current.data).toStrictEqual({
branch: 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(
() =>
useBranchComponents({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
branch: 'main',
}),
{ wrapper }
)

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

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(
() =>
useBranchComponents({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
branch: 'main',
}),
{ wrapper }
)

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

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(
() =>
useBranchComponents({
provider: 'gh',
owner: 'codecov',
repo: 'cool-repo',
branch: 'main',
}),
{ wrapper }
)

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