Skip to content

Commit

Permalink
chore: Update second group of services tests (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov authored Oct 1, 2024
1 parent 9d3c84b commit 93330a5
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 158 deletions.
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 })
}
})
)
Expand Down Expand Up @@ -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(
Expand All @@ -204,6 +215,7 @@ describe('useComparisonForCommitAndParent', () => {
)
)
})

it('can return not found error', async () => {
setup({ isNotFoundError: true })
const { result } = renderHook(
Expand All @@ -229,6 +241,7 @@ describe('useComparisonForCommitAndParent', () => {
)
)
})

it('can return owner not activated error', async () => {
setup({ isOwnerNotActivatedError: true })
const { result } = renderHook(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import config from 'config'

import { useStaticNavLinks } from './useStaticNavLinks'

jest.mock('config')
vi.mock('config')

describe('useStaticNavLinks', () => {
const view = renderHook(() => useStaticNavLinks(), {
Expand All @@ -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`
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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')
})

Expand Down Expand Up @@ -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({
Expand Down
92 changes: 0 additions & 92 deletions src/services/tracking/pendo.spec.js

This file was deleted.

125 changes: 125 additions & 0 deletions src/services/tracking/pendo.test.jsx
Original file line number Diff line number Diff line change
@@ -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)
})
})
Loading

0 comments on commit 93330a5

Please sign in to comment.