Skip to content

Commit

Permalink
chore: Update services/selfHosted tests to Vitest (#3307)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov authored Oct 1, 2024
1 parent 34be384 commit 7abf6b2
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 131 deletions.
File renamed without changes.
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 { rest } from 'msw'
import { setupServer } from 'msw/node'
import { http, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import { useSelfHostedCurrentUser } from './useSelfHostedCurrentUser'
Expand All @@ -27,36 +27,36 @@ const wrapper = ({ children }) => (
)

const server = setupServer()
beforeAll(() => server.listen())
beforeAll(() => {
server.listen()
})

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

afterAll(() => {
server.close()
})

describe('useSelfHostedCurrentUser', () => {
function setup() {
server.use(
rest.get('/internal/users/current', (req, res, ctx) =>
res(ctx.status(200), ctx.json(user))
)
http.get('/internal/users/current', (info) => {
return HttpResponse.json(user)
})
)
}

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

it('returns the user info', async () => {
setup()
const { result } = renderHook(() => useSelfHostedCurrentUser(), {
wrapper,
})

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

await waitFor(() => expect(result.current.data).toEqual(user))
})
})
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 { useSelfHostedHasAdmins } from './useSelfHostedHasAdmins'

Expand All @@ -13,55 +13,36 @@ const wrapper = ({ children }) => (
)

const server = setupServer()
beforeAll(() => server.listen())
beforeAll(() => {
server.listen()
})

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

afterAll(() => {
server.close()
})

describe('useSelfHostedHasAdmins', () => {
function setup({ data }) {
server.use(
graphql.query('HasAdmins', (req, res, ctx) =>
res(ctx.status(200), ctx.data(data))
)
graphql.query('HasAdmins', (info) => {
return HttpResponse.json({ data })
})
)
}

describe('when called', () => {
beforeEach(() => {
setup({ data: { config: { hasAdmins: true } } })
})

it('returns isLoading', () => {
const { result } = renderHook(
() => useSelfHostedHasAdmins({ provider: 'gl' }),
{
wrapper,
}
)

expect(result.current.isLoading).toBeTruthy()
})
})

describe('when data is loaded', () => {
beforeEach(async () => {
setup({ data: { config: { hasAdmins: true } } })
})

it('returns the user info', async () => {
setup({ data: { config: { hasAdmins: true } } })
const { result } = renderHook(
() => useSelfHostedHasAdmins({ provider: 'gl' }),
{
wrapper,
}
{ wrapper }
)

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

await waitFor(() => expect(result.current.data).toEqual(true))
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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 { type MockInstance } from 'vitest'

import { useSelfHostedSeatsAndLicense } from './useSelfHostedSeatsAndLicense'

const mockSelfHostedLicense = {
config: {
seatsUsed: 5,
seatsLimit: 30,
selfHostedLicense: {
expirationDate: '2020-05-09T00:00:00',
},
selfHostedLicense: { expirationDate: '2020-05-09T00:00:00' },
},
}

Expand All @@ -20,12 +19,12 @@ 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>
)

const server = setupServer()
beforeAll(() => {
server.listen()
})
Expand All @@ -46,11 +45,11 @@ interface SetupArgs {
describe('useSelfHostedSeatsAndLicense', () => {
function setup({ isUnsuccessfulParseError = false }: SetupArgs) {
server.use(
graphql.query('SelfHostedSeatsAndLicense', (req, res, ctx) => {
graphql.query('SelfHostedSeatsAndLicense', (info) => {
if (isUnsuccessfulParseError) {
return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError))
return HttpResponse.json({ data: mockUnsuccessfulParseError })
} else {
return res(ctx.status(200), ctx.data(mockSelfHostedLicense))
return HttpResponse.json({ data: mockSelfHostedLicense })
}
})
)
Expand All @@ -62,14 +61,10 @@ describe('useSelfHostedSeatsAndLicense', () => {
it('returns the license details', async () => {
setup({})
const { result } = renderHook(
() =>
useSelfHostedSeatsAndLicense({
provider: 'gh',
}),
() => useSelfHostedSeatsAndLicense({ provider: 'gh' }),
{ wrapper }
)

await waitFor(() => result.current.isSuccess)
await waitFor(() =>
expect(result.current.data).toEqual({
seatsUsed: 5,
Expand All @@ -84,32 +79,25 @@ describe('useSelfHostedSeatsAndLicense', () => {
})

describe('unsuccessful parse of zod schema', () => {
beforeEach(() => {
jest.spyOn(console, 'error')
let consoleSpy: MockInstance
beforeAll(() => {
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
jest.resetAllMocks()
afterAll(() => {
consoleSpy.mockRestore()
})

it('throws a 404', async () => {
setup({ isUnsuccessfulParseError: true })
const { result } = renderHook(
() =>
useSelfHostedSeatsAndLicense({
provider: 'gh',
}),
{
wrapper,
}
() => useSelfHostedSeatsAndLicense({ provider: 'gh' }),
{ wrapper }
)

await waitFor(() => expect(result.current.isError).toBeTruthy())
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
})
expect.objectContaining({ status: 404 })
)
)
})
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 { MemoryRouter, Route } from 'react-router-dom'

import { useSelfHostedSeatsConfig } from './useSelfHostedSeatsConfig'
Expand All @@ -25,35 +25,35 @@ const wrapper = ({ children }) => (
)

const server = setupServer()
beforeAll(() => server.listen())
beforeAll(() => {
server.listen()
})

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

afterAll(() => {
server.close()
})

describe('useSelfHostedSeatsConfig', () => {
function setup() {
server.use(
graphql.query('Seats', (req, res, ctx) =>
res(ctx.status(200), ctx.data(mockData))
)
graphql.query('Seats', (req, res, ctx) => {
return HttpResponse.json({ data: mockData })
})
)
}

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

it('returns data', async () => {
setup()
const { result } = renderHook(() => useSelfHostedSeatsConfig(), {
wrapper,
})

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

await waitFor(() =>
expect(result.current.data).toStrictEqual({
seatsUsed: 5,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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 { useSelfHostedSettings } from './useSelfHostedSettings'

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

const mockResponse = {
config: {
Expand All @@ -27,6 +27,7 @@ const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
</QueryClientProvider>
)

const server = setupServer()
beforeAll(() => {
server.listen()
})
Expand All @@ -43,11 +44,11 @@ afterAll(() => {
describe('useSelfHostedSettings', () => {
function setup({ invalidResponse = false }) {
server.use(
graphql.query('SelfHostedSettings', (req, res, ctx) => {
graphql.query('SelfHostedSettings', (info) => {
if (invalidResponse) {
return res(ctx.status(200), ctx.data({}))
return HttpResponse.json({})
}
return res(ctx.status(200), ctx.data(mockResponse))
return HttpResponse.json({ data: mockResponse })
})
)
}
Expand All @@ -57,9 +58,6 @@ describe('useSelfHostedSettings', () => {
setup({})
const { result } = renderHook(() => useSelfHostedSettings(), { wrapper })

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

await waitFor(() =>
expect(result.current.data).toStrictEqual({
planAutoActivate: true,
Expand All @@ -71,8 +69,14 @@ describe('useSelfHostedSettings', () => {
})

describe('invalid response', () => {
let consoleSpy: MockInstance

beforeAll(() => {
console.error = () => {}
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})

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

it('rejects with 404', async () => {
Expand All @@ -81,12 +85,13 @@ describe('useSelfHostedSettings', () => {
wrapper,
})

await waitFor(() => expect(result.current.isError).toBeTruthy())
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
dev: 'useSelfHostedSettings - 404 schema parsing failed',
})
await waitFor(() =>
expect(result.current.error).toEqual(
expect.objectContaining({
status: 404,
dev: 'useSelfHostedSettings - 404 schema parsing failed',
})
)
)
})
})
Expand Down
Loading

0 comments on commit 7abf6b2

Please sign in to comment.