Skip to content

Commit

Permalink
test(sanity): migrate tests to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Oct 15, 2024
1 parent d298ee0 commit ed00b56
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 260 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect, test} from '@jest/globals'
import {concat, from, lastValueFrom, of, share, timer} from 'rxjs'
import {concatMap, delay, mergeMap, take, toArray} from 'rxjs/operators'
import {expect, test} from 'vitest'

import {shareReplayLatest} from './shareReplayLatest'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen, within} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {act} from 'react'
import {type BundleDocument, useBundles} from 'sanity'
import {beforeEach, describe, expect, it, vi} from 'vitest'

import {createTestProvider} from '../../../../../test/testUtils/TestProvider'
import {Button} from '../../../../ui-components'
import {usePerspective} from '../../hooks/usePerspective'
import {LATEST} from '../../util/const'
import {ReleasesMenu} from '../ReleasesMenu'

jest.mock('../../hooks/usePerspective', () => ({
usePerspective: jest.fn().mockReturnValue({
vi.mock('../../hooks/usePerspective', () => ({
usePerspective: vi.fn().mockReturnValue({
currentGlobalBundle: {},
setPerspective: jest.fn(),
setPerspective: vi.fn(),
}),
}))

jest.mock('../../util/util', () => ({
isDraftOrPublished: jest.fn(),
vi.mock('../../util/util', () => ({
isDraftOrPublished: vi.fn(),
}))

jest.mock('../../../store/bundles/useBundles', () => ({
useBundles: jest.fn().mockReturnValue({deletedBundles: {}}),
vi.mock('../../../store/bundles/useBundles', () => ({
useBundles: vi.fn().mockReturnValue({deletedBundles: {}}),
}))

const mockUseBundles = useBundles as jest.Mock<typeof useBundles>
const mockUseBundles = useBundles as Mock<typeof useBundles>

describe('ReleasesMenu', () => {
const mockUsePerspective = usePerspective as jest.Mock
const mockUsePerspective = usePerspective as Mock
const ButtonTest = <Button text="Button Test" />
const mockBundles: BundleDocument[] = [
{
Expand Down Expand Up @@ -123,7 +123,7 @@ describe('ReleasesMenu', () => {
it('should render latest bundle menu item as selected when currentGlobalBundle is LATEST', async () => {
mockUsePerspective.mockReturnValue({
currentGlobalBundle: LATEST,
setPerspective: jest.fn(),
setPerspective: vi.fn(),
})

const wrapper = await createTestProvider()
Expand All @@ -142,7 +142,7 @@ describe('ReleasesMenu', () => {
it('should render bundle as selected when currentGlobalBundle is that bundle', async () => {
mockUsePerspective.mockReturnValue({
currentGlobalBundle: mockBundles[0],
setPerspective: jest.fn(),
setPerspective: vi.fn(),
})

const wrapper = await createTestProvider()
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('ReleasesMenu', () => {
})

it('should call setPerspective when a bundle menu item is clicked', async () => {
const setPerspective = jest.fn()
const setPerspective = vi.fn()
mockUsePerspective.mockReturnValue({
currentGlobalBundle: LATEST,
setPerspective,
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('ReleasesMenu', () => {

it('should not show deleted bundles when not included in the list', async () => {
mockUseBundles.mockReturnValue({
dispatch: jest.fn(),
dispatch: vi.fn(),
loading: false,
data: [],
deletedBundles: {
Expand All @@ -238,7 +238,7 @@ describe('ReleasesMenu', () => {

it('should show deleted bundles that are included in the list', async () => {
mockUseBundles.mockReturnValue({
dispatch: jest.fn(),
dispatch: vi.fn(),
loading: false,
data: [],
deletedBundles: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen, waitFor, within} from '@testing-library/react'
import {type BundleDocument, useBundles} from 'sanity'
import {afterEach, beforeEach, describe, expect, it, type Mock, vi} from 'vitest'

import {createTestProvider} from '../../../../../../test/testUtils/TestProvider'
import {useBundleOperations} from '../../../../store/bundles/useBundleOperations'
import {usePerspective} from '../../../hooks/usePerspective'
import {ReleaseDetailsDialog} from '../ReleaseDetailsDialog'

/*jest.mock('../../../../../core/hooks/useDateTimeFormat', () => ({
useDateTimeFormat: jest.fn(),
/*vi.mock('../../../../../core/hooks/useDateTimeFormat', () => ({
useDateTimeFormat: vi.fn(),
}))*/

jest.mock('../../../../store/bundles', () => ({
useBundles: jest.fn(),
vi.mock('../../../../store/bundles', () => ({
useBundles: vi.fn(),
}))

jest.mock('../../../../store/bundles/useBundleOperations', () => ({
useBundleOperations: jest.fn().mockReturnValue({
createBundle: jest.fn(),
updateBundle: jest.fn(),
vi.mock('../../../../store/bundles/useBundleOperations', () => ({
useBundleOperations: vi.fn().mockReturnValue({
createBundle: vi.fn(),
updateBundle: vi.fn(),
}),
}))

jest.mock('../../../hooks/usePerspective', () => ({
usePerspective: jest.fn().mockReturnValue({
setPerspective: jest.fn(),
vi.mock('../../../hooks/usePerspective', () => ({
usePerspective: vi.fn().mockReturnValue({
setPerspective: vi.fn(),
}),
}))

const mockUseBundleStore = useBundles as jest.Mock<typeof useBundles>
//const mockUseDateTimeFormat = useDateTimeFormat as jest.Mock
const mockUseBundleStore = useBundles as Mock<typeof useBundles>
//const mockUseDateTimeFormat = useDateTimeFormat as Mock

describe('ReleaseDetailsDialog', () => {
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
})
afterEach(() => {
vi.clearAllMocks()
})

describe('when creating a new bundle', () => {
const onCancelMock = jest.fn()
const onSubmitMock = jest.fn()
const onCancelMock = vi.fn()
const onSubmitMock = vi.fn()

beforeEach(async () => {
onCancelMock.mockClear()
Expand All @@ -47,12 +50,12 @@ describe('ReleaseDetailsDialog', () => {
mockUseBundleStore.mockReturnValue({
data: [],
loading: true,
dispatch: jest.fn(),
dispatch: vi.fn(),
error: undefined,
deletedBundles: {},
})

//mockUseDateTimeFormat.mockReturnValue({format: jest.fn().mockReturnValue('Mocked date')})
//mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})

const wrapper = await createTestProvider()
render(<ReleaseDetailsDialog onCancel={onCancelMock} onSubmit={onSubmitMock} />, {wrapper})
Expand Down Expand Up @@ -96,8 +99,8 @@ describe('ReleaseDetailsDialog', () => {
})

describe('when updating an existing bundle', () => {
const onCancelMock = jest.fn()
const onSubmitMock = jest.fn()
const onCancelMock = vi.fn()
const onSubmitMock = vi.fn()
const existingBundleValue: BundleDocument = {
_id: 'existing-bundle',
_type: 'release',
Expand All @@ -118,12 +121,12 @@ describe('ReleaseDetailsDialog', () => {
mockUseBundleStore.mockReturnValue({
data: [],
loading: true,
dispatch: jest.fn(),
dispatch: vi.fn(),
error: undefined,
deletedBundles: {},
})

//mockUseDateTimeFormat.mockReturnValue({format: jest.fn().mockReturnValue('Mocked date')})
//mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})

const wrapper = await createTestProvider()
render(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen} from '@testing-library/react'
import {type BundleDocument, type FormBundleDocument, useDateTimeFormat} from 'sanity'
import {beforeEach, describe, expect, it, type Mock, vi} from 'vitest'

import {createTestProvider} from '../../../../../../test/testUtils/TestProvider'
import {useBundles} from '../../../../store/bundles'
import {ReleaseForm} from '../ReleaseForm'

jest.mock('../../../../../core/hooks/useDateTimeFormat', () => ({
useDateTimeFormat: jest.fn(),
vi.mock('../../../../../core/hooks/useDateTimeFormat', () => ({
useDateTimeFormat: vi.fn(),
}))

jest.mock('../../../../store/bundles', () => ({
useBundles: jest.fn(),
vi.mock('../../../../store/bundles', () => ({
useBundles: vi.fn(),
}))

const mockUseBundleStore = useBundles as jest.Mock<typeof useBundles>
const mockUseDateTimeFormat = useDateTimeFormat as jest.Mock
const mockUseBundleStore = useBundles as Mock<typeof useBundles>
const mockUseDateTimeFormat = useDateTimeFormat as Mock

describe('ReleaseForm', () => {
const onChangeMock = jest.fn()
const onErrorMock = jest.fn()
const onChangeMock = vi.fn()
const onErrorMock = vi.fn()
const valueMock: FormBundleDocument = {
_id: 'very-random',
_type: 'release',
Expand Down Expand Up @@ -54,12 +54,12 @@ describe('ReleaseForm', () => {
mockUseBundleStore.mockReturnValue({
data: mockData,
loading: false,
dispatch: jest.fn(),
dispatch: vi.fn(),
error: undefined,
deletedBundles: {},
})

mockUseDateTimeFormat.mockReturnValue({format: jest.fn().mockReturnValue('Mocked date')})
mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})

const wrapper = await createTestProvider()
render(<ReleaseForm onChange={onChangeMock} value={valueMock} />, {
Expand Down Expand Up @@ -139,11 +139,11 @@ describe('ReleaseForm', () => {
mockUseBundleStore.mockReturnValue({
data: mockData,
loading: false,
dispatch: jest.fn(),
dispatch: vi.fn(),
deletedBundles: {} as Record<string, BundleDocument>,
})

mockUseDateTimeFormat.mockReturnValue({format: jest.fn().mockReturnValue('Mocked date')})
mockUseDateTimeFormat.mockReturnValue({format: vi.fn().mockReturnValue('Mocked date')})

const wrapper = await createTestProvider()
render(<ReleaseForm onChange={onChangeMock} value={existingBundleValue} />, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen} from '@testing-library/react'
import {beforeEach, describe, expect, it, vi} from 'vitest'

import {createTestProvider} from '../../../../../../test/testUtils/TestProvider'
import {
ReleaseIconEditorPicker,
type ReleaseIconEditorPickerValue,
} from '../ReleaseIconEditorPicker'

jest.mock('sanity', () => ({
useTranslation: jest.fn().mockReturnValue({t: jest.fn()}),
}))
vi.mock('sanity', () => {
return {
SANITY_VERSION: '0.0.0',
useTranslation: vi.fn().mockReturnValue({t: vi.fn()}),
}
})

describe('BundleIconEditorPicker', () => {
const onChangeMock = jest.fn()
const onChangeMock = vi.fn()
const valueMock: ReleaseIconEditorPickerValue = {
hue: 'gray',
icon: 'cube',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import {describe, expect, it, jest} from '@jest/globals'
import {renderHook} from '@testing-library/react'
import {of} from 'rxjs'
import {describe, expect, it, type Mock, vi} from 'vitest'

import {type DocumentPreviewStore} from '../../../preview'
import {type BundleDocument} from '../../../store'
import {type PublishedId} from '../../../util/draftUtils'
import {useDocumentVersions} from '../useDocumentVersions'

// Mock the entire module
jest.mock('../../../studio/source')
vi.mock('../../../studio/source')

jest.mock('sanity', () => {
const actual = jest.requireActual('sanity')
return Object.assign({}, actual, {
useClient: jest.fn(),
useBundles: jest.fn(() => ({data: {}})),
getPublishedId: jest.fn(),
useDocumentPreviewStore: jest.fn(),
})
})
vi.mock('sanity', async (importOriginal) => ({
...(await importOriginal()),
useClient: vi.fn(),
useBundles: vi.fn(() => ({data: {}})),
getPublishedId: vi.fn(),
useDocumentPreviewStore: vi.fn(),
}))

const mockBundles = [
{
Expand Down Expand Up @@ -57,25 +55,23 @@ async function setupMocks({
}) {
const sanityModule = await import('sanity')

const useBundles = sanityModule.useBundles as jest.Mock<typeof sanityModule.useBundles>
const useDocumentPreviewStore = sanityModule.useDocumentPreviewStore as jest.Mock<
const useBundles = sanityModule.useBundles as Mock<typeof sanityModule.useBundles>
const useDocumentPreviewStore = sanityModule.useDocumentPreviewStore as Mock<
typeof sanityModule.useDocumentPreviewStore
>
const getPublishedId = sanityModule.getPublishedId as jest.Mock<
typeof sanityModule.getPublishedId
>
const getPublishedId = sanityModule.getPublishedId as Mock<typeof sanityModule.getPublishedId>

useBundles.mockReturnValue({
data: bundles,
loading: false,
dispatch: jest.fn(),
dispatch: vi.fn(),
deletedBundles: {},
})

getPublishedId.mockReturnValue('document-1' as PublishedId)

useDocumentPreviewStore.mockReturnValue({
unstable_observeDocumentIdSet: jest
unstable_observeDocumentIdSet: vi
.fn<DocumentPreviewStore['unstable_observeDocumentIdSet']>()
.mockReturnValue(of({status: 'connected', documentIds: versionIds})),
} as unknown as DocumentPreviewStore)
Expand Down
Loading

0 comments on commit ed00b56

Please sign in to comment.