Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update shared/utils tests to Vitest #3228

Merged
merged 23 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { waitFor } from '@testing-library/react'
import { type MockInstance } from 'vitest'

import {
cancelAnimationTimeout,
requestAnimationTimeout,
} from './animationFrameUtils'

describe('requestAnimationTimeout', () => {
let requestAnimationFrameSpy: jest.SpyInstance
let dateNowSpy: jest.SpyInstance
let requestAnimationFrameSpy: MockInstance
let dateNowSpy: MockInstance

beforeEach(() => {
requestAnimationFrameSpy = jest.spyOn(window, 'requestAnimationFrame')
dateNowSpy = jest.spyOn(Date, 'now')
requestAnimationFrameSpy = vi.spyOn(window, 'requestAnimationFrame')
dateNowSpy = vi.spyOn(Date, 'now')
})

afterEach(() => {
requestAnimationFrameSpy.mockReset()
dateNowSpy.mockReset()
jest.clearAllMocks()
vi.clearAllMocks()
})

it('should call the callback after the specified delay', async () => {
Expand All @@ -31,7 +32,7 @@ describe('requestAnimationTimeout', () => {
return 1
})

const callback = jest.fn()
const callback = vi.fn()
requestAnimationTimeout(callback, 1000)

await waitFor(() => expect(callback).toHaveBeenCalled())
Expand All @@ -48,7 +49,7 @@ describe('requestAnimationTimeout', () => {
return 1
})

const callback = jest.fn()
const callback = vi.fn()
requestAnimationTimeout(callback, 1000)

await waitFor(() =>
Expand All @@ -67,7 +68,7 @@ describe('requestAnimationTimeout', () => {
return 1
})

const callback = jest.fn()
const callback = vi.fn()
requestAnimationTimeout(callback, 1000)

await waitFor(() =>
Expand All @@ -77,10 +78,10 @@ describe('requestAnimationTimeout', () => {
})

describe('cancelAnimationTimeout', () => {
let cancelAnimationFrameSpy: jest.SpyInstance
let cancelAnimationFrameSpy: MockInstance

beforeEach(() => {
cancelAnimationFrameSpy = jest.spyOn(window, 'cancelAnimationFrame')
cancelAnimationFrameSpy = vi.spyOn(window, 'cancelAnimationFrame')
})

it('should call window.cancelAnimationFrame', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
useProPlans,
} from './billing'

jest.mock('shared/featureFlags')
vi.mock('shared/featureFlags')

function getPlans() {
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('useDateFormatted and formatTimeToNow functions', () => {

describe('when called with a iso date', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2022-09-01'))
vi.useFakeTimers().setSystemTime(new Date('2022-09-01'))
setup('2020-09-08T10:45:06Z')
})

Expand All @@ -40,7 +40,7 @@ describe('useDateFormatted and formatTimeToNow functions', () => {

describe('when called with an alternative date format', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2022-09-01'))
vi.useFakeTimers().setSystemTime(new Date('2022-09-01'))
setup('2020-09-08T10:45:06Z', 'MMMM yyyy')
})

Expand All @@ -56,7 +56,7 @@ describe('useDateFormatted and formatTimeToNow functions', () => {

describe('when called with a unix timestamp', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2022-09-01'))
vi.useFakeTimers().setSystemTime(new Date('2022-09-01'))
setup(1595270468)
})

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ describe('chartQuery', () => {

describe('legacyRepoCoverageQuery', () => {
function setup(props) {
jest.useFakeTimers()
jest.setSystemTime(new Date('2022/01/01'))
vi.useFakeTimers()
vi.setSystemTime(new Date('2022/01/01'))

return legacyRepoCoverageQuery(props)
}
afterAll(() => jest.useRealTimers())
afterAll(() => vi.useRealTimers())

describe('static params', () => {
it('aggFunction', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ describe('loginProviderImage', () => {
describe('valid value is passed in', () => {
it('returns string', () => {
const data = loginProviderImage(LOGIN_PROVIDER_NAMES.gh)
expect(data).toBe('github-icon.svg')
expect(data).toMatch(/github-icon.svg/)
})
})

describe('also fetches dark mode images', () => {
it('returns string', () => {
const data = loginProviderImage(LOGIN_PROVIDER_NAMES.gh, true)
expect(data).toBe('github-icon-white.svg')
expect(data).toMatch(/github-icon-white.svg/)
})
})

describe('invalid value is passed in', () => {
describe('value is not in the object', () => {
it('returns undefined', () => {
const data = loginProviderImage('random value')
expect(data).toBe(undefined)
expect(data).toBeUndefined()
})
})

describe('passed value is undefined', () => {
it('returns undefined', () => {
const data = loginProviderImage()
expect(data).toBe(undefined)
expect(data).toBeUndefined()
})
})
})

describe('is dark mode', () => {
it('uses dark mode image', () => {
const data = loginProviderImage(LOGIN_PROVIDER_NAMES.gh, true)
expect(data).toBe('github-icon-white.svg')
expect(data).toMatch(/github-icon-white.svg/)
})
})
})
Expand All @@ -49,22 +49,22 @@ describe('loginProviderToName', () => {
describe('valid value is passed', () => {
it('returns the provider name', () => {
const data = loginProviderToName(LOGIN_PROVIDER_SHORT_NAMES.gh)
expect(data).toBe('Github')
expect(data).toMatch(/Github/)
})
})

describe('invalid value is passed in', () => {
describe('value is not in object', () => {
it('returns undefined', () => {
const data = loginProviderToName('blah')
expect(data).toBe(undefined)
expect(data).toBeUndefined()
})
})

describe('value is undefined', () => {
it('returns undefined', () => {
const data = loginProviderToName()
expect(data).toBe(undefined)
expect(data).toBeUndefined()
})
})
})
Expand All @@ -74,22 +74,22 @@ describe('loginProviderToShortName', () => {
describe('valid value is passed', () => {
it('returns the provider name', () => {
const data = loginProviderToShortName(LOGIN_PROVIDER_NAMES.github)
expect(data).toBe('gh')
expect(data).toMatch('gh')
})
})

describe('invalid value is passed in', () => {
describe('value is not in object', () => {
it('returns undefined', () => {
const data = loginProviderToShortName('blah')
expect(data).toBe(undefined)
expect(data).toBeUndefined()
})
})

describe('value is undefined', () => {
it('returns undefined', () => {
const data = loginProviderToShortName()
expect(data).toBe(undefined)
expect(data).toBeUndefined()
})
})
})
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as Sentry from '@sentry/react'

import { prismLanguageMapper } from './prismLanguageMapper'

jest.mock('@sentry/react', () => {
const originalModule = jest.requireActual('@sentry/react')
vi.mock('@sentry/react', async () => {
const originalModule = await vi.importActual('@sentry/react')
return {
...originalModule,
captureMessage: jest.fn(),
captureMessage: vi.fn(),
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
providerToName,
} from './provider'

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

describe('providerToName', () => {
describe('when called with gh', () => {
Expand Down Expand Up @@ -88,32 +88,32 @@ describe('providerToName', () => {
describe('providerImage', () => {
describe('when called for Github', () => {
it('returns correct logo url', () => {
expect(providerImage('Github')).toEqual('github-icon.svg')
expect(providerImage('Github')).toMatch(/github-icon.svg/)
})
})
describe('when called for Gitlab', () => {
it('returns correct logo url', () => {
expect(providerImage('Gitlab')).toEqual('gitlab-icon.svg')
expect(providerImage('Gitlab')).toMatch(/gitlab-icon.svg/)
})
})
describe('when called for BitBucket', () => {
it('returns correct logo url', () => {
expect(providerImage('BitBucket')).toEqual('bitbucket-icon.svg')
expect(providerImage('BitBucket')).toMatch(/bitbucket-icon.svg/)
})
})
describe('when called for Github Enterprise', () => {
it('returns correct logo url', () => {
expect(providerImage('github_enterprise')).toEqual('github-icon.svg')
expect(providerImage('github_enterprise')).toMatch(/github-icon.svg/)
})
})
describe('when called for Gitlab Enterprise', () => {
it('returns correct logo url', () => {
expect(providerImage('gitlab_enterprise')).toEqual('gitlab-icon.svg')
expect(providerImage('gitlab_enterprise')).toMatch(/gitlab-icon.svg/)
})
})
describe('when called for BitBucket Server', () => {
it('returns correct logo url', () => {
expect(providerImage('bitbucket_server')).toEqual('bitbucket-icon.svg')
expect(providerImage('bitbucket_server')).toMatch(/bitbucket-icon.svg/)
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ describe('analyticsQuery', () => {

describe('createTimeSeriesQueryVars', () => {
beforeEach(() => {
jest.useFakeTimers()
jest.setSystemTime(new Date('2022/01/01'))
vi.useFakeTimers()
vi.setSystemTime(new Date('2022/01/01'))
})

afterAll(() => jest.useRealTimers())
afterAll(() => vi.useRealTimers())

describe('dynamic params', () => {
it('sets the start date', () => {
Expand Down
File renamed without changes.
File renamed without changes.
Loading