diff --git a/packages/sanity/src/core/preview/utils/getPreviewPaths.test.ts b/packages/sanity/src/core/preview/utils/getPreviewPaths.test.ts new file mode 100644 index 00000000000..0e0e91ffa4e --- /dev/null +++ b/packages/sanity/src/core/preview/utils/getPreviewPaths.test.ts @@ -0,0 +1,35 @@ +import {SchemaType} from '@sanity/types' +import {getPreviewPaths} from './getPreviewPaths' + +const preview: SchemaType['preview'] = { + select: { + title: 'name', + awards: 'awards', + role: 'role', + relatedAuthors: 'relatedAuthors', + lastUpdated: '_updatedAt', + media: 'image', + }, +} + +describe('getPreviewPaths', () => { + it('Should return undefined if no selection is provided', () => { + const paths = getPreviewPaths({ + select: undefined, + }) + expect(paths).toBeUndefined() + }) + it('should return the default preview paths', () => { + const paths = getPreviewPaths(preview) + expect(paths).toEqual([ + ['name'], + ['awards'], + ['role'], + ['relatedAuthors'], + ['_updatedAt'], + ['image'], + ['_createdAt'], + ['_updatedAt'], + ]) + }) +}) diff --git a/packages/sanity/src/core/preview/utils/getPreviewPaths.ts b/packages/sanity/src/core/preview/utils/getPreviewPaths.ts index 10e1982f1b2..b5b860b9e57 100644 --- a/packages/sanity/src/core/preview/utils/getPreviewPaths.ts +++ b/packages/sanity/src/core/preview/utils/getPreviewPaths.ts @@ -11,7 +11,7 @@ export function getPreviewPaths(preview: SchemaType['preview']): PreviewPath[] | // Transform the selection dot-notation paths into array paths. // Example: ['object.title', 'name'] => [['object', 'title'], ['name']] - const paths = Object.keys(selection).map((value) => String(value).split('.')) || [] + const paths = Object.values(selection).map((value) => String(value).split('.')) || [] // Return the paths with the default preview paths appended. return paths.concat(DEFAULT_PREVIEW_PATHS)