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 new Use Repos Team hook #2328

Merged
merged 3 commits into from
Oct 19, 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/repos/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useRepos'
export * from './config'
export * from './useReposTeam'
173 changes: 173 additions & 0 deletions src/services/repos/useReposTeam.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { useReposTeam } from './useReposTeam'

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

const repo1 = {
name: 'codecov-bash',
active: true,
activated: true,
private: false,
latestCommitAt: '2021-04-22T14:09:39.826948+00:00',
lines: 99,
author: {
username: 'codecov',
},
}

const repo2 = {
name: 'codecov-bash-2',
active: true,
activated: true,
private: false,
latestCommitAt: '2021-04-22T14:09:39.826948+00:00',
lines: 99,
author: {
username: 'codecov',
},
}

const repo3 = {
name: 'codecov-bash-3',
active: true,
activated: true,
private: false,
latestCommitAt: '2021-04-22T14:09:39.826948+00:00',
lines: 99,
author: {
username: 'codecov',
},
}

const repo4 = {
name: 'codecov-bash-4',
active: true,
activated: true,
private: false,
latestCommitAt: '2021-04-22T14:09:39.826948+00:00',
lines: 99,
author: {
username: 'codecov',
},
}

const server = setupServer()

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

describe('useReposTeam', () => {
function setup() {
server.use(
graphql.query('GetReposTeam', (req, res, ctx) => {
const data = {
owner: {
repositories: {
edges: req.variables.after
? [
{
node: repo3,
},
{
node: repo4,
},
]
: [
{
node: repo1,
},
{
node: repo2,
},
],
pageInfo: {
hasNextPage: req.variables.after ? false : true,
endCursor: req.variables.after
? 'aa'
: 'MjAyMC0wOC0xMSAxNzozMDowMiswMDowMHwxMDA=',
},
},
},
}
return res(ctx.status(200), ctx.data(data))
})
)
}

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

it('returns repositories', async () => {
const { result } = renderHook(
() =>
useReposTeam({
activated: true,
owner: 'codecov',
}),
{
wrapper,
}
)

await waitFor(() =>
expect(result.current.data).toEqual({
repos: [repo1, repo2],
})
)
})
})

describe('when call next page', () => {
beforeEach(async () => {
setup()
})

it('returns repositories of the user', async () => {
const { result } = renderHook(
() =>
useReposTeam({
owner: 'codecov',
activated: true,
first: 2,
}),
{
wrapper,
}
)

await waitFor(() => result.current.isFetching)
await waitFor(() => !result.current.isFetching)

result.current.fetchNextPage()

await waitFor(() => result.current.isFetching)
await waitFor(() => !result.current.isFetching)

await waitFor(() =>
expect(result.current.data).toEqual({
repos: [repo1, repo2, repo3, repo4],
})
)
})
})
})
177 changes: 177 additions & 0 deletions src/services/repos/useReposTeam.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { useInfiniteQuery } from '@tanstack/react-query'
import { useParams } from 'react-router-dom'
import { z } from 'zod'

import Api from 'shared/api'
import { mapEdges } from 'shared/utils/graphql'

import { orderingOptions } from './config'

const RepositorySchema = z.object({

Check warning on line 10 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L10

Added line #L10 was not covered by tests

Check warning on line 10 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L10

Added line #L10 was not covered by tests

Check warning on line 10 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L10

Added line #L10 was not covered by tests

Check warning on line 10 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L10

Added line #L10 was not covered by tests
name: z.string(),
active: z.boolean(),
activated: z.boolean(),
private: z.boolean(),
latestCommitAt: z.string().nullable(),
lines: z.number().nullable(),
author: z.object({
username: z.string().nullable(),
}),
})

const repositoryFragment = `

Check warning on line 22 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L22

Added line #L22 was not covered by tests

Check warning on line 22 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L22

Added line #L22 was not covered by tests

Check warning on line 22 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L22

Added line #L22 was not covered by tests

Check warning on line 22 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L22

Added line #L22 was not covered by tests
fragment RepoForList on Repository {
name
active
activated
private
latestCommitAt
lines
author {
username
}
}
`

interface FetchReposTeamArgs {
provider: string
owner: string
variables: {
filters: {
activated: boolean
term?: string
repoNames?: string[]
}
ordering?: string
direction?: string
first?: number
}
after?: string
signal?: AbortSignal
}

const RequestSchema = z.object({

Check warning on line 53 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L53

Added line #L53 was not covered by tests

Check warning on line 53 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L53

Added line #L53 was not covered by tests

Check warning on line 53 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L53

Added line #L53 was not covered by tests

Check warning on line 53 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L53

Added line #L53 was not covered by tests
owner: z
.object({
repositories: z
.object({
edges: z.array(
z.object({
node: RepositorySchema,
})
),
pageInfo: z.object({
hasNextPage: z.boolean(),
endCursor: z.string().nullable(),
}),
})
.nullable(),
})
.nullable(),
})

