Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov committed Oct 29, 2024
1 parent 5568558 commit ef31a9f
Showing 1 changed file with 84 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}))

Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -72,7 +70,7 @@ class ResizeObserverMock {
}
global.window.ResizeObserver = ResizeObserverMock

const baseMock = (impactedFile) => {
const baseMock = (impactedFile: ImpactedFileType | null) => {
if (!impactedFile) {
return { owner: null }
}
Expand Down Expand Up @@ -179,7 +177,7 @@ const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, suspense: true } },
})

const wrapper = ({ children }) => (
const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
<QueryClientProvider client={queryClient}>
<MemoryRouter
initialEntries={[
Expand All @@ -193,23 +191,32 @@ const wrapper = ({ children }) => (
</QueryClientProvider>
)

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(() => ({
Expand All @@ -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) })
Expand Down Expand Up @@ -336,7 +339,7 @@ describe('CommitFileDiff', () => {
})

describe('when there is no data', () => {
let consoleSpy
let consoleSpy: MockInstance

beforeAll(() => {
consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
Expand Down Expand Up @@ -367,161 +370,92 @@ describe('CommitFileDiff', () => {
})

describe('code renderer', () => {
describe('feature flag is true', () => {
it('renders the text area', async () => {
setup({ featureFlag: true })
render(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })

const codeDisplayOverlay = await screen.findByTestId(
'virtual-file-renderer-overlay'
)
it('renders the text area', async () => {
setup({})
render(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })
it('renders hit count icon', async () => {
setup({})
render(<CommitFileDiff path={'flag1/file.js'} />, { 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(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })
it('does not render information on the code renderer', async () => {
setup({ impactedFile })
render(<CommitFileDiff path={'flag1/file.js'} />, { 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()
})
})
})
Expand Down

0 comments on commit ef31a9f

Please sign in to comment.