From 93330a5de2c0c7234e592bd7dcbe805a8426e804 Mon Sep 17 00:00:00 2001 From: nicholas-codecov Date: Tue, 1 Oct 2024 08:31:00 -0400 Subject: [PATCH] chore: Update second group of services tests (#3301) --- ... useComparisonForCommitAndParent.test.tsx} | 27 +++- .../{normalize.spec.js => normalize.test.js} | 0 ...ams.spec.js => useLocationParams.test.jsx} | 0 ...eNavLinks.spec.js => useNavLinks.test.jsx} | 0 ...nks.spec.js => useStaticNavLinks.test.jsx} | 12 +- ...rrorToast.spec.tsx => ErrorToast.test.tsx} | 0 ...icToast.spec.tsx => GenericToast.test.tsx} | 0 ...derToast.spec.tsx => renderToast.test.tsx} | 0 .../{context.spec.js => context.test.jsx} | 0 ...ureFlags.spec.js => featureFlags.test.jsx} | 38 ++++-- src/services/tracking/pendo.spec.js | 92 ------------- src/services/tracking/pendo.test.jsx | 125 ++++++++++++++++++ .../{hooks.spec.js => useTracking.test.jsx} | 24 ++-- src/services/users/mocks.js | 6 +- ...User.spec.tsx => useInfiniteUser.test.tsx} | 26 ++-- ...ateUser.spec.js => useUpdateUser.test.jsx} | 15 +-- .../{useUsers.spec.js => useUsers.test.jsx} | 27 ++-- 17 files changed, 234 insertions(+), 158 deletions(-) rename src/services/comparison/useComparisonForCommitAndParent/{useComparisonForCommitAndParent.spec.tsx => useComparisonForCommitAndParent.test.tsx} (89%) rename src/services/navigation/{normalize.spec.js => normalize.test.js} (100%) rename src/services/navigation/{useLocationParams.spec.js => useLocationParams.test.jsx} (100%) rename src/services/navigation/useNavLinks/{useNavLinks.spec.js => useNavLinks.test.jsx} (100%) rename src/services/navigation/useNavLinks/{useStaticNavLinks.spec.js => useStaticNavLinks.test.jsx} (98%) rename src/services/toast/ErrorToast/{ErrorToast.spec.tsx => ErrorToast.test.tsx} (100%) rename src/services/toast/GenericToast/{GenericToast.spec.tsx => GenericToast.test.tsx} (100%) rename src/services/toast/{renderToast.spec.tsx => renderToast.test.tsx} (100%) rename src/services/toastNotification/{context.spec.js => context.test.jsx} (100%) rename src/services/tracking/{featureFlags.spec.js => featureFlags.test.jsx} (88%) delete mode 100644 src/services/tracking/pendo.spec.js create mode 100644 src/services/tracking/pendo.test.jsx rename src/services/tracking/{hooks.spec.js => useTracking.test.jsx} (90%) rename src/services/users/{useInfiniteUser.spec.tsx => useInfiniteUser.test.tsx} (87%) rename src/services/users/{useUpdateUser.spec.js => useUpdateUser.test.jsx} (91%) rename src/services/users/{useUsers.spec.js => useUsers.test.jsx} (88%) diff --git a/src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.spec.tsx b/src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.test.tsx similarity index 89% rename from src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.spec.tsx rename to src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.test.tsx index e88f841ee7..873f50feee 100644 --- a/src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.spec.tsx +++ b/src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.test.tsx @@ -1,8 +1,9 @@ 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 React from 'react' +import { type MockInstance } from 'vitest' import { useComparisonForCommitAndParent } from './useComparisonForCommitAndParent' @@ -143,15 +144,15 @@ describe('useComparisonForCommitAndParent', () => { isUnsuccessfulParseError = false, }: SetupArgs) { server.use( - graphql.query('ImpactedFileComparedWithParent', (req, res, ctx) => { + graphql.query('ImpactedFileComparedWithParent', (info) => { if (isNotFoundError) { - return res(ctx.status(200), ctx.data(mockNotFoundError)) + return HttpResponse.json({ data: mockNotFoundError }) } else if (isOwnerNotActivatedError) { - return res(ctx.status(200), ctx.data(mockOwnerNotActivatedError)) + return HttpResponse.json({ data: mockOwnerNotActivatedError }) } else if (isUnsuccessfulParseError) { - return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError)) + return HttpResponse.json({ data: mockUnsuccessfulParseError }) } else { - return res(ctx.status(200), ctx.data(baseMock)) + return HttpResponse.json({ data: baseMock }) } }) ) @@ -179,6 +180,16 @@ describe('useComparisonForCommitAndParent', () => { }) describe('when called and error', () => { + let consoleSpy: MockInstance + + beforeEach(() => { + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => null) + }) + + afterEach(() => { + consoleSpy.mockRestore() + }) + it('can return unsuccessful parse error', async () => { setup({ isUnsuccessfulParseError: true }) const { result } = renderHook( @@ -204,6 +215,7 @@ describe('useComparisonForCommitAndParent', () => { ) ) }) + it('can return not found error', async () => { setup({ isNotFoundError: true }) const { result } = renderHook( @@ -229,6 +241,7 @@ describe('useComparisonForCommitAndParent', () => { ) ) }) + it('can return owner not activated error', async () => { setup({ isOwnerNotActivatedError: true }) const { result } = renderHook( diff --git a/src/services/navigation/normalize.spec.js b/src/services/navigation/normalize.test.js similarity index 100% rename from src/services/navigation/normalize.spec.js rename to src/services/navigation/normalize.test.js diff --git a/src/services/navigation/useLocationParams.spec.js b/src/services/navigation/useLocationParams.test.jsx similarity index 100% rename from src/services/navigation/useLocationParams.spec.js rename to src/services/navigation/useLocationParams.test.jsx diff --git a/src/services/navigation/useNavLinks/useNavLinks.spec.js b/src/services/navigation/useNavLinks/useNavLinks.test.jsx similarity index 100% rename from src/services/navigation/useNavLinks/useNavLinks.spec.js rename to src/services/navigation/useNavLinks/useNavLinks.test.jsx diff --git a/src/services/navigation/useNavLinks/useStaticNavLinks.spec.js b/src/services/navigation/useNavLinks/useStaticNavLinks.test.jsx similarity index 98% rename from src/services/navigation/useNavLinks/useStaticNavLinks.spec.js rename to src/services/navigation/useNavLinks/useStaticNavLinks.test.jsx index 1c1e22051e..bc620f6c56 100644 --- a/src/services/navigation/useNavLinks/useStaticNavLinks.spec.js +++ b/src/services/navigation/useNavLinks/useStaticNavLinks.test.jsx @@ -5,7 +5,7 @@ import config from 'config' import { useStaticNavLinks } from './useStaticNavLinks' -jest.mock('config') +vi.mock('config') describe('useStaticNavLinks', () => { const view = renderHook(() => useStaticNavLinks(), { @@ -16,8 +16,14 @@ describe('useStaticNavLinks', () => { ), }) describe('cloud', () => { - beforeAll(() => jest.requireActual('config')) - afterAll(() => jest.mock('config')) + beforeAll(async () => { + await vi.importActual('config') + }) + + afterAll(() => { + vi.mock('config') + }) + const links = view.result.current describe.each` diff --git a/src/services/toast/ErrorToast/ErrorToast.spec.tsx b/src/services/toast/ErrorToast/ErrorToast.test.tsx similarity index 100% rename from src/services/toast/ErrorToast/ErrorToast.spec.tsx rename to src/services/toast/ErrorToast/ErrorToast.test.tsx diff --git a/src/services/toast/GenericToast/GenericToast.spec.tsx b/src/services/toast/GenericToast/GenericToast.test.tsx similarity index 100% rename from src/services/toast/GenericToast/GenericToast.spec.tsx rename to src/services/toast/GenericToast/GenericToast.test.tsx diff --git a/src/services/toast/renderToast.spec.tsx b/src/services/toast/renderToast.test.tsx similarity index 100% rename from src/services/toast/renderToast.spec.tsx rename to src/services/toast/renderToast.test.tsx diff --git a/src/services/toastNotification/context.spec.js b/src/services/toastNotification/context.test.jsx similarity index 100% rename from src/services/toastNotification/context.spec.js rename to src/services/toastNotification/context.test.jsx diff --git a/src/services/tracking/featureFlags.spec.js b/src/services/tracking/featureFlags.test.jsx similarity index 88% rename from src/services/tracking/featureFlags.spec.js rename to src/services/tracking/featureFlags.test.jsx index c5af8c67c0..c9356eb475 100644 --- a/src/services/tracking/featureFlags.spec.js +++ b/src/services/tracking/featureFlags.test.jsx @@ -1,23 +1,30 @@ import { renderHook } from '@testing-library/react' import Cookie from 'js-cookie' -import { useIdentifyUser } from 'shared/featureFlags' - import { useTrackFeatureFlags } from './featureFlags' -jest.mock('shared/featureFlags', () => ({ - useIdentifyUser: jest.fn(), +const mocks = vi.hoisted(() => ({ + useIdentifyUser: vi.fn(), })) +vi.mock('shared/featureFlags', async () => { + const actual = await vi.importActual('shared/featureFlags') + return { + ...actual, + useIdentifyUser: mocks.useIdentifyUser, + } +}) + describe('useTrackFeatureFlags', () => { - const mockIdentifyUser = jest.fn() + const mockIdentifyUser = vi.fn() describe('normal use', () => { beforeEach(() => { - useIdentifyUser.mockImplementation(mockIdentifyUser) + mocks.useIdentifyUser.mockImplementation(mockIdentifyUser) }) + afterEach(() => { - jest.clearAllMocks() + vi.clearAllMocks() }) it('Creates the expected user and key identified', () => { @@ -63,13 +70,15 @@ describe('useTrackFeatureFlags', () => { describe('impersonating on github', () => { beforeEach(() => { - useIdentifyUser.mockImplementation(mockIdentifyUser) + mocks.useIdentifyUser.mockImplementation(mockIdentifyUser) Cookie.set('staff_user', 'doggo') }) + afterEach(() => { - jest.clearAllMocks() + vi.clearAllMocks() Cookie.remove('staff_user') }) + it('Creates the expected user and key identified', () => { renderHook(() => useTrackFeatureFlags({ @@ -113,11 +122,12 @@ describe('useTrackFeatureFlags', () => { describe('impersonating on bitbucket', () => { beforeEach(() => { - useIdentifyUser.mockImplementation(mockIdentifyUser) + mocks.useIdentifyUser.mockImplementation(mockIdentifyUser) Cookie.set('staff_user', 'doggo') }) + afterEach(() => { - jest.clearAllMocks() + vi.clearAllMocks() Cookie.remove('staff_user') }) @@ -164,13 +174,15 @@ describe('useTrackFeatureFlags', () => { describe('impersonating on gitlab', () => { beforeEach(() => { - useIdentifyUser.mockImplementation(mockIdentifyUser) + mocks.useIdentifyUser.mockImplementation(mockIdentifyUser) Cookie.set('staff_user', 'doggo') }) + afterEach(() => { - jest.clearAllMocks() + vi.clearAllMocks() Cookie.remove('staff_user') }) + it('Creates the expected user and key identified', () => { renderHook(() => useTrackFeatureFlags({ diff --git a/src/services/tracking/pendo.spec.js b/src/services/tracking/pendo.spec.js deleted file mode 100644 index da13148143..0000000000 --- a/src/services/tracking/pendo.spec.js +++ /dev/null @@ -1,92 +0,0 @@ -import { renderHook } from '@testing-library/react' -import React from 'react' -import { useParams } from 'react-router-dom' - -import { useOwner } from 'services/user' - -import { firePendo, useUpdatePendoWithOwner } from './pendo' - -jest.mock('services/user') -jest.mock('react-router-dom', () => ({ - ...jest.requireActual('react-router-dom'), // import and retain the original functionalities - useParams: jest.fn(() => {}), - useLocation: jest.fn(() => {}), -})) - -const curUser = { - businessEmail: 'userbzemail@gmail.com', - email: 'user@gmail.com', - onboardingCompleted: true, - user: { - username: 'random', - }, - trackingMetadata: { - ownerid: 1999, - plan: 'users-free', - service: 'github', - staff: false, - }, -} - -const ownerData = { - ownerid: 123, - username: 'codecov', - isCurrentUserPartOfOrg: true, -} - -describe('initialize pendo', () => { - function setup() { - window.pendo = { - initialize: jest.fn(), - } - } - - it('fires pendo initialization with expected params', () => { - setup() - firePendo(curUser) - - expect(window.pendo.initialize).toHaveBeenCalledTimes(1) - }) -}) - -describe('update pendo on owner change', () => { - function setup() { - window.pendo = { - updateOptions: jest.fn(), - } - jest - .spyOn(React, 'useRef') - .mockReturnValueOnce({ current: { ...ownerData, ownerid: 456 } }) - - useParams.mockReturnValue({ owner: 'codecov' }) - useOwner.mockReturnValue({ data: ownerData }) - } - - it('fires pendo update options when pathname is different', () => { - setup() - - renderHook(() => useUpdatePendoWithOwner(curUser)) - - expect(window.pendo.updateOptions).toHaveBeenCalledTimes(1) - }) -}) - -describe('update pendo when owner is not changed', () => { - function setup() { - window.pendo = { - updateOptions: jest.fn(), - } - jest.spyOn(React, 'useRef').mockReturnValueOnce({ current: 'codecov' }) - - useParams.mockReturnValue({ owner: 'codecov' }) - useOwner.mockReturnValue({ data: ownerData }) - } - - it('does not fire pendo update', () => { - setup() - - renderHook(() => useUpdatePendoWithOwner(curUser)) - - expect(window.pendo.updateOptions).toHaveBeenCalledTimes(0) - }) -}) diff --git a/src/services/tracking/pendo.test.jsx b/src/services/tracking/pendo.test.jsx new file mode 100644 index 0000000000..d4a4671e3d --- /dev/null +++ b/src/services/tracking/pendo.test.jsx @@ -0,0 +1,125 @@ +import { renderHook } from '@testing-library/react' + +import { firePendo, useUpdatePendoWithOwner } from './pendo' + +const mocks = vi.hoisted(() => ({ + useParams: vi.fn(), + useLocation: vi.fn(), + useOwner: vi.fn(), + useRef: vi.fn(), +})) + +vi.mock('react', async () => { + const original = await vi.importActual('react') + return { + ...original, + useRef: mocks.useRef, + } +}) + +vi.mock('services/user', async () => { + const original = await vi.importActual('services/user') + return { + ...original, + useOwner: mocks.useOwner, + } +}) + +vi.mock('react-router-dom', async () => { + // import and retain the original functionalities + const original = await vi.importActual('react-router-dom') + return { + ...original, + useParams: mocks.useParams, + useLocation: mocks.useLocation, + } +}) + +const curUser = { + businessEmail: 'userbzemail@gmail.com', + email: 'user@gmail.com', + onboardingCompleted: true, + user: { + username: 'random', + }, + trackingMetadata: { + ownerid: 1999, + plan: 'users-free', + service: 'github', + staff: false, + }, +} + +const ownerData = { + ownerid: 123, + username: 'codecov', + isCurrentUserPartOfOrg: true, +} + +afterEach(() => { + vi.clearAllMocks() +}) + +describe('initialize pendo', () => { + function setup() { + const mockInitialize = vi.fn() + window.pendo = { + initialize: mockInitialize, + } + + return { mockInitialize } + } + + it('fires pendo initialization with expected params', () => { + const { mockInitialize } = setup() + firePendo(curUser) + + expect(mockInitialize).toHaveBeenCalledTimes(1) + }) +}) + +describe('update pendo on owner change', () => { + function setup() { + const mockUpdateOptions = vi.fn() + window.pendo = { + updateOptions: mockUpdateOptions, + } + + mocks.useRef.mockReturnValueOnce({ + current: { ...ownerData, ownerid: 456 }, + }) + mocks.useParams.mockReturnValue({ owner: 'codecov' }) + mocks.useOwner.mockReturnValue({ data: ownerData }) + + return { mockUpdateOptions } + } + + it('fires pendo update options when pathname is different', () => { + const { mockUpdateOptions } = setup() + renderHook(() => useUpdatePendoWithOwner(curUser)) + + expect(mockUpdateOptions).toHaveBeenCalledTimes(1) + }) +}) + +describe('update pendo when owner is not changed', () => { + function setup() { + const mockUpdateOptions = vi.fn() + window.pendo = { + updateOptions: mockUpdateOptions, + } + + mocks.useRef.mockReturnValueOnce({ current: 'codecov' }) + mocks.useParams.mockReturnValue({ owner: 'codecov' }) + mocks.useOwner.mockReturnValue({ data: ownerData }) + + return { mockUpdateOptions } + } + + it('does not fire pendo update', () => { + const { mockUpdateOptions } = setup() + renderHook(() => useUpdatePendoWithOwner(curUser)) + + expect(mockUpdateOptions).toHaveBeenCalledTimes(0) + }) +}) diff --git a/src/services/tracking/hooks.spec.js b/src/services/tracking/useTracking.test.jsx similarity index 90% rename from src/services/tracking/hooks.spec.js rename to src/services/tracking/useTracking.test.jsx index beea873269..e01edc7559 100644 --- a/src/services/tracking/hooks.spec.js +++ b/src/services/tracking/useTracking.test.jsx @@ -1,8 +1,8 @@ import * as Sentry from '@sentry/react' 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 { useTracking } from './useTracking' @@ -44,16 +44,16 @@ describe('useTracking', () => { function setup(user) { window.pendo = { - initialize: jest.fn(), - updateOptions: jest.fn(), + initialize: vi.fn(), + updateOptions: vi.fn(), } server.use( - graphql.query('CurrentUser', (req, res, ctx) => { - return res(ctx.status(200), ctx.data(user)) + graphql.query('CurrentUser', (info) => { + return HttpResponse.json({ data: user }) }), - graphql.query('DetailOwner', (req, res, ctx) => { - return res(ctx.status(200), ctx.data({ owner: 'codecov' })) + graphql.query('DetailOwner', (info) => { + return HttpResponse.json({ data: { owner: 'codecov' } }) }) ) } @@ -182,13 +182,17 @@ describe('useTracking', () => { }) describe('when user is not logged in', () => { + let consoleSpy beforeEach(() => { - const spy = jest.spyOn(console, 'error') - spy.mockImplementation(jest.fn()) + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) setup({ me: null }) }) + afterEach(() => { + consoleSpy.mockRestore() + }) + it('sets null user in sentry', async () => { renderHook(() => useTracking(), { wrapper }) diff --git a/src/services/users/mocks.js b/src/services/users/mocks.js index 9bc65f0e27..eab5e08c9a 100644 --- a/src/services/users/mocks.js +++ b/src/services/users/mocks.js @@ -1,11 +1,11 @@ /* eslint-disable camelcase */ -import { rest } from 'msw' +import { http, HttpResponse } from 'msw2' const usersUri = '/internal/:provider/:owner/users/?activated=&is_admin=&ordering=name&search=&page=1&page_size=50' -export const randomUsersHandler = rest.get(usersUri, (req, res, ctx) => { - return res(ctx.status(200), ctx.json(usersObject)) +export const randomUsersHandler = http.get(usersUri, (info) => { + return HttpResponse.json(usersObject) }) const usersObject = { diff --git a/src/services/users/useInfiniteUser.spec.tsx b/src/services/users/useInfiniteUser.test.tsx similarity index 87% rename from src/services/users/useInfiniteUser.spec.tsx rename to src/services/users/useInfiniteUser.test.tsx index 89ba8f17f3..ac5d16a78b 100644 --- a/src/services/users/useInfiniteUser.spec.tsx +++ b/src/services/users/useInfiniteUser.test.tsx @@ -1,7 +1,8 @@ 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 { type MockInstance } from 'vitest' import { useInfiniteUsers } from './useInfiniteUser' @@ -61,17 +62,15 @@ afterAll(() => server.close()) describe('useInfiniteUser', () => { function setup(options = {}) { server.use( - rest.get('/internal/gh/codecov/users', (req, res, ctx) => { - const { - url: { searchParams }, - } = req + http.get('/internal/gh/codecov/users', (info) => { + const searchParams = new URL(info.request.url).searchParams const pageNumber = Number(searchParams.get('page')) if (pageNumber > 1) { - return res(ctx.status(200), ctx.json(mockSecondResponse)) + return HttpResponse.json(mockSecondResponse) } - return res(ctx.status(200), ctx.json(mockFirstResponse)) + return HttpResponse.json(mockFirstResponse) }) ) } @@ -158,14 +157,21 @@ describe('useInfiniteUser', () => { }) describe('when the schema is invalid', () => { + let consoleSpy: MockInstance + beforeEach(() => { + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) server.use( - rest.get('/internal/gh/codecov/users', (req, res, ctx) => { - return res(ctx.status(200), ctx.json({ count: 2 })) + http.get('/internal/gh/codecov/users', (info) => { + return HttpResponse.json({ count: 2 }) }) ) }) + afterEach(() => { + consoleSpy.mockRestore() + }) + it('throws an error', async () => { const { result } = renderHook( () => diff --git a/src/services/users/useUpdateUser.spec.js b/src/services/users/useUpdateUser.test.jsx similarity index 91% rename from src/services/users/useUpdateUser.spec.js rename to src/services/users/useUpdateUser.test.jsx index c1ea621f61..99671663c8 100644 --- a/src/services/users/useUpdateUser.spec.js +++ b/src/services/users/useUpdateUser.test.jsx @@ -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 { useUpdateUser } from './useUpdateUser' @@ -45,12 +45,9 @@ afterAll(() => server.close()) describe('useUpdateUser', () => { function setup({ ownerid, body, opts = {} }) { server.use( - rest.patch( - `/internal/:provider/:owner/users/:ownerid`, - (req, res, ctx) => { - return res(ctx.status(200), ctx.json(body)) - } - ) + http.patch(`/internal/:provider/:owner/users/:ownerid`, (info) => { + return HttpResponse.json(body) + }) ) } @@ -84,7 +81,7 @@ describe('useUpdateUser', () => { }) describe('onSuccess handler', () => { - const mockSuccess = jest.fn() + const mockSuccess = vi.fn() beforeEach(() => { // pass mock response const mockRes = 'new account details data' diff --git a/src/services/users/useUsers.spec.js b/src/services/users/useUsers.test.jsx similarity index 88% rename from src/services/users/useUsers.spec.js rename to src/services/users/useUsers.test.jsx index b8750a7f9c..2cc34c2aa4 100644 --- a/src/services/users/useUsers.spec.js +++ b/src/services/users/useUsers.test.jsx @@ -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 { useUsers } from './useUsers' @@ -55,26 +55,31 @@ const wrapper = ) const server = setupServer() +beforeAll(() => { + server.listen() +}) + +afterEach(() => { + queryClient.clear() + server.resetHandlers() +}) -beforeAll(() => server.listen()) -afterEach(() => server.resetHandlers()) -afterAll(() => server.close()) +afterAll(() => { + server.close() +}) describe('useUsers', () => { function setup() { server.use( - rest.get(`/internal/:provider/:owner/users`, (req, res, ctx) => { - return res(ctx.status(200), ctx.json(users)) + http.get(`/internal/:provider/:owner/users`, (info) => { + return HttpResponse.json(users) }) ) } describe('when data is loaded', () => { - beforeEach(() => { - setup() - }) - it('returns the users data', async () => { + setup() const { result } = renderHook( () => useUsers({ provider, owner, query }), {