From a6c11b0c3bd7b7d31b5501fc66d394f0fe255f43 Mon Sep 17 00:00:00 2001 From: RitaDias Date: Fri, 19 Jul 2024 12:01:23 +0200 Subject: [PATCH] fix: move createWrapper to test package, update name -> slug (#7177) * fix: issue with slugs and condition * fix: issue with badge with global bundle + tests * chore: update test imports * test: update createWrapper import * refactor: update dummygetters to reflect corel --- .../components/__tests__/BundleMenu.test.tsx | 2 +- .../__tests__/BundleDetailsDialog.test.tsx | 2 +- .../dialog/__tests__/BundleForm.test.tsx | 2 +- .../__tests__/BundleIconEditorPicker.test.tsx | 2 +- .../__tests__/CreateBundleDialog.test.tsx | 83 +++++++++++++++++++ packages/sanity/src/core/bundles/index.ts | 1 - packages/sanity/src/core/index.ts | 1 - .../detail/__tests__/ReleaseReview.test.tsx | 2 +- .../__tests__/ReleasesOverview.test.tsx | 2 +- .../perspective/DocumentPerspectiveMenu.tsx | 2 +- .../DocumentPerspectiveMenu.test.tsx | 4 +- .../testUtils}/createWrapper.tsx | 7 +- 12 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx rename packages/sanity/{src/structure/panes/document/documentPanel/header/perspective => test/structure}/DocumentPerspectiveMenu.test.tsx (96%) rename packages/sanity/{src/core/bundles/util/tests => test/testUtils}/createWrapper.tsx (68%) diff --git a/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx b/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx index 4e94a4eb6cb3..3e62bd3dd7a5 100644 --- a/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx +++ b/packages/sanity/src/core/bundles/components/__tests__/BundleMenu.test.tsx @@ -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', () => ({ diff --git a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleDetailsDialog.test.tsx b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleDetailsDialog.test.tsx index 19a7de957ede..1f310c27e6a7 100644 --- a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleDetailsDialog.test.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleDetailsDialog.test.tsx @@ -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', () => ({ diff --git a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx index 0828fe651865..6bf396a7376e 100644 --- a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleForm.test.tsx @@ -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', () => ({ diff --git a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleIconEditorPicker.test.tsx b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleIconEditorPicker.test.tsx index 3d2c8513cc43..8e75e35ecc0a 100644 --- a/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleIconEditorPicker.test.tsx +++ b/packages/sanity/src/core/bundles/components/dialog/__tests__/BundleIconEditorPicker.test.tsx @@ -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', () => { diff --git a/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx b/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx new file mode 100644 index 000000000000..29c8371aa1df --- /dev/null +++ b/packages/sanity/src/core/bundles/components/dialog/__tests__/CreateBundleDialog.test.tsx @@ -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 +//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(, {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 = { + 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() + }) +}) diff --git a/packages/sanity/src/core/bundles/index.ts b/packages/sanity/src/core/bundles/index.ts index 0a7341092e7c..f4174d634d25 100644 --- a/packages/sanity/src/core/bundles/index.ts +++ b/packages/sanity/src/core/bundles/index.ts @@ -2,4 +2,3 @@ export * from './components' export * from './hooks' export * from './util/const' export * from './util/dummyGetters' -export * from './util/tests/createWrapper' diff --git a/packages/sanity/src/core/index.ts b/packages/sanity/src/core/index.ts index 06cc369f1b08..269783647c6d 100644 --- a/packages/sanity/src/core/index.ts +++ b/packages/sanity/src/core/index.ts @@ -2,7 +2,6 @@ export { BundleActions, BundleBadge, BundleMenu, - createWrapper, getAllVersionsOfDocument, getBundleSlug, LATEST, diff --git a/packages/sanity/src/core/releases/tool/detail/__tests__/ReleaseReview.test.tsx b/packages/sanity/src/core/releases/tool/detail/__tests__/ReleaseReview.test.tsx index 4bc0d063525d..a04cbe02c19e 100644 --- a/packages/sanity/src/core/releases/tool/detail/__tests__/ReleaseReview.test.tsx +++ b/packages/sanity/src/core/releases/tool/detail/__tests__/ReleaseReview.test.tsx @@ -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' diff --git a/packages/sanity/src/core/releases/tool/overview/__tests__/ReleasesOverview.test.tsx b/packages/sanity/src/core/releases/tool/overview/__tests__/ReleasesOverview.test.tsx index c433df4238b7..2b6720b2a306 100644 --- a/packages/sanity/src/core/releases/tool/overview/__tests__/ReleasesOverview.test.tsx +++ b/packages/sanity/src/core/releases/tool/overview/__tests__/ReleasesOverview.test.tsx @@ -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' diff --git a/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.tsx b/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.tsx index 53bdee5527c3..6bb90c7f5971 100644 --- a/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.tsx +++ b/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.tsx @@ -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 const {title, hue, icon, slug} = currentGlobalBundle const router = useRouter() diff --git a/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.test.tsx b/packages/sanity/test/structure/DocumentPerspectiveMenu.test.tsx similarity index 96% rename from packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.test.tsx rename to packages/sanity/test/structure/DocumentPerspectiveMenu.test.tsx index 4c40aec44cc1..4292953e8d20 100644 --- a/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/DocumentPerspectiveMenu.test.tsx +++ b/packages/sanity/test/structure/DocumentPerspectiveMenu.test.tsx @@ -3,7 +3,6 @@ import {fireEvent, render, screen} from '@testing-library/react' import { BundleBadge, type BundleDocument, - createWrapper, getAllVersionsOfDocument, getBundleSlug, type SanityClient, @@ -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 = ( diff --git a/packages/sanity/src/core/bundles/util/tests/createWrapper.tsx b/packages/sanity/test/testUtils/createWrapper.tsx similarity index 68% rename from packages/sanity/src/core/bundles/util/tests/createWrapper.tsx rename to packages/sanity/test/testUtils/createWrapper.tsx index f190aee7cb20..e6d65206d85d 100644 --- a/packages/sanity/src/core/bundles/util/tests/createWrapper.tsx +++ b/packages/sanity/test/testUtils/createWrapper.tsx @@ -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 (