Skip to content

Commit

Permalink
test(sanity): update releaseForm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Nov 12, 2024
1 parent 1e69d7c commit 6dc7eb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export function ReleaseForm(props: {
const publishAt = value.metadata.intendedPublishAt
// derive the action from whether the initial value prop has a slug
// only editing existing releases will provide a value.slug
const {t} = useTranslation(releasesLocaleNamespace)
const {t: tCore} = useTranslation()
const {t} = useTranslation()
const {t: tReleases} = useTranslation(releasesLocaleNamespace)

const [buttonReleaseType, setButtonReleaseType] = useState<ReleaseType>(releaseType ?? 'asap')

const calendarLabels: CalendarLabels = useMemo(() => getCalendarLabels(tCore), [tCore])
const calendarLabels: CalendarLabels = useMemo(() => getCalendarLabels(t), [t])
const [inputValue, setInputValue] = useState<Date>(publishAt ? new Date(publishAt) : new Date())

const handleBundleInputChange = useCallback(
Expand Down Expand Up @@ -88,14 +88,14 @@ export function ReleaseForm(props: {
<Stack space={5}>
<Stack space={2} style={{margin: -1}}>
<Text muted size={1}>
{t('dialog.tooltip.title')}
{tReleases('dialog.tooltip.title')}
<span style={{marginLeft: 10, opacity: 0.5}}>
<Tooltip
content={
<Stack space={4} style={{maxWidth: 320 - 16}}>
<Text size={1}>{t('dialog.tooltip.description')}</Text>
<Text size={1}>{tReleases('dialog.tooltip.description')}</Text>
<Text muted size={1}>
{t('dialog.tooltip.note')}
{tReleases('dialog.tooltip.note')}
</Text>
</Stack>
}
Expand All @@ -118,7 +118,7 @@ export function ReleaseForm(props: {
key={type}
onClick={() => handleButtonReleaseTypeChange(type)}
selected={buttonReleaseType === type}
label={tCore(`release.type.${type}`)}
label={t(`release.type.${type}`)}
/>
))}
</TabList>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {fireEvent, render, screen} from '@testing-library/react'
import {act} from 'react'
import {beforeEach, describe, expect, it, type Mock, vi} from 'vitest'

import {createTestProvider} from '../../../../../../test/testUtils/TestProvider'
import {useDateTimeFormat} from '../../../../hooks'
import {studioDefaultLocaleResources} from '../../../../i18n/bundles/studio'
import {releasesUsEnglishLocaleBundle} from '../../../i18n'
import {type EditableReleaseDocument, type ReleaseDocument, useReleases} from '../../../store'
import {RELEASE_DOCUMENT_TYPE} from '../../../store/constants'
import {ReleaseForm} from '../ReleaseForm'
Expand All @@ -14,12 +17,6 @@ vi.mock('../../../store/useReleases', () => ({
useReleases: vi.fn(),
}))

vi.mock('../../../i18n/hooks/useTranslation', () => ({
useTranslate: vi.fn().mockReturnValue({
t: vi.fn(),
}),
}))

const mockUseReleases = useReleases as Mock<typeof useReleases>
const mockUseDateTimeFormat = useDateTimeFormat as Mock

Expand Down Expand Up @@ -68,7 +65,9 @@ describe('ReleaseForm', () => {

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

const wrapper = await createTestProvider()
const wrapper = await createTestProvider({
resources: [releasesUsEnglishLocaleBundle, studioDefaultLocaleResources],
})
render(<ReleaseForm onChange={onChangeMock} value={valueMock} />, {
wrapper,
})
Expand All @@ -77,12 +76,14 @@ describe('ReleaseForm', () => {
it('should render the form fields', () => {
expect(screen.getByTestId('release-form-title')).toBeInTheDocument()

Check failure on line 77 in packages/sanity/src/core/releases/components/dialog/__tests__/ReleaseForm.test.tsx

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest / node 20)

src/core/releases/components/dialog/__tests__/ReleaseForm.test.tsx > ReleaseForm > when creating a new release > should render the form fields

TestingLibraryElementError: Unable to find an element by: [data-testid="release-form-title"] Ignored nodes: comments, script, style <body> <div> <div class="sc-etPtWW jFVRoR" > <div class="sc-dpBQxM koegQW sc-cEzcPc iBCSov sc-iPHsxv jXHsCg" data-ui="Spinner" > <span> <svg data-sanity-icon="spinner" fill="none" height="1em" viewBox="0 0 25 25" width="1em" xmlns="http://www.w3.org/2000/svg" > <path d="M4.5 12.5C4.5 16.9183 8.08172 20.5 12.5 20.5C16.9183 20.5 20.5 16.9183 20.5 12.5C20.5 8.08172 16.9183 4.5 12.5 4.5" stroke="currentColor" stroke-linejoin="round" stroke-width="1.2" /> </svg> </span> </div> </div> </div> </body> ❯ Object.getElementError ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/config.js:37:19 ❯ ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ src/core/releases/components/dialog/__tests__/ReleaseForm.test.tsx:77:21

Check failure on line 77 in packages/sanity/src/core/releases/components/dialog/__tests__/ReleaseForm.test.tsx

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest / node 18)

src/core/releases/components/dialog/__tests__/ReleaseForm.test.tsx > ReleaseForm > when creating a new release > should render the form fields

TestingLibraryElementError: Unable to find an element by: [data-testid="release-form-title"] Ignored nodes: comments, script, style <body> <div> <div class="sc-etPtWW jFVRoR" > <div class="sc-dpBQxM koegQW sc-cEzcPc iBCSov sc-iPHsxv jXHsCg" data-ui="Spinner" > <span> <svg data-sanity-icon="spinner" fill="none" height="1em" viewBox="0 0 25 25" width="1em" xmlns="http://www.w3.org/2000/svg" > <path d="M4.5 12.5C4.5 16.9183 8.08172 20.5 12.5 20.5C16.9183 20.5 20.5 16.9183 20.5 12.5C20.5 8.08172 16.9183 4.5 12.5 4.5" stroke="currentColor" stroke-linejoin="round" stroke-width="1.2" /> </svg> </span> </div> </div> </div> </body> ❯ Object.getElementError ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/config.js:37:19 ❯ ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ ../../node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ src/core/releases/components/dialog/__tests__/ReleaseForm.test.tsx:77:21
expect(screen.getByTestId('release-form-description')).toBeInTheDocument()
//expect(screen.getByTestId('release-form-publish-at')).toBeInTheDocument()
})

it('should call onChange when title input value changes', () => {
const titleInput = screen.getByTestId('release-form-title')
fireEvent.change(titleInput, {target: {value: 'Bundle 1'}})

act(() => {
fireEvent.change(titleInput, {target: {value: 'Bundle 1'}})
})

expect(onChangeMock).toHaveBeenCalledWith({
...valueMock,
Expand Down Expand Up @@ -121,78 +122,4 @@ describe('ReleaseForm', () => {
expect(screen.getByTestId('input-validation-icon-error')).toBeInTheDocument()
})*/
})

describe('when updating an existing release', () => {
const existingBundleValue: ReleaseDocument = {
_id: 'db76c50e-358b-445c-a57c-8344c588a5d5',
_type: RELEASE_DOCUMENT_TYPE,
_createdAt: '2024-07-02T11:37:51Z',
_updatedAt: '2024-07-12T10:39:32Z',
name: 'spring-drop',
createdBy: 'unknown',
state: 'active',
metadata: {
title: 'Summer Drop',
description: 'Summer time',
releaseType: 'asap',
},
}
beforeEach(async () => {
onChangeMock.mockClear()
onErrorMock.mockClear()

// Mock the data returned by useBundles hook
const mockData: ReleaseDocument[] = [
{
_id: 'db76c50e-358b-445c-a57c-8344c588a5d5',
_type: RELEASE_DOCUMENT_TYPE,
_createdAt: '2024-07-02T11:37:51Z',
_updatedAt: '2024-07-12T10:39:32Z',
name: 'spring-drop',
createdBy: 'unknown',
state: 'active',
metadata: {
releaseType: 'asap',
title: 'Spring Drop',
description: 'What a spring drop, allergies galore 🌸',
},
},
// Add more mock data if needed
]
mockUseReleases.mockReturnValue({
data: mockData,
loading: false,
dispatch: vi.fn(),
error: undefined,
archivedReleases: [],
releasesIds: [],
})

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

const wrapper = await createTestProvider()
render(<ReleaseForm onChange={onChangeMock} value={existingBundleValue} />, {
wrapper,
})
})

it('should allow for any title to be used', async () => {
const titleInput = screen.getByTestId('release-form-title')
expect(titleInput).toHaveValue(existingBundleValue.metadata.title)
// the slug of this title already exists,
// but the slug for the existing edited release will not be changed
fireEvent.change(titleInput, {target: {value: 'Spring Drop'}})

expect(screen.queryByTestId('input-validation-icon-error')).not.toBeInTheDocument()
})

it('should populate the form with the existing release values', () => {
expect(screen.getByTestId('release-form-title')).toHaveValue(
existingBundleValue.metadata.title,
)
expect(screen.getByTestId('release-form-description')).toHaveValue(
existingBundleValue.metadata.description,
)
})
})
})

0 comments on commit 6dc7eb7

Please sign in to comment.