function fetchReposForOwner({
provider,
owner,
variables,
after,
signal,
}: FetchReposTeamArgs) {
const query = `

Check warning on line 80 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L79-L80

Added lines #L79 - L80 were not covered by tests

Check warning on line 80 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L79-L80

Added lines #L79 - L80 were not covered by tests

Check warning on line 80 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L79-L80

Added lines #L79 - L80 were not covered by tests

Check warning on line 80 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L79-L80

Added lines #L79 - L80 were not covered by tests
query GetReposTeam($filters: RepositorySetFilters!, $owner: String!, $ordering: RepositoryOrdering!, $direction: OrderingDirection!, $after: String, $first: Int) {
owner(username: $owner) {
repositories(filters: $filters, ordering: $ordering, orderingDirection: $direction, first: $first, after: $after) {
edges {
node {
...RepoForList
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
${repositoryFragment}
`

return Api.graphql({

Check warning on line 99 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L99

Added line #L99 was not covered by tests

Check warning on line 99 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L99

Added line #L99 was not covered by tests

Check warning on line 99 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L99

Added line #L99 was not covered by tests

Check warning on line 99 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L99

Added line #L99 was not covered by tests
provider,
query,
signal,
variables: {
...variables,
owner,
after,
},
}).then((res) => {
const parsedRes = RequestSchema.safeParse(res?.data)

Check warning on line 109 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L108-L109

Added lines #L108 - L109 were not covered by tests

Check warning on line 109 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L108-L109

Added lines #L108 - L109 were not covered by tests

Check warning on line 109 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L108-L109

Added lines #L108 - L109 were not covered by tests

Check warning on line 109 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L108-L109

Added lines #L108 - L109 were not covered by tests

if (!parsedRes.success) {
return Promise.reject({

Check warning on line 112 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L112

Added line #L112 was not covered by tests

Check warning on line 112 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L112

Added line #L112 was not covered by tests

Check warning on line 112 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L112

Added line #L112 was not covered by tests

Check warning on line 112 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L112

Added line #L112 was not covered by tests
status: 404,
data: null,
})
}

const owner = parsedRes.data?.owner

Check warning on line 118 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L118

Added line #L118 was not covered by tests

Check warning on line 118 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L118

Added line #L118 was not covered by tests

Check warning on line 118 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L118

Added line #L118 was not covered by tests

Check warning on line 118 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L118

Added line #L118 was not covered by tests

return {

Check warning on line 120 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L120

Added line #L120 was not covered by tests

Check warning on line 120 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L120

Added line #L120 was not covered by tests

Check warning on line 120 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L120

Added line #L120 was not covered by tests

Check warning on line 120 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L120

Added line #L120 was not covered by tests
repos: mapEdges(owner?.repositories),
pageInfo: owner?.repositories?.pageInfo,
}
})
}

interface UseReposTeamArgs {
activated: boolean
term?: string
owner: string
sortItem?: {
ordering: string
direction: string
}
first?: number
repoNames?: string[]
}

export function useReposTeam({
activated,
term,
owner,
sortItem = orderingOptions[0],
first = 20,
repoNames,
...options
}: UseReposTeamArgs) {
const { provider } = useParams<{ provider: string }>()
const variables = {

Check warning on line 149 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L147-L149

Added lines #L147 - L149 were not covered by tests

Check warning on line 149 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L147-L149

Added lines #L147 - L149 were not covered by tests

Check warning on line 149 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L147-L149

Added lines #L147 - L149 were not covered by tests

Check warning on line 149 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L147-L149

Added lines #L147 - L149 were not covered by tests
filters: { activated, term, repoNames },
ordering: sortItem?.ordering,
direction: sortItem?.direction,
first,
}

const { data, ...rest } = useInfiniteQuery(

Check warning on line 156 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L156

Added line #L156 was not covered by tests

Check warning on line 156 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L156

Added line #L156 was not covered by tests

Check warning on line 156 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L156

Added line #L156 was not covered by tests

Check warning on line 156 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L156

Added line #L156 was not covered by tests
['GetReposTeam', provider, variables, owner],
({ pageParam, signal }) => {
return fetchReposForOwner({

Check warning on line 159 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L158-L159

Added lines #L158 - L159 were not covered by tests

Check warning on line 159 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L158-L159

Added lines #L158 - L159 were not covered by tests

Check warning on line 159 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L158-L159

Added lines #L158 - L159 were not covered by tests

Check warning on line 159 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L158-L159

Added lines #L158 - L159 were not covered by tests
provider,
variables,
owner,
after: pageParam,
signal,
})
},
{
getNextPageParam: (data) =>
data?.pageInfo?.hasNextPage ? data.pageInfo.endCursor : undefined,
...options,
}
)
return {
data: { repos: data?.pages.map((page) => page.repos).flat() },

Check warning on line 174 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - Staging / codecov/patch

src/services/repos/useReposTeam.tsx#L173-L174

Added lines #L173 - L174 were not covered by tests

Check warning on line 174 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov - QA / codecov/patch

src/services/repos/useReposTeam.tsx#L173-L174

Added lines #L173 - L174 were not covered by tests

Check warning on line 174 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov Public QA / codecov/patch

src/services/repos/useReposTeam.tsx#L173-L174

Added lines #L173 - L174 were not covered by tests

Check warning on line 174 in src/services/repos/useReposTeam.tsx

View check run for this annotation

Codecov / codecov/patch

src/services/repos/useReposTeam.tsx#L173-L174

Added lines #L173 - L174 were not covered by tests
...rest,
}
}
Loading