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

chore: Update pages/RepoPage/ConfigTab tests to Vitest #3332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a0dd034
update ConfigTab
nicholas-codecov Sep 27, 2024
e4930a9
update BadgesAndGraphs
nicholas-codecov Sep 27, 2024
ce2b911
update Badges
nicholas-codecov Sep 27, 2024
7c3e1be
update Graphs
nicholas-codecov Sep 27, 2024
eb3c263
update ConfigurationManager
nicholas-codecov Sep 27, 2024
67e943d
update FeatureGroup
nicholas-codecov Sep 27, 2024
ffbf45f
update FeatureItem
nicholas-codecov Sep 27, 2024
5f5ebc4
update useRepoConfigurationStatus
nicholas-codecov Sep 27, 2024
a4f049b
update GeneralTab
nicholas-codecov Sep 27, 2024
4be38f0
update EraseRepoContent
nicholas-codecov Sep 27, 2024
e7fd030
update RepoState
nicholas-codecov Sep 27, 2024
d869286
update DeactivateRepoModal
nicholas-codecov Sep 27, 2024
b74eb1b
update useRepoActivation
nicholas-codecov Sep 27, 2024
1a3c99f
update DefaultBranch
nicholas-codecov Sep 27, 2024
1be610c
update useRepoForTokensTeam
nicholas-codecov Sep 27, 2024
75c72be
update Tokens
nicholas-codecov Sep 27, 2024
c5ecf6c
update GraphToken
nicholas-codecov Sep 27, 2024
a7b355f
update Impact AnalysisToken
nicholas-codecov Sep 27, 2024
4138df2
update RepoUploadToken
nicholas-codecov Sep 27, 2024
de1cc82
update StaticAnalysisToken
nicholas-codecov Sep 27, 2024
3e5bd39
update TokensTeam
nicholas-codecov Sep 27, 2024
02c65ab
update YamlTab
nicholas-codecov Sep 27, 2024
4f2f977
update CurrentRepoSettings
nicholas-codecov Sep 27, 2024
f2fb2b2
update SecretString
nicholas-codecov Sep 27, 2024
5ea12a9
update ValidateYaml
nicholas-codecov Sep 27, 2024
be76bb4
update YAML
nicholas-codecov Sep 27, 2024
caf0d0a
update index files
nicholas-codecov Sep 27, 2024
2f96905
Merge branch 'main' into chore-update-pages-repo-page-config-tab-test…
nicholas-codecov Oct 2, 2024
3aa9556
Merge branch 'main' into chore-update-pages-repo-page-config-tab-test…
nicholas-codecov Oct 2, 2024
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { render, screen } from '@testing-library/react'
import { MemoryRouter, Route } from 'react-router-dom'

import { useOwner } from 'services/user'

import ConfigTab from './ConfigTab'

jest.mock('services/user')
const mockedUseOwner = useOwner as jest.Mock
const mocks = vi.hoisted(() => ({
useOwner: vi.fn(),
}))

vi.mock('services/user', () => ({
useOwner: mocks.useOwner,
}))

jest.mock('./tabs/ConfigurationManager', () => () => 'Configuration Manager')
vi.mock('./tabs/ConfigurationManager', () => ({
default: () => 'Configuration Manager',
}))

