From ef31a9f65b2871b820ca0ee75a659b6563fb8fff Mon Sep 17 00:00:00 2001 From: nicholas-codecov Date: Tue, 29 Oct 2024 15:09:27 -0300 Subject: [PATCH] update tests --- ...eDiff.test.jsx => CommitFileDiff.test.tsx} | 234 +++++++----------- 1 file changed, 84 insertions(+), 150 deletions(-) rename src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/{CommitFileDiff.test.jsx => CommitFileDiff.test.tsx} (62%) diff --git a/src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.jsx b/src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.tsx similarity index 62% rename from src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.jsx rename to src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.tsx index bec1d93308..2d208900f4 100644 --- a/src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.jsx +++ b/src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.tsx @@ -4,13 +4,15 @@ import { graphql, HttpResponse } from 'msw' import { setupServer } from 'msw/node' import { Suspense } from 'react' import { MemoryRouter, Route } from 'react-router-dom' +import { type MockInstance } from 'vitest' + +import { ImpactedFileType } from 'services/commit' import CommitFileDiff from './CommitFileDiff' const mocks = vi.hoisted(() => ({ - useFlags: vi.fn(), useScrollToLine: vi.fn(), - withProfiler: (component) => component, + withProfiler: (component: any) => component, captureMessage: vi.fn(), })) @@ -22,10 +24,6 @@ vi.mock('ui/CodeRenderer/hooks', () => { } }) -vi.mock('shared/featureFlags', () => ({ - useFlags: mocks.useFlags, -})) - vi.mock('@sentry/react', () => { const originalModule = vi.importActual('@sentry/react') return { @@ -46,9 +44,9 @@ window.scrollTo = scrollToMock window.scrollY = 100 class ResizeObserverMock { - callback = (x) => null + callback = (x: any) => null - constructor(callback) { + constructor(callback: any) { this.callback = callback } @@ -72,7 +70,7 @@ class ResizeObserverMock { } global.window.ResizeObserver = ResizeObserverMock -const baseMock = (impactedFile) => { +const baseMock = (impactedFile: ImpactedFileType | null) => { if (!impactedFile) { return { owner: null } } @@ -179,7 +177,7 @@ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, suspense: true } }, }) -const wrapper = ({ children }) => ( +const wrapper: React.FC = ({ children }) => ( ( ) -beforeAll(() => server.listen()) +beforeAll(() => { + server.listen() +}) + afterEach(() => { queryClient.clear() server.resetHandlers() }) -afterAll(() => server.close()) + +afterAll(() => { + server.close() +}) + +interface SetupArgs { + impactedFile?: ImpactedFileType | null + bundleAnalysisEnabled?: boolean +} describe('CommitFileDiff', () => { function setup( { impactedFile = mockImpactedFile, bundleAnalysisEnabled = false, - featureFlag = false, - } = { + }: SetupArgs = { impactedFile: mockImpactedFile, bundleAnalysisEnabled: false, - featureFlag: false, } ) { mocks.useScrollToLine.mockImplementation(() => ({ @@ -218,10 +225,6 @@ describe('CommitFileDiff', () => { targeted: false, })) - mocks.useFlags.mockImplementation(() => ({ - virtualDiffRenderer: featureFlag, - })) - server.use( graphql.query('ImpactedFileComparedWithParent', (info) => { return HttpResponse.json({ data: baseMock(impactedFile) }) @@ -336,7 +339,7 @@ describe('CommitFileDiff', () => { }) describe('when there is no data', () => { - let consoleSpy + let consoleSpy: MockInstance beforeAll(() => { consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) @@ -367,161 +370,92 @@ describe('CommitFileDiff', () => { }) describe('code renderer', () => { - describe('feature flag is true', () => { - it('renders the text area', async () => { - setup({ featureFlag: true }) - render(, { wrapper }) - - const textArea = await screen.findByTestId( - 'virtual-file-renderer-text-area' - ) - expect(textArea).toBeInTheDocument() - - const calculator = await within(textArea).findByText(/Calculator/) - expect(calculator).toBeInTheDocument() - - const value = await within(textArea).findByText(/value/) - expect(value).toBeInTheDocument() - - const calcMode = await within(textArea).findByText(/calcMode/) - expect(calcMode).toBeInTheDocument() - }) - - it('renders the lines of a segment', async () => { - setup({ featureFlag: true }) - render(, { wrapper }) - - const codeDisplayOverlay = await screen.findByTestId( - 'virtual-file-renderer-overlay' - ) + it('renders the text area', async () => { + setup({}) + render(, { wrapper }) - const calculator = - await within(codeDisplayOverlay).findByText(/Calculator/) - expect(calculator).toBeInTheDocument() + const textArea = await screen.findByTestId( + 'virtual-file-renderer-text-area' + ) + expect(textArea).toBeInTheDocument() - const value = await within(codeDisplayOverlay).findByText(/value/) - expect(value).toBeInTheDocument() + const calculator = await within(textArea).findByText(/Calculator/) + expect(calculator).toBeInTheDocument() - const calcMode = await within(codeDisplayOverlay).findByText(/calcMode/) - expect(calcMode).toBeInTheDocument() - }) + const value = await within(textArea).findByText(/value/) + expect(value).toBeInTheDocument() - describe('rendering hit icon', () => { - describe('there are no ignored ids', () => { - it('renders hit count icon', async () => { - setup({ featureFlag: true }) - render(, { wrapper }) + const calcMode = await within(textArea).findByText(/calcMode/) + expect(calcMode).toBeInTheDocument() + }) - const hitCount = await screen.findByText('5') - expect(hitCount).toBeInTheDocument() - }) - }) + it('renders the lines of a segment', async () => { + setup({}) + render(, { wrapper }) - describe('there are ignored ids', () => { - beforeEach(() => { - queryClient.setQueryData(['IgnoredUploadIds'], [0]) - }) + const codeDisplayOverlay = await screen.findByTestId( + 'virtual-file-renderer-overlay' + ) - it('renders hit count icon', async () => { - setup({ featureFlag: true }) - render(, { wrapper }) + const calculator = + await within(codeDisplayOverlay).findByText(/Calculator/) + expect(calculator).toBeInTheDocument() - const hitCount = await screen.findByText('4') - expect(hitCount).toBeInTheDocument() - }) - }) - }) + const value = await within(codeDisplayOverlay).findByText(/value/) + expect(value).toBeInTheDocument() - describe('when segment is an empty array', () => { - const impactedFile = { - ...mockImpactedFile, - isCriticalFile: false, - headName: 'flag1/file.js', - segments: { - results: [], - }, - } + const calcMode = await within(codeDisplayOverlay).findByText(/calcMode/) + expect(calcMode).toBeInTheDocument() + }) - it('does not render information on the code renderer', async () => { - setup({ impactedFile, featureFlag: true }) + describe('rendering hit icon', () => { + describe('there are no ignored ids', () => { + it('renders hit count icon', async () => { + setup({}) render(, { wrapper }) - await waitFor(() => queryClient.isFetching) - await waitFor(() => !queryClient.isFetching) - - const unexpectedChange = screen.queryByText(/Unexpected Changes/i) - expect(unexpectedChange).not.toBeInTheDocument() - - const diffLine = screen.queryByText('fv-diff-line') - expect(diffLine).not.toBeInTheDocument() + const hitCount = await screen.findByText('5') + expect(hitCount).toBeInTheDocument() }) }) - }) - - describe('feature flag is false', () => { - it('renders the lines of a segment', async () => { - setup() - render(, { wrapper }) - const calculator = await screen.findByText(/Calculator/) - expect(calculator).toBeInTheDocument() - - const value = await screen.findByText(/value/) - expect(value).toBeInTheDocument() - - const calcMode = await screen.findByText(/calcMode/) - expect(calcMode).toBeInTheDocument() - }) - - describe('rendering hit icon', () => { - describe('there are no ignored ids', () => { - it('renders hit count icon', async () => { - setup() - render(, { wrapper }) - - const hitCount = await screen.findByText('5') - expect(hitCount).toBeInTheDocument() - }) + describe('there are ignored ids', () => { + beforeEach(() => { + queryClient.setQueryData(['IgnoredUploadIds'], [0]) }) - describe('there are ignored ids', () => { - beforeEach(() => { - queryClient.setQueryData(['IgnoredUploadIds'], [0]) - }) - - it('renders hit count icon', async () => { - setup() - render(, { wrapper }) + it('renders hit count icon', async () => { + setup({}) + render(, { wrapper }) - const hitCount = await screen.findByText('4') - expect(hitCount).toBeInTheDocument() - }) + const hitCount = await screen.findByText('4') + expect(hitCount).toBeInTheDocument() }) }) + }) - describe('when segment is an empty array', () => { - const impactedFile = { - ...mockImpactedFile, - isCriticalFile: false, - headName: 'flag1/file.js', - segments: { - results: [], - }, - } + describe('when segment is an empty array', () => { + const impactedFile = { + ...mockImpactedFile, + isCriticalFile: false, + headName: 'flag1/file.js', + segments: { + results: [], + }, + } - it('does not render information on the code renderer', async () => { - setup({ impactedFile }) - render(, { wrapper }) + it('does not render information on the code renderer', async () => { + setup({ impactedFile }) + render(, { wrapper }) - await waitFor(() => queryClient.isFetching) - await waitFor(() => !queryClient.isFetching) + await waitFor(() => queryClient.isFetching) + await waitFor(() => !queryClient.isFetching) - const unexpectedChange = screen.queryByText(/Unexpected Changes/i) - expect(unexpectedChange).not.toBeInTheDocument() + const unexpectedChange = screen.queryByText(/Unexpected Changes/i) + expect(unexpectedChange).not.toBeInTheDocument() - const diffLine = screen.queryByText('fv-diff-line') - expect(diffLine).not.toBeInTheDocument() - }) + const diffLine = screen.queryByText('fv-diff-line') + expect(diffLine).not.toBeInTheDocument() }) }) })