Skip to content

Commit

Permalink
refactor(core): move some documentPerspective components from structu…
Browse files Browse the repository at this point in the history
…re to core
  • Loading branch information
RitaDias committed Nov 7, 2024
1 parent 2d3951a commit d30cda9
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 102 deletions.
1 change: 1 addition & 0 deletions packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
useReleaseOperations,
useReleases,
useVersionOperations,
VersionChip,
versionDocumentExists,
} from './releases'
export * from './scheduledPublishing'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from 'sanity'
import {styled} from 'styled-components'

import {Button, Popover, Tooltip} from '../../../../../../ui-components'
import {Button, Popover, Tooltip} from '../../../../ui-components'
import {VersionContextMenu} from './contextMenu/VersionContextMenu'
import {CreateReleaseDialog} from './dialog/CreateReleaseDialog'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {AddIcon, CalendarIcon, CopyIcon, TrashIcon} from '@sanity/icons'
import {Menu, MenuDivider, Spinner, Stack} from '@sanity/ui'
import {memo} from 'react'
import {isPublishedId, type ReleaseDocument, useTranslation} from 'sanity'
import {IntentLink} from 'sanity/router'
import {styled} from 'styled-components'

import {MenuGroup, MenuItem} from '../../../../../../../ui-components'
import {MenuGroup} from '../../../../../ui-components/menuGroup/MenuGroup'
import {MenuItem} from '../../../../../ui-components/menuItem/MenuItem'
import {useTranslation} from '../../../../i18n/hooks/useTranslation'
import {isPublishedId} from '../../../../util/draftUtils'
import {type ReleaseDocument} from '../../../store/types'
import {VersionContextMenuItem} from './VersionContextMenuItem'

