Skip to content

Commit

Permalink
fix(core): update getPreviewPaths function (#5362)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin authored and ninaandal committed Dec 14, 2023
1 parent 37cc483 commit bd77e0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions packages/sanity/src/core/preview/utils/getPreviewPaths.test.ts
Original file line number Diff line number Diff line change
@@ -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'],
])
})
})
2 changes: 1 addition & 1 deletion packages/sanity/src/core/preview/utils/getPreviewPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bd77e0e

Please sign in to comment.