diff --git a/src/services/pull/hooks.spec.js b/src/services/pull/hooks.spec.js deleted file mode 100644 index 814faace84..0000000000 --- a/src/services/pull/hooks.spec.js +++ /dev/null @@ -1,728 +0,0 @@ -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { renderHook, waitFor } from '@testing-library/react' -import { graphql } from 'msw' -import { setupServer } from 'msw/node' - -import { usePull, useSingularImpactedFileComparison } from './index' - -const queryClient = new QueryClient({ - defaultOptions: { - queries: { - retry: false, - }, - }, -}) - -const wrapper = ({ children }) => ( - {children} -) - -const server = setupServer() - -beforeAll(() => server.listen()) -afterEach(() => server.resetHandlers()) -afterAll(() => server.close()) - -const mockImpactedFiles = [ - { - isCriticalFile: true, - missesCount: 3, - fileName: 'mafs.js', - headName: 'flag1/mafs.js', - baseCoverage: { - percentCovered: 45.38, - }, - headCoverage: { - percentCovered: 90.23, - }, - patchCoverage: { - percentCovered: 27.43, - }, - changeCoverage: 41, - }, -] - -const pull = { - owner: { - isCurrentUserPartOfOrg: true, - repository: { - __typename: 'Repository', - defaultBranch: 'main', - private: false, - pull: { - commits: { - edges: [ - { - node: { - state: 'complete', - commitid: 'fc43199ccde1f21a940aa3d596c711c1c420651f', - message: - 'create component to hold bundle list table for a given pull 2', - author: { - username: 'nicholas-codecov', - }, - }, - }, - ], - }, - compareWithBase: { - state: 'complete', - __typename: 'Comparison', - flagComparisons: [], - patchTotals: { - percentCovered: 92.12, - }, - baseTotals: { - percentCovered: 98.25, - }, - headTotals: { - percentCovered: 78.33, - }, - impactedFiles: { - __typename: 'ImpactedFiles', - results: mockImpactedFiles, - }, - changeCoverage: 38.94, - hasDifferentNumberOfHeadAndBaseReports: true, - }, - pullId: 2510, - title: 'feat: Create bundle analysis table for a given pull', - state: 'OPEN', - author: { - username: 'nicholas-codecov', - }, - head: { - ciPassed: true, - branchName: - 'gh-eng-994-create-bundle-analysis-table-for-a-given-pull', - state: 'complete', - commitid: 'fc43199b07c52cf3d6c19b7cdb368f74387c38ab', - totals: { - percentCovered: 78.33, - }, - uploads: { - totalCount: 4, - edges: [], - }, - }, - updatestamp: '2024-01-12T12:56:18.912860', - behindBy: 82367894, - behindByCommit: '1798hvs8ofhn', - comparedTo: { - commitid: '2d6c42fe217c61b007b2c17544a9d85840381857', - uploads: { - totalCount: 1, - edges: [], - }, - }, - }, - }, - }, -} - -const provider = 'gh' -const owner = 'codecov' -const repo = 'gazebo' - -describe('usePull', () => { - afterEach(() => queryClient.clear()) - - function setup(data) { - server.use( - graphql.query('Pull', (req, res, ctx) => { - return res(ctx.status(200), ctx.data(data)) - }) - ) - } - - describe('when called', () => { - beforeEach(() => { - setup(pull) - }) - - describe('when data is loaded', () => { - it('returns the data', async () => { - const { result } = renderHook( - () => usePull({ provider, owner, repo }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => - expect(result.current.data).toEqual({ - defaultBranch: 'main', - hasAccess: true, - pull: { - behindBy: 82367894, - behindByCommit: '1798hvs8ofhn', - pullId: 2510, - title: 'feat: Create bundle analysis table for a given pull', - state: 'OPEN', - updatestamp: '2024-01-12T12:56:18.912860', - author: { username: 'nicholas-codecov' }, - comparedTo: { - commitid: '2d6c42fe217c61b007b2c17544a9d85840381857', - uploads: { totalCount: 1, edges: [] }, - }, - head: { - state: 'complete', - ciPassed: true, - branchName: - 'gh-eng-994-create-bundle-analysis-table-for-a-given-pull', - commitid: 'fc43199b07c52cf3d6c19b7cdb368f74387c38ab', - totals: { percentCovered: 78.33 }, - uploads: { totalCount: 4, edges: [] }, - }, - commits: { - edges: [ - { - node: { - state: 'complete', - commitid: 'fc43199ccde1f21a940aa3d596c711c1c420651f', - message: - 'create component to hold bundle list table for a given pull 2', - author: { username: 'nicholas-codecov' }, - }, - }, - ], - }, - compareWithBase: { - __typename: 'Comparison', - state: 'complete', - patchTotals: { percentCovered: 92.12 }, - baseTotals: { percentCovered: 98.25 }, - headTotals: { percentCovered: 78.33 }, - impactedFiles: { - __typename: 'ImpactedFiles', - results: [ - { - isCriticalFile: true, - missesCount: 3, - fileName: 'mafs.js', - headName: 'flag1/mafs.js', - baseCoverage: { percentCovered: 45.38 }, - headCoverage: { percentCovered: 90.23 }, - patchCoverage: { percentCovered: 27.43 }, - changeCoverage: 41, - }, - ], - }, - flagComparisons: [], - changeCoverage: 38.94, - hasDifferentNumberOfHeadAndBaseReports: true, - }, - }, - }) - ) - }) - }) - - describe('when it is of OwnerNotActivatedError type', () => { - it('returns the error', async () => { - setup({ - owner: { - isCurrentUserPartOfOrg: false, - repository: { - __typename: 'OwnerNotActivatedError', - message: 'owner not activated', - }, - }, - }) - - const { result } = renderHook( - () => usePull({ provider, owner, repo }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => expect(result.current.error.status).toEqual(403)) - }) - }) - - describe('when it is of NotFoundError type', () => { - it('returns the error', async () => { - setup({ - owner: { - isCurrentUserPartOfOrg: false, - repository: { - __typename: 'NotFoundError', - message: 'not found', - }, - }, - }) - - const { result } = renderHook( - () => usePull({ provider, owner, repo }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => expect(result.current.error.status).toEqual(404)) - }) - }) - - describe('when there is no pull returned', () => { - it('returns pull null', async () => { - setup({ - owner: { - isCurrentUserPartOfOrg: true, - repository: { - __typename: 'Repository', - defaultBranch: 'main', - private: false, - pull: null, - }, - }, - }) - - const { result } = renderHook( - () => usePull({ provider, owner, repo }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => expect(result.current.data.pull).toEqual(null)) - }) - }) - - describe('when schema is not valid', () => { - it('throws an error', async () => { - setup({ - owner: { - isCurrentUserPartOfOrg: true, - repository: { - __typename: 'Repository', - defaultBranch: 'main', - private: false, - pull: { - commits: { - edges: [ - { - node: { - state: 'complete', - commitid: 'fc43199ccde1f21a940aa3d596c711c1c420651f', - message: - 'create component to hold bundle list table for a given pull 2', - }, - }, - ], - }, - }, - }, - }, - }) - - const { result } = renderHook( - () => usePull({ provider, owner, repo }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => expect(result.current.error.status).toEqual(404)) - }) - }) - }) -}) - -const mockSingularImpactedFilesData = { - headName: 'file A', - hashedPath: 'hashedFilePath', - isRenamedFile: false, - isDeletedFile: false, - isCriticalFile: false, - isNewFile: true, - headCoverage: { - percentCovered: 90.23, - }, - baseCoverage: { - percentCovered: 23.42, - }, - patchCoverage: { - percentCovered: 27.43, - }, - changeCoverage: 58.333333333333336, - segments: { - __typename: 'SegmentComparisons', - results: [ - { - header: '@@ -0,0 +1,45 @@', - hasUnintendedChanges: false, - lines: [ - { - baseNumber: null, - headNumber: '1', - baseCoverage: null, - headCoverage: 'H', - content: '+export default class Calculator {', - }, - { - baseNumber: null, - headNumber: '2', - baseCoverage: null, - headCoverage: 'H', - content: '+ private value = 0;', - }, - { - baseNumber: null, - headNumber: '3', - baseCoverage: null, - headCoverage: 'H', - content: '+ private calcMode = ""', - }, - ], - }, - ], - }, -} - -describe('useSingularImpactedFileComparison', () => { - afterEach(() => queryClient.clear()) - - function setup(data) { - server.use( - graphql.query('ImpactedFileComparison', (req, res, ctx) => { - return res(ctx.status(200), ctx.data(data)) - }) - ) - } - - describe('when called', () => { - beforeEach(() => { - setup({ - owner: { - repository: { - __typename: 'Repository', - pull: { - compareWithBase: { - __typename: 'Comparison', - impactedFile: mockSingularImpactedFilesData, - }, - }, - }, - }, - }) - }) - - describe('when data is loaded', () => { - it('returns the data', async () => { - const { result } = renderHook( - () => - useSingularImpactedFileComparison({ - provider, - owner, - repo, - pullId: 10, - path: 'someFile.js', - }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => - expect(result.current.data).toEqual({ - fileLabel: 'New', - hashedPath: 'hashedFilePath', - headName: 'file A', - isCriticalFile: false, - segments: [ - { - hasUnintendedChanges: false, - header: '@@ -0,0 +1,45 @@', - lines: [ - { - baseCoverage: null, - baseNumber: null, - content: '+export default class Calculator {', - headCoverage: 'H', - headNumber: '1', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private value = 0;', - headCoverage: 'H', - headNumber: '2', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private calcMode = ""', - headCoverage: 'H', - headNumber: '3', - }, - ], - }, - ], - }) - ) - }) - }) - }) - - describe('when called with renamed file', () => { - beforeEach(() => { - setup({ - owner: { - repository: { - __typename: 'Repository', - pull: { - compareWithBase: { - __typename: 'Comparison', - impactedFile: { - ...mockSingularImpactedFilesData, - isRenamedFile: true, - isNewFile: false, - }, - }, - }, - }, - }, - }) - }) - - describe('when data is loaded', () => { - it('returns the data', async () => { - const { result } = renderHook( - () => - useSingularImpactedFileComparison({ - provider, - owner, - repo, - pullId: 10, - path: 'someFile.js', - }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => - expect(result.current.data).toEqual({ - fileLabel: 'Renamed', - headName: 'file A', - isCriticalFile: false, - hashedPath: 'hashedFilePath', - segments: [ - { - hasUnintendedChanges: false, - header: '@@ -0,0 +1,45 @@', - lines: [ - { - baseCoverage: null, - baseNumber: null, - content: '+export default class Calculator {', - headCoverage: 'H', - headNumber: '1', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private value = 0;', - headCoverage: 'H', - headNumber: '2', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private calcMode = ""', - headCoverage: 'H', - headNumber: '3', - }, - ], - }, - ], - }) - ) - }) - }) - }) - - describe('when called with deleted file', () => { - beforeEach(() => { - setup({ - owner: { - repository: { - __typename: 'Repository', - pull: { - compareWithBase: { - __typename: 'Comparison', - impactedFile: { - ...mockSingularImpactedFilesData, - isDeletedFile: true, - }, - }, - }, - }, - }, - }) - }) - - describe('when data is loaded', () => { - it('returns the data', async () => { - const { result } = renderHook( - () => - useSingularImpactedFileComparison({ - provider, - owner, - repo, - pullId: 10, - path: 'someFile.js', - }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => - expect(result.current.data).toEqual({ - fileLabel: 'New', - headName: 'file A', - isCriticalFile: false, - hashedPath: 'hashedFilePath', - segments: [ - { - hasUnintendedChanges: false, - header: '@@ -0,0 +1,45 @@', - lines: [ - { - baseCoverage: null, - baseNumber: null, - content: '+export default class Calculator {', - headCoverage: 'H', - headNumber: '1', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private value = 0;', - headCoverage: 'H', - headNumber: '2', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private calcMode = ""', - headCoverage: 'H', - headNumber: '3', - }, - ], - }, - ], - }) - ) - }) - }) - }) - - describe('when called with an unchanged file label', () => { - beforeEach(() => { - setup({ - owner: { - repository: { - __typename: 'Repository', - pull: { - compareWithBase: { - __typename: 'Comparison', - impactedFile: { - ...mockSingularImpactedFilesData, - isNewFile: false, - isRenamedFile: false, - isDeletedFile: false, - }, - }, - }, - }, - }, - }) - }) - - describe('when data is loaded', () => { - it('returns the data', async () => { - const { result } = renderHook( - () => - useSingularImpactedFileComparison({ - provider, - owner, - repo, - pullId: 10, - path: 'someFile.js', - }), - { - wrapper, - } - ) - - await waitFor(() => result.current.isLoading) - await waitFor(() => !result.current.isLoading) - - await waitFor(() => - expect(result.current.data).toEqual({ - fileLabel: null, - headName: 'file A', - isCriticalFile: false, - hashedPath: 'hashedFilePath', - segments: [ - { - hasUnintendedChanges: false, - header: '@@ -0,0 +1,45 @@', - lines: [ - { - baseCoverage: null, - baseNumber: null, - content: '+export default class Calculator {', - headCoverage: 'H', - headNumber: '1', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private value = 0;', - headCoverage: 'H', - headNumber: '2', - }, - { - baseCoverage: null, - baseNumber: null, - content: '+ private calcMode = ""', - headCoverage: 'H', - headNumber: '3', - }, - ], - }, - ], - }) - ) - }) - }) - }) -}) diff --git a/src/services/pull/index.js b/src/services/pull/index.ts similarity index 100% rename from src/services/pull/index.js rename to src/services/pull/index.ts diff --git a/src/services/pull/usePrefetchSingleFileComp.spec.tsx b/src/services/pull/usePrefetchSingleFileComp.test.tsx similarity index 93% rename from src/services/pull/usePrefetchSingleFileComp.spec.tsx rename to src/services/pull/usePrefetchSingleFileComp.test.tsx index 9d7008daa4..d6bb9ced2c 100644 --- a/src/services/pull/usePrefetchSingleFileComp.spec.tsx +++ b/src/services/pull/usePrefetchSingleFileComp.test.tsx @@ -1,7 +1,8 @@ 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 { usePrefetchSingleFileComp } from './usePrefetchSingleFileComp' @@ -181,24 +182,24 @@ describe('usePrefetchSingleFileComp', () => { isNullOwner = false, }: SetupArgs) { server.use( - graphql.query('ImpactedFileComparison', (req, res, ctx) => { + graphql.query('ImpactedFileComparison', (info) => { if (isRenamed) { - return res(ctx.status(200), ctx.data(mockRenamedFile)) + return HttpResponse.json({ data: mockRenamedFile }) } else if (isDeleted) { - return res(ctx.status(200), ctx.data(mockDeletedFile)) + return HttpResponse.json({ data: mockDeletedFile }) } else if (isUnchanged) { - return res(ctx.status(200), ctx.data(mockUnchangedFile)) + return HttpResponse.json({ data: mockUnchangedFile }) } else if (isOwnerNotActivatedError) { - return res(ctx.status(200), ctx.data(mockDataOwnerNotActivated)) + return HttpResponse.json({ data: mockDataOwnerNotActivated }) } else if (isRepositoryNotFoundError) { - return res(ctx.status(200), ctx.data(mockDataRepositoryNotFound)) + return HttpResponse.json({ data: mockDataRepositoryNotFound }) } else if (isUnsuccessfulParseError) { - return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError)) + return HttpResponse.json({ data: mockUnsuccessfulParseError }) } else if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } - return res(ctx.status(200), ctx.data(mockData)) + return HttpResponse.json({ data: mockData }) }) ) } @@ -398,14 +399,13 @@ describe('usePrefetchSingleFileComp', () => { }) describe('rejecting request', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('fails to parse bad schema', async () => { diff --git a/src/services/pull/usePull.test.tsx b/src/services/pull/usePull.test.tsx new file mode 100644 index 0000000000..92134c9388 --- /dev/null +++ b/src/services/pull/usePull.test.tsx @@ -0,0 +1,394 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { renderHook, waitFor } from '@testing-library/react' +import { graphql, HttpResponse } from 'msw2' +import { setupServer } from 'msw2/node' +import { type MockInstance } from 'vitest' + +import { usePull } from './usePull' + +const queryClient = new QueryClient({ + defaultOptions: { + queries: { + retry: false, + }, + }, +}) + +const wrapper: React.FC = ({ children }) => ( + {children} +) + +const server = setupServer() +beforeAll(() => { + server.listen() +}) + +afterEach(() => { + queryClient.clear() + server.resetHandlers() +}) + +afterAll(() => { + server.close() +}) + +const mockImpactedFiles = [ + { + isCriticalFile: true, + missesCount: 3, + fileName: 'mafs.js', + headName: 'flag1/mafs.js', + baseCoverage: { + percentCovered: 45.38, + }, + headCoverage: { + percentCovered: 90.23, + }, + patchCoverage: { + percentCovered: 27.43, + }, + changeCoverage: 41, + }, +] + +const pull = { + owner: { + isCurrentUserPartOfOrg: true, + repository: { + __typename: 'Repository', + defaultBranch: 'main', + private: false, + pull: { + commits: { + edges: [ + { + node: { + state: 'complete', + commitid: 'fc43199ccde1f21a940aa3d596c711c1c420651f', + message: + 'create component to hold bundle list table for a given pull 2', + author: { + username: 'nicholas-codecov', + }, + }, + }, + ], + }, + compareWithBase: { + state: 'complete', + __typename: 'Comparison', + flagComparisons: [], + patchTotals: { + percentCovered: 92.12, + }, + baseTotals: { + percentCovered: 98.25, + }, + headTotals: { + percentCovered: 78.33, + }, + impactedFiles: { + __typename: 'ImpactedFiles', + results: mockImpactedFiles, + }, + changeCoverage: 38.94, + hasDifferentNumberOfHeadAndBaseReports: true, + }, + pullId: 2510, + title: 'feat: Create bundle analysis table for a given pull', + state: 'OPEN', + author: { + username: 'nicholas-codecov', + }, + head: { + ciPassed: true, + branchName: + 'gh-eng-994-create-bundle-analysis-table-for-a-given-pull', + state: 'complete', + commitid: 'fc43199b07c52cf3d6c19b7cdb368f74387c38ab', + totals: { + percentCovered: 78.33, + }, + uploads: { + totalCount: 4, + edges: [], + }, + }, + updatestamp: '2024-01-12T12:56:18.912860', + behindBy: 82367894, + behindByCommit: '1798hvs8ofhn', + comparedTo: { + commitid: '2d6c42fe217c61b007b2c17544a9d85840381857', + uploads: { + totalCount: 1, + edges: [], + }, + }, + }, + }, + }, +} + +const provider = 'gh' +const owner = 'codecov' +const repo = 'gazebo' + +describe('usePull', () => { + afterEach(() => queryClient.clear()) + + function setup(data: {}) { + server.use( + graphql.query('Pull', (info) => { + return HttpResponse.json({ data }) + }) + ) + } + + describe('when called', () => { + beforeEach(() => { + setup(pull) + }) + + describe('when data is loaded', () => { + it('returns the data', async () => { + const { result } = renderHook( + () => usePull({ provider, owner, repo, pullId: '2510' }), + { wrapper } + ) + + await waitFor(() => result.current.isLoading) + await waitFor(() => !result.current.isLoading) + + await waitFor(() => + expect(result.current.data).toEqual({ + defaultBranch: 'main', + hasAccess: true, + pull: { + behindBy: 82367894, + behindByCommit: '1798hvs8ofhn', + pullId: 2510, + title: 'feat: Create bundle analysis table for a given pull', + state: 'OPEN', + updatestamp: '2024-01-12T12:56:18.912860', + author: { username: 'nicholas-codecov' }, + comparedTo: { + commitid: '2d6c42fe217c61b007b2c17544a9d85840381857', + uploads: { totalCount: 1, edges: [] }, + }, + head: { + state: 'complete', + ciPassed: true, + branchName: + 'gh-eng-994-create-bundle-analysis-table-for-a-given-pull', + commitid: 'fc43199b07c52cf3d6c19b7cdb368f74387c38ab', + totals: { percentCovered: 78.33 }, + uploads: { totalCount: 4, edges: [] }, + }, + commits: { + edges: [ + { + node: { + state: 'complete', + commitid: 'fc43199ccde1f21a940aa3d596c711c1c420651f', + message: + 'create component to hold bundle list table for a given pull 2', + author: { username: 'nicholas-codecov' }, + }, + }, + ], + }, + compareWithBase: { + __typename: 'Comparison', + state: 'complete', + patchTotals: { percentCovered: 92.12 }, + baseTotals: { percentCovered: 98.25 }, + headTotals: { percentCovered: 78.33 }, + impactedFiles: { + __typename: 'ImpactedFiles', + results: [ + { + isCriticalFile: true, + missesCount: 3, + fileName: 'mafs.js', + headName: 'flag1/mafs.js', + baseCoverage: { percentCovered: 45.38 }, + headCoverage: { percentCovered: 90.23 }, + patchCoverage: { percentCovered: 27.43 }, + changeCoverage: 41, + }, + ], + }, + flagComparisons: [], + changeCoverage: 38.94, + hasDifferentNumberOfHeadAndBaseReports: true, + }, + }, + }) + ) + }) + }) + + describe('when it is of OwnerNotActivatedError type', () => { + let consoleSpy: MockInstance + beforeEach(() => { + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + }) + + afterEach(() => { + consoleSpy.mockRestore() + }) + + it('returns the error', async () => { + setup({ + owner: { + isCurrentUserPartOfOrg: false, + repository: { + __typename: 'OwnerNotActivatedError', + message: 'owner not activated', + }, + }, + }) + + const { result } = renderHook( + () => usePull({ provider, owner, repo, pullId: '2510' }), + { wrapper } + ) + + await waitFor(() => result.current.isLoading) + await waitFor(() => !result.current.isLoading) + + await waitFor(() => + expect(result.current.error).toEqual( + expect.objectContaining({ status: 403 }) + ) + ) + }) + }) + + describe('when it is of NotFoundError type', () => { + let consoleSpy: MockInstance + beforeEach(() => { + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + }) + + afterEach(() => { + consoleSpy.mockRestore() + }) + + it('returns the error', async () => { + setup({ + owner: { + isCurrentUserPartOfOrg: false, + repository: { + __typename: 'NotFoundError', + message: 'not found', + }, + }, + }) + + const { result } = renderHook( + () => usePull({ provider, owner, repo, pullId: '2510' }), + { wrapper } + ) + + await waitFor(() => result.current.isLoading) + await waitFor(() => !result.current.isLoading) + + await waitFor(() => + expect(result.current.error).toEqual( + expect.objectContaining({ status: 404, data: {} }) + ) + ) + }) + }) + + describe('when there is no pull returned', () => { + let consoleSpy: MockInstance + beforeEach(() => { + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + }) + + afterEach(() => { + consoleSpy.mockRestore() + }) + + it('returns pull null', async () => { + setup({ + owner: { + isCurrentUserPartOfOrg: true, + repository: { + __typename: 'Repository', + defaultBranch: 'main', + private: false, + pull: null, + }, + }, + }) + + const { result } = renderHook( + () => usePull({ provider, owner, repo, pullId: '2510' }), + { wrapper } + ) + + await waitFor(() => result.current.isLoading) + await waitFor(() => !result.current.isLoading) + + await waitFor(() => expect(result.current.data?.pull).toEqual(null)) + }) + }) + + describe('when schema is not valid', () => { + let consoleSpy: MockInstance + beforeEach(() => { + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) + }) + + afterEach(() => { + consoleSpy.mockRestore() + }) + + it('throws an error', async () => { + setup({ + owner: { + isCurrentUserPartOfOrg: true, + repository: { + __typename: 'Repository', + defaultBranch: 'main', + private: false, + pull: { + commits: { + edges: [ + { + node: { + state: 'complete', + commitid: 'fc43199ccde1f21a940aa3d596c711c1c420651f', + message: + 'create component to hold bundle list table for a given pull 2', + }, + }, + ], + }, + }, + }, + }, + }) + + const { result } = renderHook( + () => usePull({ provider, owner, repo, pullId: '2510' }), + { + wrapper, + } + ) + + await waitFor(() => result.current.isLoading) + await waitFor(() => !result.current.isLoading) + + await waitFor(() => + expect(result.current.error).toEqual( + expect.objectContaining({ status: 404, data: {} }) + ) + ) + }) + }) + }) +}) diff --git a/src/services/pull/usePull.tsx b/src/services/pull/usePull.tsx index 4dc85c082a..7e9ace8991 100644 --- a/src/services/pull/usePull.tsx +++ b/src/services/pull/usePull.tsx @@ -322,7 +322,7 @@ export function usePull({ if (!parsedRes.success) { return Promise.reject({ status: 404, - data: null, + data: {}, }) } diff --git a/src/services/pull/usePullBADropdownSummary.spec.tsx b/src/services/pull/usePullBADropdownSummary.test.tsx similarity index 85% rename from src/services/pull/usePullBADropdownSummary.spec.tsx rename to src/services/pull/usePullBADropdownSummary.test.tsx index 5ae8a2bbde..89e7090592 100644 --- a/src/services/pull/usePullBADropdownSummary.spec.tsx +++ b/src/services/pull/usePullBADropdownSummary.test.tsx @@ -1,7 +1,8 @@ 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 { usePullBADropdownSummary } from './usePullBADropdownSummary' @@ -94,17 +95,17 @@ describe('usePullBADropdownSummary', () => { isOwnerNotActivatedError = false, }: SetupArgs = {}) { server.use( - graphql.query('PullBADropdownSummary', (req, res, ctx) => { + graphql.query('PullBADropdownSummary', (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 if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } else { - return res(ctx.status(200), ctx.data(mockPullBASummaryData)) + return HttpResponse.json({ data: mockPullBASummaryData }) } }) ) @@ -170,14 +171,13 @@ describe('usePullBADropdownSummary', () => { }) describe('unsuccessful parse of zod schema', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -206,14 +206,13 @@ describe('usePullBADropdownSummary', () => { }) describe('returns NotFoundError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -242,14 +241,13 @@ describe('usePullBADropdownSummary', () => { }) describe('returns OwnerNotActivatedError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 403', async () => { diff --git a/src/services/pull/usePullBundleComparisonList.spec.tsx b/src/services/pull/usePullBundleComparisonList.test.tsx similarity index 88% rename from src/services/pull/usePullBundleComparisonList.spec.tsx rename to src/services/pull/usePullBundleComparisonList.test.tsx index 815cf7a2a8..55af431606 100644 --- a/src/services/pull/usePullBundleComparisonList.spec.tsx +++ b/src/services/pull/usePullBundleComparisonList.test.tsx @@ -1,7 +1,8 @@ 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 { usePullBundleComparisonList } from './usePullBundleComparisonList' @@ -125,17 +126,17 @@ describe('usePullBundleComparisonList', () => { isOwnerNotActivatedError = false, }: SetupArgs = {}) { server.use( - graphql.query('PullBundleComparisonList', (req, res, ctx) => { + graphql.query('PullBundleComparisonList', (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 if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } else { - return res(ctx.status(200), ctx.data(mockPullBundleListData)) + return HttpResponse.json({ data: mockPullBundleListData }) } }) ) @@ -232,14 +233,13 @@ describe('usePullBundleComparisonList', () => { }) describe('unsuccessful parse of zod schema', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -268,14 +268,13 @@ describe('usePullBundleComparisonList', () => { }) describe('returns NotFoundError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -304,14 +303,13 @@ describe('usePullBundleComparisonList', () => { }) describe('returns OwnerNotActivatedError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 403', async () => { diff --git a/src/services/pull/usePullBundleHeadList.spec.tsx b/src/services/pull/usePullBundleHeadList.test.tsx similarity index 85% rename from src/services/pull/usePullBundleHeadList.spec.tsx rename to src/services/pull/usePullBundleHeadList.test.tsx index 77dcec23df..8e759c06e2 100644 --- a/src/services/pull/usePullBundleHeadList.spec.tsx +++ b/src/services/pull/usePullBundleHeadList.test.tsx @@ -1,7 +1,8 @@ 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 { usePullBundleHeadList } from './usePullBundleHeadList' @@ -86,7 +87,6 @@ beforeAll(() => { }) afterEach(() => { - jest.resetAllMocks() queryClient.clear() server.resetHandlers() }) @@ -110,21 +110,21 @@ describe('usePullBundleHeadList', () => { isNullOwner = false, }: SetupArgs) { server.use( - graphql.query('PullBundleHeadList', (req, res, ctx) => { + graphql.query('PullBundleHeadList', (info) => { if (isNotFoundError) { - return res(ctx.status(200), ctx.data(mockRepoNotFound)) + return HttpResponse.json({ data: mockRepoNotFound }) } else if (isOwnerNotActivatedError) { - return res(ctx.status(200), ctx.data(mockOwnerNotActivated)) + return HttpResponse.json({ data: mockOwnerNotActivated }) } else if (isUnsuccessfulParseError) { - return res(ctx.status(200), ctx.data(mockUnsuccessfulParseError)) + return HttpResponse.json({ data: mockUnsuccessfulParseError }) } else if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } - return res(ctx.status(200), ctx.data(mockPullBundleList)) + return HttpResponse.json({ data: mockPullBundleList }) }), - graphql.query('GetRepoOverview', (req, res, ctx) => { - return res(ctx.status(200), ctx.data(mockRepoOverview)) + graphql.query('GetRepoOverview', (info) => { + return HttpResponse.json({ data: mockRepoOverview }) }) ) } @@ -195,14 +195,13 @@ describe('usePullBundleHeadList', () => { }) describe('returns NotFoundError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -230,14 +229,13 @@ describe('usePullBundleHeadList', () => { }) describe('returns OwnerNotActivatedError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 403', async () => { @@ -265,14 +263,13 @@ describe('usePullBundleHeadList', () => { }) describe('unsuccessful parse of zod schema', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { diff --git a/src/services/pull/usePullCompareTotalsTeam.spec.tsx b/src/services/pull/usePullCompareTotalsTeam.test.tsx similarity index 86% rename from src/services/pull/usePullCompareTotalsTeam.spec.tsx rename to src/services/pull/usePullCompareTotalsTeam.test.tsx index af3e441113..ead0ad0782 100644 --- a/src/services/pull/usePullCompareTotalsTeam.spec.tsx +++ b/src/services/pull/usePullCompareTotalsTeam.test.tsx @@ -1,7 +1,8 @@ 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 { usePullCompareTotalsTeam } from './usePullCompareTotalsTeam' @@ -100,17 +101,17 @@ describe('usePullCompareTotalsTeam', () => { isNullOwner = false, }: SetupArgs) { server.use( - graphql.query('GetPullCompareTotalsTeam', (req, res, ctx) => { + graphql.query('GetPullCompareTotalsTeam', (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 if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } else { - return res(ctx.status(200), ctx.data(mockCompareData)) + return HttpResponse.json({ data: mockCompareData }) } }) ) @@ -180,12 +181,13 @@ describe('usePullCompareTotalsTeam', () => { }) describe('returns NotFound Error __typename', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -211,12 +213,13 @@ describe('usePullCompareTotalsTeam', () => { }) describe('returns OwnerNotActivatedError __typename', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 403', async () => { @@ -242,12 +245,13 @@ describe('usePullCompareTotalsTeam', () => { }) describe('unsuccessful parse of zod schema', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 404', async () => { diff --git a/src/services/pull/usePullComponents.spec.tsx b/src/services/pull/usePullComponents.test.tsx similarity index 83% rename from src/services/pull/usePullComponents.spec.tsx rename to src/services/pull/usePullComponents.test.tsx index 6ee5c735c7..5bcdd9fd9b 100644 --- a/src/services/pull/usePullComponents.spec.tsx +++ b/src/services/pull/usePullComponents.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 { MemoryRouter, Route } from 'react-router-dom' +import { type MockInstance } from 'vitest' import { usePullComponents } from './usePullComponents' @@ -96,17 +97,17 @@ describe('usePullComponents', () => { isOwnerNotActivatedError = false, }: SetupArgs = {}) { server.use( - graphql.query('PullComponentsSelector', (req, res, ctx) => { + graphql.query('PullComponentsSelector', (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 if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } else { - return res(ctx.status(200), ctx.data(mockCompareData)) + return HttpResponse.json({ data: mockCompareData }) } }) ) @@ -150,12 +151,13 @@ describe('usePullComponents', () => { }) describe('unsuccessful parse of zod schema', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -177,14 +179,13 @@ describe('usePullComponents', () => { }) describe('returns NotFoundError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -208,14 +209,13 @@ describe('usePullComponents', () => { }) describe('returns OwnerNotActivatedError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 403', async () => { diff --git a/src/services/pull/usePullCoverageDropdownSummary.spec.tsx b/src/services/pull/usePullCoverageDropdownSummary.test.tsx similarity index 84% rename from src/services/pull/usePullCoverageDropdownSummary.spec.tsx rename to src/services/pull/usePullCoverageDropdownSummary.test.tsx index b943d41a6e..024a822870 100644 --- a/src/services/pull/usePullCoverageDropdownSummary.spec.tsx +++ b/src/services/pull/usePullCoverageDropdownSummary.test.tsx @@ -1,7 +1,8 @@ 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 { usePullCoverageDropdownSummary } from './usePullCoverageDropdownSummary' @@ -87,17 +88,17 @@ describe('usePullCoverageDropdownSummary', () => { isOwnerNotActivatedError = false, }: SetupArgs = {}) { server.use( - graphql.query('PullCoverageDropdownSummary', (req, res, ctx) => { + graphql.query('PullCoverageDropdownSummary', (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 if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } else { - return res(ctx.status(200), ctx.data(mockPullSummaryData)) + return HttpResponse.json({ data: mockPullSummaryData }) } }) ) @@ -156,14 +157,13 @@ describe('usePullCoverageDropdownSummary', () => { }) describe('unsuccessful parse of zod schema', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -192,14 +192,13 @@ describe('usePullCoverageDropdownSummary', () => { }) describe('returns NotFoundError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -228,14 +227,13 @@ describe('usePullCoverageDropdownSummary', () => { }) describe('returns OwnerNotActivatedError __typename', () => { - let oldConsoleError = console.error - + let consoleSpy: MockInstance beforeEach(() => { - console.error = () => null + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - console.error = oldConsoleError + consoleSpy.mockRestore() }) it('throws a 403', async () => { diff --git a/src/services/pull/usePullTeam.spec.tsx b/src/services/pull/usePullTeam.test.tsx similarity index 86% rename from src/services/pull/usePullTeam.spec.tsx rename to src/services/pull/usePullTeam.test.tsx index 54854f36bd..79f781b9db 100644 --- a/src/services/pull/usePullTeam.spec.tsx +++ b/src/services/pull/usePullTeam.test.tsx @@ -1,7 +1,8 @@ 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 { usePullTeam } from './usePullTeam' @@ -108,7 +109,6 @@ beforeAll(() => { }) afterEach(() => { - jest.useRealTimers() queryClient.clear() server.resetHandlers() }) @@ -132,21 +132,21 @@ describe('usePullTeam', () => { isNullOwner = false, }: SetupArgs) { server.use( - graphql.query('GetPullTeam', (req, res, ctx) => { + graphql.query('GetPullTeam', (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 if (isNullOwner) { - return res(ctx.status(200), ctx.data(mockNullOwner)) + return HttpResponse.json({ data: mockNullOwner }) } else { - return res(ctx.status(200), ctx.data(mockPullData)) + return HttpResponse.json({ data: mockPullData }) } }), - graphql.query('GetPullCompareTotalsTeam', (req, res, ctx) => { - return res(ctx.status(200), ctx.data(mockCompareData)) + graphql.query('GetPullCompareTotalsTeam', (info) => { + return HttpResponse.json({ data: mockCompareData }) }) ) } @@ -228,12 +228,13 @@ describe('usePullTeam', () => { }) describe('returns NotFoundError __typename', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -263,12 +264,13 @@ describe('usePullTeam', () => { }) describe('returns OwnerNotActivatedError __typename', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 403', async () => { @@ -298,12 +300,13 @@ describe('usePullTeam', () => { }) describe('unsuccessful parse of zod schema', () => { + let consoleSpy: MockInstance beforeEach(() => { - jest.spyOn(console, 'error') + consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { - jest.resetAllMocks() + consoleSpy.mockRestore() }) it('throws a 404', async () => { @@ -337,17 +340,17 @@ describe('usePullTeam polling', () => { function setup() { let nbCallCompare = 0 server.use( - graphql.query(`GetPullTeam`, (req, res, ctx) => { - return res(ctx.status(200), ctx.data(mockPullData)) + graphql.query(`GetPullTeam`, (info) => { + return HttpResponse.json({ data: mockPullData }) }), - graphql.query(`GetPullCompareTotalsTeam`, (req, res, ctx) => { + graphql.query(`GetPullCompareTotalsTeam`, (info) => { nbCallCompare++ if (nbCallCompare < 9) { - return res(ctx.status(200), ctx.data(mockPullData)) + return HttpResponse.json({ data: mockPullData }) } - return res(ctx.status(200), ctx.data(mockCompareData)) + return HttpResponse.json({ data: mockCompareData }) }) ) } diff --git a/src/services/pull/useSingularImpactedFileComparison.spec.tsx b/src/services/pull/useSingularImpactedFileComparison.test.tsx similarity index 93% rename from src/services/pull/useSingularImpactedFileComparison.spec.tsx rename to src/services/pull/useSingularImpactedFileComparison.test.tsx index 8156d132fb..b0bd3bd141 100644 --- a/src/services/pull/useSingularImpactedFileComparison.spec.tsx +++ b/src/services/pull/useSingularImpactedFileComparison.test.tsx @@ -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 { useSingularImpactedFileComparison } from './useSingularImpactedFileComparison' import { transformImpactedFileData } from './utils' @@ -123,17 +123,17 @@ describe('useSingularImpactedFileComparison', () => { isMissingBaseCommit = false, }) { server.use( - graphql.query('ImpactedFileComparison', (req, res, ctx) => { + graphql.query('ImpactedFileComparison', (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(mockIncorrectResponse)) + return HttpResponse.json({ data: mockIncorrectResponse }) } else if (isMissingBaseCommit) { - return res(ctx.status(200), ctx.data(mockMissingBaseCommitResponse)) + return HttpResponse.json({ data: mockMissingBaseCommitResponse }) } - return res(ctx.status(200), ctx.data(mockResponse)) + return HttpResponse.json({ data: mockResponse }) }) ) } diff --git a/src/services/pull/utils/index.js b/src/services/pull/utils/index.ts similarity index 100% rename from src/services/pull/utils/index.js rename to src/services/pull/utils/index.ts diff --git a/src/services/pull/utils/setFileLabel.js b/src/services/pull/utils/setFileLabel.js deleted file mode 100644 index e2e91e49c0..0000000000 --- a/src/services/pull/utils/setFileLabel.js +++ /dev/null @@ -1,6 +0,0 @@ -export function setFileLabel({ isNewFile, isRenamedFile, isDeletedFile }) { - if (isNewFile) return 'New' - if (isRenamedFile) return 'Renamed' - if (isDeletedFile) return 'Deleted' - return null -} diff --git a/src/services/pull/utils/setFileLabel.spec.js b/src/services/pull/utils/setFileLabel.test.ts similarity index 100% rename from src/services/pull/utils/setFileLabel.spec.js rename to src/services/pull/utils/setFileLabel.test.ts diff --git a/src/services/pull/utils/setFileLabel.ts b/src/services/pull/utils/setFileLabel.ts new file mode 100644 index 0000000000..49bc7b70ed --- /dev/null +++ b/src/services/pull/utils/setFileLabel.ts @@ -0,0 +1,16 @@ +interface SetFileLabel { + isNewFile?: boolean + isRenamedFile?: boolean + isDeletedFile?: boolean +} + +export function setFileLabel({ + isNewFile, + isRenamedFile, + isDeletedFile, +}: SetFileLabel) { + if (isNewFile) return 'New' + if (isRenamedFile) return 'Renamed' + if (isDeletedFile) return 'Deleted' + return null +} diff --git a/src/services/pull/utils/transformImpactedFileData.spec.js b/src/services/pull/utils/transformImpactedFileData.test.js similarity index 100% rename from src/services/pull/utils/transformImpactedFileData.spec.js rename to src/services/pull/utils/transformImpactedFileData.test.js