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/PlanPage tests to Vitest #3322

Merged
merged 64 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
8743c16
update context
nicholas-codecov Sep 26, 2024
9cdf901
update PlanPage
nicholas-codecov Sep 26, 2024
9313473
update usePlanPageData
nicholas-codecov Sep 26, 2024
831c034
update CancelPlanPage
nicholas-codecov Sep 26, 2024
1652981
update useProPlanMonth
nicholas-codecov Sep 26, 2024
cf48cb2
update DowngradePlan
nicholas-codecov Sep 26, 2024
dfa3828
update CancelButton
nicholas-codecov Sep 26, 2024
1c2dc29
update useBarecancel
nicholas-codecov Sep 26, 2024
4860dc3
update SpecialOffer
nicholas-codecov Sep 26, 2024
3a33af2
update TeamPlanSpecialOffer
nicholas-codecov Sep 26, 2024
672e95c
update TeamPlanCard
nicholas-codecov Sep 26, 2024
fd78be1
update CurrentOrgPlan
nicholas-codecov Sep 26, 2024
901e2d3
update BillingDetails
nicholas-codecov Sep 26, 2024
dfe2f3c
update AddressCard
nicholas-codecov Sep 26, 2024
e6da3c7
update EmailAddress
nicholas-codecov Sep 26, 2024
b4828f6
update useEnterpriseCloudPlanSupport
nicholas-codecov Sep 26, 2024
cea0ab8
update PaymentCard
nicholas-codecov Sep 26, 2024
7f0a464
update CurrentPlanCard
nicholas-codecov Sep 26, 2024
71100fc
update EnterprisePlanCard
nicholas-codecov Sep 26, 2024
c7882ac
update FreePlanCard
nicholas-codecov Sep 26, 2024
cbb2404
update PlanUpgradePro
nicholas-codecov Sep 26, 2024
7bc7541
update PlanUpgradeTeam
nicholas-codecov Sep 26, 2024
7329bf8
update ProPlanSubheading
nicholas-codecov Sep 26, 2024
4a2fb9b
update PaidPlanCard
nicholas-codecov Sep 26, 2024
d7bacf4
update ActionsBilling
nicholas-codecov Sep 26, 2024
fc77cf4
update PlanPricing
nicholas-codecov Sep 26, 2024
c072403
update InfoMessageCancellation
nicholas-codecov Sep 26, 2024
44f93b4
update InfoMessageStripeCallback
nicholas-codecov Sep 26, 2024
70e378c
update LatestInvoiceCard
nicholas-codecov Sep 26, 2024
825db4b
update InvoiceDetail
nicholas-codecov Sep 26, 2024
1280774
update generateAddressInfo
nicholas-codecov Sep 26, 2024
30121e7
update InvoiceFooter
nicholas-codecov Sep 26, 2024
cefc154
update InvoiceHeader
nicholas-codecov Sep 26, 2024
ac97903
update InvoiceItems
nicholas-codecov Sep 26, 2024
4682bc5
update InvoicesPage
nicholas-codecov Sep 26, 2024
2aa7688
update UpgradePlanPage
nicholas-codecov Sep 26, 2024
c12e89b
update PlanDetailsControls
nicholas-codecov Sep 26, 2024
86c5417
update UpgradeDetails
nicholas-codecov Sep 26, 2024
7d39671
update ProPlanDetails
nicholas-codecov Sep 26, 2024
66f4822
update SentryPlanDetails
nicholas-codecov Sep 26, 2024
5610b1e
update TeamPlanDetails
nicholas-codecov Sep 26, 2024
b6db9ca
update UpgradeForm
nicholas-codecov Sep 26, 2024
ba93b4d
update Controller
nicholas-codecov Sep 26, 2024
95904c1
update ProPlanController
nicholas-codecov Sep 26, 2024
274e213
update BillingOptions
nicholas-codecov Sep 26, 2024
bf09a46
update PriceCallout
nicholas-codecov Sep 26, 2024
b446e44
update UserCount
nicholas-codecov Sep 26, 2024
af97604
update SentryPlanController
nicholas-codecov Sep 26, 2024
0802de0
update BillingOptions
nicholas-codecov Sep 26, 2024
a4e8fa8
update PriceCallout
nicholas-codecov Sep 26, 2024
8b6cf15
update UserCount
nicholas-codecov Sep 26, 2024
61799ce
update TeamPlanController
nicholas-codecov Sep 26, 2024
e17c34d
update BillingOptions
nicholas-codecov Sep 26, 2024
46d0212
update ErrorBanner
nicholas-codecov Sep 26, 2024
a622f09
update PriceCallout
nicholas-codecov Sep 26, 2024
97057cc
update UserCount
nicholas-codecov Sep 26, 2024
f5bec6b
update usePlanParams
nicholas-codecov Sep 26, 2024
a41f76f
update PlanTypeOptions
nicholas-codecov Sep 26, 2024
dcfd0b9
update UpdateBlurb
nicholas-codecov Sep 26, 2024
89eb03e
update UpdateButton
nicholas-codecov Sep 26, 2024
742b9c9
update Tabs
nicholas-codecov Sep 26, 2024
b347aaf
update index files
nicholas-codecov Sep 26, 2024
a936962
fix issues with useEnterpriseCloudPlanSupport
nicholas-codecov Sep 26, 2024
edf0526
Merge branch 'main' into chore-update-pages-plan-page-tests-to-vitest
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,22 +1,30 @@
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 { Suspense } from 'react'
import { MemoryRouter, Route } from 'react-router-dom'

