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

fix: move createWrapper to test package, update name -> slug #7177

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -5,9 +5,9 @@ import userEvent from '@testing-library/user-event'
import {act} from 'react'
import {type BundleDocument} from 'sanity'

import {createWrapper} from '../../../../../test/testUtils/createWrapper'
import {usePerspective} from '../../hooks/usePerspective'
import {LATEST} from '../../util/const'
import {createWrapper} from '../../util/tests/createWrapper'
import {BundleMenu} from '../BundleMenu'

jest.mock('../../hooks/usePerspective', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ 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 {createWrapper} from '../../../../../../test/testUtils/createWrapper'
import {useBundleOperations} from '../../../../store/bundles/useBundleOperations'
import {usePerspective} from '../../../hooks/usePerspective'
import {createWrapper} from '../../../util/tests/createWrapper'
import {BundleDetailsDialog} from '../BundleDetailsDialog'

/*jest.mock('../../../../../core/hooks/useDateTimeFormat', () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen} from '@testing-library/react'
import {type BundleDocument, useDateTimeFormat} from 'sanity'

import {createWrapper} from '../../../../../../test/testUtils/createWrapper'
import {useBundles} from '../../../../store/bundles'
import {createWrapper} from '../../../util/tests/createWrapper'
import {BundleForm} from '../BundleForm'

jest.mock('../../../../../core/hooks/useDateTimeFormat', () => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen} from '@testing-library/react'

import {createWrapper} from '../../../util/tests/createWrapper'
import {createWrapper} from '../../../../../../test/testUtils/createWrapper'
import {BundleIconEditorPicker, type BundleIconEditorPickerValue} from '../BundleIconEditorPicker'

describe('BundleIconEditorPicker', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {fireEvent, render, screen} from '@testing-library/react'
import {type BundleDocument, useBundles} from 'sanity'

import {createWrapper} from '../../../../../../test/testUtils/createWrapper'
import {useBundleOperations} from '../../../../store/bundles/useBundleOperations'
import {usePerspective} from '../../../hooks/usePerspective'
import {CreateBundleDialog} from '../CreateBundleDialog'

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

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

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

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

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

describe('CreateBundleDialog', () => {
const onCancelMock = jest.fn()
const onCreateMock = jest.fn()

beforeEach(async () => {
onCancelMock.mockClear()
onCreateMock.mockClear()

mockUseBundleStore.mockReturnValue({
data: [],
loading: true,
dispatch: jest.fn(),
})

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

const wrapper = await createWrapper()
render(<CreateBundleDialog onCancel={onCancelMock} onCreate={onCreateMock} />, {wrapper})
})

it('should render the dialog', () => {
expect(screen.getByRole('dialog')).toBeInTheDocument()
})

it('should call onCancel when dialog is closed', () => {
fireEvent.click(screen.getByRole('button', {name: /close/i}))

expect(onCancelMock).toHaveBeenCalled()
})

it('should call createBundle, setPerspective, and onCreate when form is submitted with a valid slug', async () => {
const value: Partial<BundleDocument> = {
slug: 'bundle-1',
title: 'Bundle 1',
hue: 'gray',
icon: 'cube',
//publishAt: undefined,
}

const titleInput = screen.getByTestId('bundle-form-title')
fireEvent.change(titleInput, {target: {value: value.title}})

const submitButton = screen.getByTestId('create-release-button')
fireEvent.click(submitButton)

await expect(useBundleOperations().createBundle).toHaveBeenCalledWith(value)

expect(usePerspective().setPerspective).toHaveBeenCalledWith(value.slug)
expect(onCreateMock).toHaveBeenCalled()
})
})
1 change: 0 additions & 1 deletion packages/sanity/src/core/bundles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './components'
export * from './hooks'
export * from './util/const'
export * from './util/dummyGetters'
export * from './util/tests/createWrapper'
1 change: 0 additions & 1 deletion packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export {
BundleActions,
BundleBadge,
BundleMenu,
createWrapper,
getAllVersionsOfDocument,
getBundleSlug,
LATEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {render, screen} from '@testing-library/react'
import {ColorSchemeProvider, UserColorManagerProvider} from 'sanity'

import {queryByDataUi} from '../../../../../../test/setup/customQueries'
import {createWrapper} from '../../../../bundles'
import {createWrapper} from '../../../../../../test/testUtils/createWrapper'
import {useObserveDocument} from '../../../../preview/useObserveDocument'
import {releasesUsEnglishLocaleBundle} from '../../../i18n'
import {useDocumentPreviewValues} from '../documentTable/useDocumentPreviewValues'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {fireEvent, render, screen, waitFor, within} from '@testing-library/react
import {useRouter} from 'sanity/router'

import {queryByDataUi} from '../../../../../../test/setup/customQueries'
import {createWrapper} from '../../../../bundles/util/tests/createWrapper'
import {createWrapper} from '../../../../../../test/testUtils/createWrapper'
import {useBundles} from '../../../../store'
import {type BundleDocument} from '../../../../store/bundles/types'
import {releasesUsEnglishLocaleBundle} from '../../../i18n'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function DocumentPerspectiveMenu(props: {documentId: string}): JSX.Elemen
const {currentGlobalBundle} = usePerspective()
const bundles = useMemo(() => data ?? [], [data])

const existsInBundle = bundles.some((bundle) => bundle.slug === getBundleSlug(documentId))
const existsInBundle = getBundleSlug(documentId) === currentGlobalBundle?.slug
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the misaligned logic here surfaced by the test by any chance? 👀
Least if it was you can say that all the toil was worth it...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to admit I don't recall 🥲

const {title, hue, icon, slug} = currentGlobalBundle

const router = useRouter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {fireEvent, render, screen} from '@testing-library/react'
import {
BundleBadge,
type BundleDocument,
createWrapper,
getAllVersionsOfDocument,
getBundleSlug,
type SanityClient,
Expand All @@ -13,7 +12,8 @@ import {
} from 'sanity'
import {useRouter} from 'sanity/router'

import {DocumentPerspectiveMenu} from './DocumentPerspectiveMenu'
import {DocumentPerspectiveMenu} from '../../src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu'
import {createWrapper} from '../testUtils/createWrapper'

type getBundleSlugType = (documentId: string) => string
type GetAllVersionsOfDocumentType = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {studioTheme, ThemeProvider} from '@sanity/ui'
import {type ReactNode} from 'react'

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

/**
* @internal
* @hidden
*/
export const createWrapper = async (options?: TestProviderOptions) => {
export async function createWrapper(options?: TestProviderOptions) {
const TestProvider = await createTestProvider(options)
return function Wrapper({children}: {children: ReactNode}): JSX.Element {
return (
Expand Down
Loading