-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): basic test of
buildTreeEditingState
- Loading branch information
1 parent
1d1315f
commit 8ac9bf3
Showing
1 changed file
with
240 additions
and
0 deletions.
There are no files selected for viewing
240 changes: 240 additions & 0 deletions
240
packages/sanity/src/core/form/studio/tree-editing/__tests__/buildTreeEditingState.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
import {describe, expect, test} from '@jest/globals' | ||
import {Schema} from '@sanity/schema' | ||
import {type Path, type SanityDocumentLike} from 'sanity' | ||
|
||
import {buildTreeEditingState, type TreeEditingState} from '../utils' | ||
|
||
const schema = Schema.compile({ | ||
name: 'default', | ||
types: [ | ||
{ | ||
name: 'testDocument', | ||
title: 'Document', | ||
type: 'document', | ||
fields: [ | ||
{ | ||
type: 'string', | ||
name: 'title', | ||
title: 'Title', | ||
}, | ||
{ | ||
type: 'array', | ||
name: 'array1', | ||
title: 'Array 1', | ||
of: [ | ||
{ | ||
type: 'object', | ||
name: 'array1Object', | ||
title: 'Array 1 object 1', | ||
fields: [ | ||
{ | ||
type: 'string', | ||
name: 'array1Object1String', | ||
title: 'Array 1 Object 1 String', | ||
}, | ||
{ | ||
type: 'array', | ||
name: 'array1Object1Array', | ||
title: 'Array 1 Object 1 Array', | ||
of: [ | ||
{ | ||
type: 'object', | ||
name: 'array1Object1Object', | ||
title: 'Array 1 Object 1 Object', | ||
fields: [ | ||
{ | ||
type: 'string', | ||
name: 'array1Object1ObjectString', | ||
title: 'Array 1 Object 1 Object String', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'array', | ||
name: 'arrayOfPrimitives', | ||
title: 'Array of primitives', | ||
of: [{type: 'string'}], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}) | ||
|
||
describe('tree-editing: buildTreeEditingState', () => { | ||
test('should build tree editing state for a document with an array of objects', () => { | ||
const documentValue: SanityDocumentLike = { | ||
_id: 'testDocument', | ||
_type: 'testDocument', | ||
title: 'Test document', | ||
arrayOfPrimitives: ['string1', 'string2', 'string3'], | ||
array1: [ | ||
{ | ||
_key: 'key1', | ||
_type: 'array1Object', | ||
array1Object1String: 'My string 1', | ||
}, | ||
{ | ||
_key: 'key2', | ||
_type: 'array1Object', | ||
array1Object1String: 'My string 2', | ||
}, | ||
{ | ||
_key: 'key3', | ||
_type: 'array1Object', | ||
array1Object1String: 'My string 3', | ||
array1Object1Array: [ | ||
{ | ||
_key: 'key4', | ||
_type: 'array1Object1Object', | ||
array1Object1ObjectString: 'My string 4', | ||
}, | ||
{ | ||
_key: 'key5', | ||
_type: 'array1Object1Object', | ||
array1Object1ObjectString: 'My string 5', | ||
}, | ||
], | ||
}, | ||
{ | ||
_key: 'key6', | ||
_type: 'array1Object', | ||
array1Object1String: 'My string 6', | ||
}, | ||
{ | ||
_key: 'key7', | ||
_type: 'array1Object', | ||
array1Object1String: 'My string 7', | ||
array1Object1Array: [ | ||
{ | ||
_key: 'key8', | ||
_type: 'array1Object1Object', | ||
array1Object1ObjectString: 'My string 8', | ||
}, | ||
{ | ||
_key: 'key9', | ||
_type: 'array1Object1Object', | ||
array1Object1ObjectString: 'My string 9', | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
const focusPath: Path = ['array1', {_key: 'key1'}] | ||
|
||
const schemaType = schema.get('testDocument') | ||
|
||
const result = buildTreeEditingState({ | ||
documentValue, | ||
focusPath, | ||
schemaType, | ||
}) | ||
|
||
const expectedResult: TreeEditingState = { | ||
breadcrumbs: [ | ||
{ | ||
path: ['array1', {_key: 'key1'}], | ||
title: 'My string 1', | ||
children: [ | ||
{ | ||
path: ['array1', {_key: 'key1'}], | ||
title: 'My string 1', | ||
children: [], | ||
}, | ||
{ | ||
path: ['array1', {_key: 'key2'}], | ||
title: 'My string 2', | ||
children: [], | ||
}, | ||
{ | ||
path: ['array1', {_key: 'key3'}], | ||
title: 'My string 3', | ||
children: [], | ||
}, | ||
{ | ||
path: ['array1', {_key: 'key6'}], | ||
title: 'My string 6', | ||
children: [], | ||
}, | ||
{ | ||
path: ['array1', {_key: 'key7'}], | ||
title: 'My string 7', | ||
children: [], | ||
}, | ||
], | ||
}, | ||
], | ||
menuItems: [ | ||
{ | ||
title: 'My string 1', | ||
path: ['array1', {_key: 'key1'}], | ||
children: [], | ||
}, | ||
{ | ||
title: 'My string 2', | ||
path: ['array1', {_key: 'key2'}], | ||
children: [], | ||
}, | ||
{ | ||
title: 'My string 3', | ||
path: ['array1', {_key: 'key3'}], | ||
children: [ | ||
{ | ||
title: 'Array 1 Object 1 Array', | ||
path: ['array1', {_key: 'key3'}, 'array1Object1Array'], | ||
children: [ | ||
{ | ||
path: ['array1', {_key: 'key3'}, 'array1Object1Array', {_key: 'key4'}], | ||
title: 'My string 4', | ||
children: [], | ||
}, | ||
{ | ||
path: ['array1', {_key: 'key3'}, 'array1Object1Array', {_key: 'key5'}], | ||
title: 'My string 5', | ||
children: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
title: 'My string 6', | ||
path: ['array1', {_key: 'key6'}], | ||
children: [], | ||
}, | ||
{ | ||
title: 'My string 7', | ||
path: ['array1', {_key: 'key7'}], | ||
children: [ | ||
{ | ||
title: 'Array 1 Object 1 Array', | ||
path: ['array1', {_key: 'key7'}, 'array1Object1Array'], | ||
children: [ | ||
{ | ||
path: ['array1', {_key: 'key7'}, 'array1Object1Array', {_key: 'key8'}], | ||
title: 'My string 8', | ||
children: [], | ||
}, | ||
{ | ||
path: ['array1', {_key: 'key7'}, 'array1Object1Array', {_key: 'key9'}], | ||
title: 'My string 9', | ||
children: [], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
relativePath: ['array1', {_key: 'key1'}], | ||
rootTitle: 'Array 1', | ||
} | ||
|
||
expect(result).toStrictEqual(expectedResult) | ||
}) | ||
}) |