import config from 'config'

import PlanPage from './PlanPage'

jest.mock('config')

jest.mock('./Tabs', () => () => 'Tabs')
jest.mock('./subRoutes/CancelPlanPage', () => () => 'CancelPlanPage')
jest.mock('./subRoutes/CurrentOrgPlan', () => () => 'CurrentOrgPlan')
jest.mock('./subRoutes/InvoicesPage', () => () => 'InvoicesPage')
jest.mock('./subRoutes/InvoiceDetailsPage', () => () => 'InvoiceDetailsPage')
jest.mock('./subRoutes/UpgradePlanPage', () => () => 'UpgradePlanPage')
vi.mock('config')

vi.mock('./Tabs', () => ({ default: () => 'Tabs' }))
vi.mock('./subRoutes/CancelPlanPage', () => ({
default: () => 'CancelPlanPage',
}))
vi.mock('./subRoutes/CurrentOrgPlan', () => ({
default: () => 'CurrentOrgPlan',
}))
vi.mock('./subRoutes/InvoicesPage', () => ({ default: () => 'InvoicesPage' }))
vi.mock('./subRoutes/InvoiceDetailsPage', () => ({
default: () => 'InvoiceDetailsPage',
}))
vi.mock('./subRoutes/UpgradePlanPage', () => ({
default: () => 'UpgradePlanPage',
}))

