From fcccaa2e4bafc488afdd03b4efe9586e16a3232e Mon Sep 17 00:00:00 2001 From: pedrobonamin Date: Wed, 4 Sep 2024 17:10:19 +0200 Subject: [PATCH] chore(core): update DocumentPerspectiveMenu test --- .../DocumentPerspectiveMenu.test.tsx | 65 +++++++++---------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/__tests__/DocumentPerspectiveMenu.test.tsx b/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/__tests__/DocumentPerspectiveMenu.test.tsx index 176f26e6eed9..987fe8588c0a 100644 --- a/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/__tests__/DocumentPerspectiveMenu.test.tsx +++ b/packages/sanity/src/structure/panes/document/documentPanel/header/perspective/__tests__/DocumentPerspectiveMenu.test.tsx @@ -1,7 +1,8 @@ import {beforeEach, describe, expect, it, jest} from '@jest/globals' -import {fireEvent, render, screen} from '@testing-library/react' +import {render, screen} from '@testing-library/react' +import {type HTMLProps} from 'react' import {type BundleDocument, usePerspective} from 'sanity' -import {useRouter} from 'sanity/router' +import {type IntentLinkProps} from 'sanity/router' import {createTestProvider} from '../../../../../../../../test/testUtils/TestProvider' import {type DocumentPaneContextValue} from '../../../../DocumentPaneContext' @@ -22,24 +23,34 @@ jest.mock('sanity', () => { } }) -jest.mock('sanity/router', () => ({ - useRouter: jest.fn().mockReturnValue({ - navigateIntent: jest.fn(), - stickyParams: {}, - }), - route: { - create: jest.fn(), - }, - IntentLink: jest.fn(), -})) +const IntentLinkMock = (props: IntentLinkProps & HTMLProps) => { + const {params = {}, intent, ...rest} = props + const stringParams = params + ? Object.entries(params) + .map(([key, value]) => `${key}=${value}`) + .join('&') + : '' + + return +} + +jest.mock('sanity/router', () => { + return { + useRouter: jest.fn().mockReturnValue({ + stickyParams: {}, + }), + route: { + create: jest.fn(), + }, + IntentLink: IntentLinkMock, + } +}) jest.mock('../../../../useDocumentPane') const mockUseDocumentPane = useDocumentPane as jest.MockedFunction< () => Partial > -const mockUseRouter = useRouter as jest.MockedFunction -const navigateIntent = mockUseRouter().navigateIntent as jest.Mock const mockUsePerspective = usePerspective as jest.Mock @@ -90,45 +101,33 @@ describe('DocumentPerspectiveMenu', () => { const wrapper = await createTestProvider() render(, {wrapper}) - expect(screen.getByTestId('button-document-release')).toBeInTheDocument() + const linkButton = screen.getByRole('link', {name: 'Spring Drop'}) + expect(linkButton).toBeInTheDocument() + expect(linkButton).toHaveAttribute('href', '/intent/release/id=spring-drop') + expect(linkButton).toHaveTextContent('Spring Drop') }) it('should not render the bundle badge if the document does not exist in the bundle', async () => { - mockUseDocumentPane.mockReturnValue({ - existsInBundle: false, - }) - - const wrapper = await createTestProvider() - render(, {wrapper}) - - expect(screen.queryByTestId('button-document-release')).toBeNull() - }) - - it('should navigate to the release intent when the bundle badge is clicked', async () => { mockUseDocumentPane.mockReturnValue({ documentVersions: [ { + _id: 'spring-drop', title: 'Spring Drop', hue: 'magenta', icon: 'heart-filled', _type: 'release', authorId: '', - _id: 'spring-drop', _createdAt: '', _updatedAt: '', _rev: '', }, ], - existsInBundle: true, + existsInBundle: false, }) const wrapper = await createTestProvider() render(, {wrapper}) - expect(screen.queryByTestId('button-document-release')).toBeInTheDocument() - fireEvent.click(screen.getByTestId('button-document-release')) - - expect(navigateIntent).toHaveBeenCalledTimes(1) - expect(navigateIntent).toHaveBeenCalledWith('release', {id: 'spring-drop'}) + expect(screen.queryByRole('link', {name: 'Spring Drop'})).toBeNull() }) })