const wrapper: (initialEntries?: string) => React.FC<React.PropsWithChildren> =
(initialEntries = '/gh/codecov/codecov-client/config') =>
Expand All @@ -24,7 +29,7 @@ interface SetupArgs {

describe('ConfigTab', () => {
function setup({ isCurrentUserPartOfOrg = true }: SetupArgs) {
mockedUseOwner.mockReturnValue({ data: { isCurrentUserPartOfOrg } })
mocks.useOwner.mockReturnValue({ data: { isCurrentUserPartOfOrg } })
}

describe('Render for a repo', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
render,
screen,
waitFor,
waitForElementToBeRemoved,
} from '@testing-library/react'
import { render, screen, waitFor } from '@testing-library/react'
import { userEvent } from '@testing-library/user-event'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'
import { useIntersection } from 'react-use'

import config from 'config'

import Badges from './Badges'

jest.mock('config')
jest.mock('react-use/lib/useIntersection')
const mockedUseIntersection = useIntersection as jest.Mock
const mocks = vi.hoisted(() => ({
useIntersection: vi.fn().mockReturnValue({ isIntersecting: false }),
}))

vi.mock('config')
vi.mock('react-use', async () => {
const actual = await vi.importActual('react-use')
return {
...actual,
useIntersection: mocks.useIntersection,
}
})

const mockNoBranches = {
__typename: 'Repository',
Expand All @@ -37,9 +40,7 @@ const mockBranches = (hasNextPage = false) => ({
{
node: {
name: 'branch-1',
head: {
commitid: 'asdf123',
},
head: { commitid: 'asdf123' },
},
},
{
Expand Down Expand Up @@ -80,12 +81,12 @@ const queryClient = new QueryClient({
})

beforeAll(() => {
jest.clearAllMocks()
server.listen()
})
afterEach(() => {
queryClient.clear()
server.resetHandlers()
vi.clearAllMocks()
})
afterAll(() => {
server.close()
Expand All @@ -98,34 +99,32 @@ type SetupArgs = {

describe('Badges', () => {
function setup({ noBranches = false, hasNextPage = false }: SetupArgs) {
const fetchNextPage = jest.fn()
const mockSearching = jest.fn()
const fetchNextPage = vi.fn()
const mockSearching = vi.fn()
config.BASE_URL = 'https://stage-web.codecov.dev'
server.use(
graphql.query('GetBranches', (req, res, ctx) => {
if (req.variables?.after) {
fetchNextPage()
graphql.query('GetBranches', (info) => {
if (!!info.variables?.after) {
fetchNextPage(info.variables?.after)
}

if (req.variables?.filters?.searchValue) {
mockSearching(req.variables?.filters?.searchValue)
if (info.variables?.filters?.searchValue) {
mockSearching(info.variables?.filters?.searchValue)
}

if (noBranches) {
return res(
ctx.status(200),
ctx.data({
return HttpResponse.json({
data: {
owner: { repository: mockNoBranches },
})
)
},
})
}

return res(
ctx.status(200),
ctx.data({
return HttpResponse.json({
data: {
owner: { repository: mockBranches(hasNextPage) },
})
)
},
})
})
)

Expand Down Expand Up @@ -203,12 +202,10 @@ describe('Badges', () => {

const button = await screen.findByText('Default branch')
expect(button).toBeInTheDocument()
user.click(button)
await user.click(button)

const branch = await screen.findByText('branch-2')
user.click(branch)

await waitForElementToBeRemoved(branch)
await user.click(branch)

const baseUrl = await screen.findByText(
'[![codecov](https://stage-web.codecov.dev/gh/codecov/codecov-client/branch/branch-2/graph/badge.svg?token=WIO9JXFGE)](https://stage-web.codecov.dev/gh/codecov/codecov-client)'
Expand All @@ -224,7 +221,7 @@ describe('Badges', () => {

const button = await screen.findByText('Default branch')
expect(button).toBeInTheDocument()
user.click(button)
await user.click(button)

const loading = await screen.findByText('Loading more items...')
expect(loading).toBeInTheDocument()
Expand All @@ -238,40 +235,38 @@ describe('Badges', () => {

const button = await screen.findByText('Default branch')
expect(button).toBeInTheDocument()
user.click(button)
await user.click(button)

await waitFor(() =>
expect(screen.queryAllByText('Default branch')).toHaveLength(2)
)
})

it('tries to load more', async () => {
mockedUseIntersection.mockReturnValue({ isIntersecting: true })
mocks.useIntersection.mockReturnValue({ isIntersecting: true })
const { user, fetchNextPage } = setup({ hasNextPage: true })
render(<Badges graphToken="WIO9JXFGE" />, {
wrapper,
})

const button = await screen.findByText('Default branch')
expect(button).toBeInTheDocument()
user.click(button)

expect(await screen.findByText('Search')).toBeInTheDocument()
await user.click(button)

await waitFor(() => expect(fetchNextPage).toHaveBeenCalled())
await waitFor(() =>
expect(fetchNextPage).toHaveBeenCalledWith('end-cursor')
)
})

it('handles searching', async () => {
const { user, mockSearching } = setup({ hasNextPage: true })
const { user, mockSearching } = setup({ hasNextPage: false })
render(<Badges graphToken="WIO9JXFGE" />, {
wrapper,
})

const button = await screen.findByText('Default branch')
expect(button).toBeInTheDocument()
user.click(button)

expect(await screen.findByText('Search')).toBeInTheDocument()
await user.click(button)

const searchField = await screen.findByPlaceholderText('Search')
await user.type(searchField, 'branch-3')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import BadgesAndGraphsTab from './BadgesAndGraphsTab'
Expand All @@ -21,7 +21,7 @@ const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
)

beforeAll(() => {
jest.clearAllMocks()
vi.clearAllMocks()
server.listen()
console.error = () => {}
})
Expand All @@ -34,24 +34,22 @@ afterAll(() => server.close())
describe('BadgesAndGraphsTab', () => {
function setup({ graphToken }: { graphToken: string | null }) {
server.use(
graphql.query('GetBranches', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.data({
graphql.query('GetBranches', (info) => {
return HttpResponse.json({
data: {
owner: {
repository: {
branches: {
edges: [],
},
},
},
})
)
},
})
}),
graphql.query('GetRepoSettings', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.data({
graphql.query('GetRepoSettings', (info) => {
return HttpResponse.json({
data: {
owner: {
repository: {
__typename: 'Repository',
Expand All @@ -68,8 +66,8 @@ describe('BadgesAndGraphsTab', () => {
staticAnalysisToken: null,
},
},
})
)
},
})
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router'

import { TierNames, TTierNames } from 'services/tier'
Expand Down Expand Up @@ -83,8 +83,8 @@ interface SetupArgs {
describe('Configuration Manager', () => {
function setup({ repoConfig = mockRepoConfig({}) }: SetupArgs) {
server.use(
graphql.query('GetRepoConfigurationStatus', (req, res, ctx) => {
return res(ctx.status(200), ctx.data({ owner: repoConfig }))
graphql.query('GetRepoConfigurationStatus', (info) => {
return HttpResponse.json({ data: { owner: repoConfig } })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'

import { useRepoConfigurationStatus } from './useRepoConfigurationStatus'

Expand Down Expand Up @@ -82,17 +82,17 @@ describe('useRepoConfigurationStatus', () => {
nullOwner: nullResponse = false,
}: SetupArgs) {
server.use(
graphql.query('GetRepoConfigurationStatus', (req, res, ctx) => {
graphql.query('GetRepoConfigurationStatus', (info) => {
if (badResponse) {
return res(ctx.status(200), ctx.data({}))
return HttpResponse.json({})
} else if (repoNotFound) {
return res(ctx.status(200), ctx.data(mockRepoNotFound))
return HttpResponse.json({ data: mockRepoNotFound })
} else if (ownerNotActivated) {
return res(ctx.status(200), ctx.data(mockOwnerNotActivated))
return HttpResponse.json({ data: mockOwnerNotActivated })
} else if (nullResponse) {
return res(ctx.status(200), ctx.data(mockNullOwner))
return HttpResponse.json({ data: mockNullOwner })
}
return res(ctx.status(200), ctx.data(mockGoodResponse))
return HttpResponse.json({ data: mockGoodResponse })
})
)
}
Expand Down
Loading
Loading