const server = setupServer()
const queryClient = new QueryClient({
Expand Down Expand Up @@ -54,7 +62,7 @@ beforeAll(() => {
afterEach(() => {
queryClient.clear()
server.resetHandlers()
jest.resetAllMocks()
vi.resetAllMocks()
})
afterAll(() => {
server.close()
Expand All @@ -73,9 +81,9 @@ describe('PlanPage', () => {
config.IS_SELF_HOSTED = isSelfHosted

server.use(
graphql.query('PlanPageData', (req, res, ctx) =>
res(ctx.status(200), ctx.data({ owner }))
)
graphql.query('PlanPageData', (info) => {
return HttpResponse.json({ data: { owner } })
})
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen } from '@testing-library/react'
import { setupServer } from 'msw/node'
import { MemoryRouter, Route } from 'react-router-dom'

import config from 'config'

import { useFlags } from 'shared/featureFlags'

import Tabs from './Tabs'

jest.mock('config')

jest.mock('shared/featureFlags')

const mockedUseFlags = useFlags as jest.Mock
const mocks = vi.hoisted(() => ({
useFlags: vi.fn(),
}))

const queryClient = new QueryClient()
const server = setupServer()
vi.mock('config')
vi.mock('shared/featureFlags', async () => {
const actual = await vi.importActual('shared/featureFlags')
return {
...actual,
useFlags: mocks.useFlags,
}
})

const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/analytics/gh/codecov']}>
<Route path="/analytics/:provider/:owner">{children}</Route>
</MemoryRouter>
</QueryClientProvider>
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<MemoryRouter initialEntries={['/analytics/gh/codecov']}>
<Route path="/analytics/:provider/:owner">{children}</Route>
</MemoryRouter>
)

beforeAll(() => {
server.listen({ onUnhandledRequest: 'warn' })
console.error = () => {}
})
beforeEach(() => {
queryClient.clear()
server.resetHandlers()
})
afterAll(() => {
server.close()
})

describe('Tabs', () => {
function setup(isSelfHosted: boolean = false) {
config.IS_SELF_HOSTED = isSelfHosted
mockedUseFlags.mockReturnValue({ codecovAiFeaturesTab: true })
mocks.useFlags.mockReturnValue({ codecovAiFeaturesTab: true })
}

describe('when user is part of the org', () => {
Expand Down Expand Up @@ -127,7 +113,7 @@ describe('Tabs', () => {

describe('ai features tab', () => {
it('does not render tab when flag is off', () => {
mockedUseFlags.mockReturnValue({ codecovAiFeaturesTab: false })
mocks.useFlags.mockReturnValue({ codecovAiFeaturesTab: false })
render(<Tabs />, { wrapper })

expect(
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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 { MemoryRouter, Route } from 'react-router-dom'
import { type MockInstance } from 'vitest'

import { usePlanPageData } from './usePlanPageData'

const mockOwner = {
username: 'TerrySmithDC',
username: 'cool-user',
isCurrentUserPartOfOrg: true,
numberOfUploads: 30,
}
Expand Down Expand Up @@ -35,11 +36,11 @@ afterAll(() => server.close())
describe('usePlanPageData', () => {
function setup({ invalidSchema = false }) {
server.use(
graphql.query('PlanPageData', (req, res, ctx) => {
graphql.query('PlanPageData', (info) => {
if (invalidSchema) {
return res(ctx.status(200), ctx.data({}))
return HttpResponse.json({ data: {} })
}
return res(ctx.status(200), ctx.data({ owner: mockOwner }))
return HttpResponse.json({ data: { owner: mockOwner } })
})
)
}
Expand Down Expand Up @@ -67,6 +68,15 @@ describe('usePlanPageData', () => {
})

describe('when schema parsing fails', () => {
let consoleSpy: MockInstance
beforeAll(() => {
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterAll(() => {
consoleSpy.mockRestore()
})

it('rejects with status 404', async () => {
setup({ invalidSchema: true })
const { result } = renderHook(
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, rest } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, http, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { Suspense } from 'react'
import { MemoryRouter, Route } from 'react-router-dom'

Expand All @@ -10,12 +10,11 @@ import { Plans } from 'shared/utils/billing'

import CancelPlanPage from './CancelPlanPage'

jest.mock('./subRoutes/SpecialOffer', () => () => 'SpecialOffer')
jest.mock('./subRoutes/DowngradePlan', () => () => 'DowngradePlan')
jest.mock(
'./subRoutes/TeamPlanSpecialOffer',
() => () => 'TeamPlanSpecialOffer'
)
vi.mock('./subRoutes/SpecialOffer', () => ({ default: () => 'SpecialOffer' }))
vi.mock('./subRoutes/DowngradePlan', () => ({ default: () => 'DowngradePlan' }))
vi.mock('./subRoutes/TeamPlanSpecialOffer', () => ({
default: () => 'TeamPlanSpecialOffer',
}))

const teamPlans = [
{
Expand Down Expand Up @@ -137,25 +136,15 @@ describe('CancelPlanPage', () => {
hasTeamPlans = false,
}: SetupProps = {}) {
server.use(
rest.get('internal/gh/codecov/account-details/', (req, res, ctx) =>
res(
ctx.status(200),
ctx.json({
plan: {
value: planValue,
},
subscriptionDetail: {
customer: {
discount: hasDiscount,
},
},
})
)
),
graphql.query('GetPlanData', (req, res, ctx) =>
res(
ctx.status(200),
ctx.data({
http.get('internal/gh/codecov/account-details/', (info) => {
return HttpResponse.json({
plan: { value: planValue },
subscriptionDetail: { customer: { discount: hasDiscount } },
})
}),
graphql.query('GetPlanData', (info) => {
return HttpResponse.json({
data: {
owner: {
hasPrivateRepos: true,
plan: {
Expand All @@ -164,19 +153,18 @@ describe('CancelPlanPage', () => {
value: planValue,
},
},
})
)
),
graphql.query('GetAvailablePlans', (req, res, ctx) =>
res(
ctx.status(200),
ctx.data({
},
})
}),
graphql.query('GetAvailablePlans', (info) => {
return HttpResponse.json({
data: {
owner: {
availablePlans: mockAvailablePlans({ hasTeamPlans }),
},
})
)
)
},
})
})
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { act, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter, Route, useParams } from 'react-router-dom'

import { useCancelPlan } from 'services/account'
import { useAddNotification } from 'services/toastNotification'
import { MemoryRouter, Route } from 'react-router-dom'

import CancelButton from './CancelButton'
import { useBarecancel } from './useBarecancel'

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'), // import and retain the original functionalities
useParams: jest.fn(() => {}),
const mocks = vi.hoisted(() => ({
useParams: vi.fn(),
useBarecancel: vi.fn(),
useCancelPlan: vi.fn(),
useAddNotification: vi.fn(),
}))
jest.mock('./useBarecancel')
jest.mock('services/account')
jest.mock('services/toastNotification')

vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom')
return {
...actual, // import and retain the original functionalities
useParams: mocks.useParams,
}
})

vi.mock('./useBarecancel', async () => {
const actual = await vi.importActual('./useBarecancel')
return {
...actual,
useBarecancel: mocks.useBarecancel,
}
})

vi.mock('services/account', async () => {
const actual = await vi.importActual('services/account')
return {
...actual,
useCancelPlan: mocks.useCancelPlan,
}
})

vi.mock('services/toastNotification', async () => {
const actual = await vi.importActual('services/toastNotification')
return {
...actual,
useAddNotification: mocks.useAddNotification,
}
})

const queryClient = new QueryClient({
defaultOptions: {
Expand Down Expand Up @@ -47,17 +74,17 @@ const wrapper = ({ children }) => (
describe('CancelButton', () => {
function setup(baremetricsBlocked = false) {
const user = userEvent.setup()
const mutate = jest.fn()
const addNotification = jest.fn()
const mutate = vi.fn()
const addNotification = vi.fn()

useAddNotification.mockReturnValue(addNotification)
useParams.mockReturnValue({ owner: 'Ollie', provider: 'gh' })
useCancelPlan.mockReturnValue({
mocks.useAddNotification.mockReturnValue(addNotification)
mocks.useParams.mockReturnValue({ owner: 'Ollie', provider: 'gh' })
mocks.useCancelPlan.mockReturnValue({
isLoading: false,
mutate,
onError: jest.fn(),
onError: vi.fn(),
})
useBarecancel.mockReturnValue({ baremetricsBlocked })
mocks.useBarecancel.mockReturnValue({ baremetricsBlocked })

return { mutate, addNotification, user }
}
Expand Down
Loading
Loading