const ReleasesList = styled(Stack)`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {LockIcon} from '@sanity/icons'
import {Flex, Stack, Text} from '@sanity/ui'
import {memo} from 'react'
import {
getReleaseTone,
ReleaseAvatar,
type ReleaseDocument,
useDateTimeFormat,
useTranslation,
} from 'sanity'

import {useDateTimeFormat} from '../../../../hooks/useDateTimeFormat'
import {useTranslation} from '../../../../i18n'
import {type ReleaseDocument} from '../../../store/types'
import {getReleaseTone} from '../../../util/getReleaseTone'
import {ReleaseAvatar} from '../../ReleaseAvatar'

export const VersionContextMenuItem = memo(function VersionContextMenuItem(props: {
release: ReleaseDocument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,9 @@ import {fireEvent, render, screen, waitFor} from '@testing-library/react'
import * as sanity from 'sanity'
import {describe, expect, it, vi} from 'vitest'

import {createTestProvider} from '../../../../../../../../../test/testUtils/TestProvider'
import {createTestProvider} from '../../../../../../../test/testUtils/TestProvider'
import {VersionContextMenu} from '../VersionContextMenu'

// Mock necessary hooks and components
vi.mock('sanity', async (importOriginal) => {
return {
...(await importOriginal()),
useTelemetry: vi.fn(),
useDocumentOperation: vi.fn().mockReturnValue({createVersion: {execute: vi.fn()}}),
useDocumentStore: vi.fn().mockReturnValue({pair: {operationEvents: vi.fn()}}),
usePerspective: vi.fn().mockReturnValue({setPerspective: vi.fn()}),
SANITY_VERSION: 'test',
isPublishedId: vi.fn(),
getPublishedId: vi.fn(),
useDateTimeFormat: vi.fn(),
ReleaseAvatar: () => <div>ReleaseAvatar</div>,
getReleaseTone: vi.fn(),
}
})

/**
* @todo change this once improvements have been done to test mocking
* there is currently some limitations with importing translations
* in order to make sure that the translations work on tests,
* you need to mock it from the relative path vs doing it in the main mock
*/
vi.mock('../../../../../../../core/i18n/hooks/useTranslation')

vi.mock('sanity/router', () => ({
IntentLink: ({children}: {children: React.ReactNode}) => <div>{children}</div>,
route: {
Expand All @@ -49,8 +24,6 @@ describe('VersionContextMenu', () => {
createdBy: 'safsd',
metadata: {
title: 'Release 1',
hue: 'gray',
icon: 'string',
releaseType: 'asap',
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {render, screen} from '@testing-library/react'
import {type ReleaseDocument, type ReleaseType} from 'sanity'
import {beforeEach, describe, expect, it, vi} from 'vitest'

import {createTestProvider} from '../../../../../../../../../test/testUtils/TestProvider'
import {createTestProvider} from '../../../../../../../test/testUtils/TestProvider'
import {type ReleaseDocument, type ReleaseType} from '../../../../store/types'
import {VersionContextMenuItem} from '../VersionContextMenuItem'

const mockRelease: ReleaseDocument = {
Expand All @@ -17,35 +17,9 @@ const mockRelease: ReleaseDocument = {
title: 'Test Release',
releaseType: 'scheduled',
intendedPublishAt: '2023-10-01T10:00:00Z',
hue: 'gray',
icon: 'string',
},
}

vi.mock('sanity', async (importOriginal) => ({
...(await importOriginal()),
useDateTimeFormat: vi.fn(() => ({
format: (date: Date) => date.toLocaleString(),
})),
ReleaseAvatar: () => <div data-testid="release-avatar" />,
getReleaseTone: vi.fn(),
SANITY_VERSION: 'test',

/**
* @todo
* is there no better way of mocking this?? */
useTranslation: vi.fn().mockReturnValue({
t: vi.fn().mockImplementation((key: string) => {
const translations: Record<string, string> = {
'release.chip.tooltip.unknown-date': 'Unknown date',
'release.type.asap': 'ASAP',
'release.type.undecided': 'Undecided',
}
return translations[key]
}),
}),
}))

describe('VersionContextMenuItem', () => {
beforeEach(() => {
vi.clearAllMocks()
Expand All @@ -62,7 +36,7 @@ describe('VersionContextMenuItem', () => {
const scheduledRelease = {...mockRelease, releaseType: 'scheduled' as ReleaseType}

render(<VersionContextMenuItem release={scheduledRelease} />, {wrapper})
expect(screen.getByText('10/1/2023, 3:00:00 AM')).toBeInTheDocument()
expect(screen.getByText('Oct 1, 2023, 3:00 AM')).toBeInTheDocument()
})

it('renders release type as ASAP', async () => {
Expand Down Expand Up @@ -102,6 +76,6 @@ describe('VersionContextMenuItem', () => {
const wrapper = await createTestProvider()

render(<VersionContextMenuItem release={mockRelease} />, {wrapper})
expect(screen.getByTestId('release-avatar')).toBeInTheDocument()
expect(screen.getByTestId('release-avatar-primary')).toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {useTelemetry} from '@sanity/telemetry/react'
import {type BadgeTone, Box, Flex, Text, useToast} from '@sanity/ui'
import {useCallback, useState} from 'react'
import {
CreatedRelease,
createReleaseId,
DEFAULT_RELEASE_TYPE,
type EditableReleaseDocument,
LoadingBlock,
Preview,
ReleaseAvatar,
ReleaseForm,
useReleaseOperations,
useSchema,
useTranslation,
} from 'sanity'

import {Dialog} from '../../../../../../../ui-components'
import {Dialog} from '../../../../../ui-components/dialog/Dialog'
import {LoadingBlock} from '../../../../components/loadingBlock/LoadingBlock'
import {useSchema} from '../../../../hooks/useSchema'
import {useTranslation} from '../../../../i18n/hooks/useTranslation'
import {Preview} from '../../../../preview/components/Preview'
import {CreatedRelease} from '../../../__telemetry__/releases.telemetry'
import {type EditableReleaseDocument} from '../../../store/types'
import {useReleaseOperations} from '../../../store/useReleaseOperations'
import {DEFAULT_RELEASE_TYPE} from '../../../util/const'
import {createReleaseId} from '../../../util/createReleaseId'
import {ReleaseForm} from '../../dialog/ReleaseForm'
import {ReleaseAvatar} from '../../ReleaseAvatar'

export function CreateReleaseDialog(props: {
onClose: () => void
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/releases/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './dialog/DiscardVersionDialog'
export * from './dialog/ReleaseForm'
export * from './documentHeader/VersionChip'
export * from './ReleaseAvatar'
export * from './ReleaseBadge'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {useToast} from '@sanity/ui'

import {Translate, useTranslation} from '../../i18n'
import {AddedVersion} from '../__telemetry__/releases.telemetry'
import {useReleaseOperations} from '../index'
import {useReleaseOperations} from '../store/useReleaseOperations'
import {getCreateVersionOrigin} from '../util/util'
import {usePerspective} from './usePerspective'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
usePerspective,
useReleases,
useTranslation,
VersionChip,
versionDocumentExists,
} from 'sanity'

import {useDocumentPane} from '../../../useDocumentPane'
import {VersionChip} from './VersionChip'

type FilterReleases = {
notCurrentReleases: ReleaseDocument[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ vi.mock('sanity', async (importOriginal) => {
...(await importOriginal()),
unstable_useValuePreview: vi.fn(),
useDocumentVersions: vi.fn(),
useDateTimeFormat: vi.fn().mockReturnValue({format: vi.fn()}),
useReleases: vi.fn(),
usePerspective: vi.fn(() => ({perspective: undefined})),
useVersionOperations: vi.fn(() => ({})),
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ vi.mock('sanity', async (importOriginal) => ({
setPerspective: vi.fn(),
}),
useReleases: vi.fn().mockReturnValue({data: [], loading: false}),
useVersionOperations: vi.fn().mockReturnValue({}),
versionDocumentExists: vi.fn().mockReturnValue(true),
Translate: vi.fn(),
/**
* @todo
* is there no better way of mocking this?? */
useTranslation: vi.fn().mockReturnValue({
t: vi.fn().mockImplementation((key: string) => {
const translations: Record<string, string> = {
'release.chip.published': 'Published',
'release.chip.draft': 'Draft',
}
return translations[key]
}),
}),
}))

vi.mock('sanity/router', () => {
Expand Down Expand Up @@ -90,8 +75,9 @@ describe('DocumentPerspectiveList', () => {
excludedPerspectives: [],
isPerspectiveExcluded: vi.fn().mockReturnValue(false),
perspective: undefined,
setPerspectiveFromRelease: vi.fn(),
toggleExcludedPerspective: vi.fn(),
setPerspectiveFromReleaseDocumentId: vi.fn(),
setPerspectiveFromReleaseId: vi.fn(),
})

mockUseDocumentPane.mockReturnValue({
Expand Down

0 comments on commit d30cda9

Please sign in to comment.