@@ -35,7 +35,7 @@ import {
3535 TestTreeProviderLite ,
3636 validateUsageError ,
3737} from "../../utils.js" ;
38- import { getViewForForkedBranch , hydrate } from "../utils.js" ;
38+ import { describeHydration , getViewForForkedBranch , hydrate } from "../utils.js" ;
3939import { brand , type areSafelyAssignable , type requireTrue } from "../../../util/index.js" ;
4040
4141import {
@@ -165,39 +165,70 @@ describe("treeNodeApi", () => {
165165 } ) ;
166166 } ) ;
167167
168- it ( "key" , ( ) => {
169- class Child extends schema . object ( "Child" , {
170- x : Point ,
171- y : schema . optional ( Point , { key : "stable-y" } ) ,
172- } ) { }
173- const Root = schema . array ( Child ) ;
174- const config = new TreeViewConfiguration ( { schema : Root } ) ;
175- const view = getView ( config ) ;
176- view . initialize ( [
177- { x : { } , y : undefined } ,
178- { x : { } , y : { } } ,
179- ] ) ;
180- const { root } = view ;
181- assert . equal ( Tree . key ( root ) , rootFieldKey ) ;
182- assert . equal ( Tree . key ( root [ 0 ] ) , 0 ) ;
183- assert . equal ( Tree . key ( root [ 0 ] . x ) , "x" ) ;
184- assert . equal ( Tree . key ( root [ 1 ] ) , 1 ) ;
185- assert . equal ( Tree . key ( root [ 1 ] . x ) , "x" ) ;
186- assert ( root [ 1 ] . y !== undefined ) ;
187- assert . equal ( Tree . key ( root [ 1 ] . y ) , "y" ) ;
188- } ) ;
168+ describeHydration ( "upward path" , ( init ) => {
169+ for ( const [ name , keyApi ] of [
170+ [ "key" , ( n : TreeNode ) : string | undefined | number => Tree . key ( n ) ] ,
171+ [ "key2" , ( n : TreeNode ) : string | undefined | number => TreeAlpha . key2 ( n ) ] ,
172+ ] as const ) {
173+ it ( name , ( ) => {
174+ class Child extends schema . object ( "Child" , {
175+ x : Point ,
176+ y : schema . optional ( Point , { key : "stable-y" } ) ,
177+ } ) { }
178+ class Root extends schema . array ( "Root" , Child ) { }
179+ const root = init ( Root , [
180+ { x : { } , y : undefined } ,
181+ { x : { } , y : { } } ,
182+ ] ) ;
189183
190- it ( "parent" , ( ) => {
191- class Child extends schema . object ( "Child" , { x : Point } ) { }
192- const Root = schema . array ( Child ) ;
193- const config = new TreeViewConfiguration ( { schema : Root } ) ;
194- const view = getView ( config ) ;
195- view . initialize ( [ { x : { } } , { x : { } } ] ) ;
196- const { root } = view ;
197- assert . equal ( Tree . parent ( root ) , undefined ) ;
198- assert . equal ( Tree . parent ( root [ 0 ] ) , root ) ;
199- assert . equal ( Tree . parent ( root [ 1 ] ) , root ) ;
200- assert . equal ( Tree . parent ( root [ 1 ] . x ) , root [ 1 ] ) ;
184+ // This is this how we handle root keys.
185+ // Seems odd for detached fields other than root to have `rootFieldKey` key though.
186+ // Exactly which key is given in this case is undocumented, it could change in the future.
187+ // TreeAlpha.key2 just gives undefined, which is documented.
188+ const rootKey = name === "key" ? rootFieldKey : undefined ;
189+
190+ assert . equal ( keyApi ( root ) , rootKey ) ;
191+ assert . equal ( keyApi ( root [ 0 ] ) , 0 ) ;
192+ assert . equal ( keyApi ( root [ 0 ] . x ) , "x" ) ;
193+ assert . equal ( keyApi ( root [ 1 ] ) , 1 ) ;
194+ assert . equal ( keyApi ( root [ 1 ] . x ) , "x" ) ;
195+ assert ( root [ 1 ] . y !== undefined ) ;
196+ assert . equal ( keyApi ( root [ 1 ] . y ) , "y" ) ;
197+
198+ const added = new Child ( { x : { } , y : { } } ) ;
199+
200+ assert . equal ( keyApi ( added ) , rootKey ) ;
201+
202+ // Check index is updated after insert.
203+ root . insertAtStart ( added ) ;
204+ assert . equal ( keyApi ( root [ 2 ] ) , 2 ) ;
205+ assert . equal ( keyApi ( added ) , 0 ) ;
206+
207+ // Check index is updated after removal.
208+ root . removeRange ( 0 , 1 ) ;
209+ assert . equal ( keyApi ( root [ 1 ] ) , 1 ) ;
210+ assert . equal ( keyApi ( added ) , rootKey ) ;
211+ } ) ;
212+ }
213+
214+ it ( "parent" , ( ) => {
215+ class Child extends schema . object ( "Child" , { x : Point } ) { }
216+ class Root extends schema . array ( "Root" , Child ) { }
217+ const root = init ( Root , [ { x : { } } , { x : { } } ] ) ;
218+
219+ assert . equal ( Tree . parent ( root ) , undefined ) ;
220+ assert . equal ( Tree . parent ( root [ 0 ] ) , root ) ;
221+ assert . equal ( Tree . parent ( root [ 1 ] ) , root ) ;
222+ assert . equal ( Tree . parent ( root [ 1 ] . x ) , root [ 1 ] ) ;
223+
224+ const added = new Child ( { x : { } } ) ;
225+
226+ assert . equal ( Tree . parent ( added ) , undefined ) ;
227+ root . insertAtStart ( added ) ;
228+ assert . equal ( Tree . parent ( added ) , root ) ;
229+ root . removeRange ( 0 , 1 ) ;
230+ assert . equal ( Tree . parent ( added ) , undefined ) ;
231+ } ) ;
201232 } ) ;
202233
203234 it ( "treeStatus" , ( ) => {
@@ -215,29 +246,13 @@ describe("treeNodeApi", () => {
215246 assert . equal ( Tree . status ( root ) , TreeStatus . InDocument ) ;
216247 assert . equal ( Tree . status ( child ) , TreeStatus . Removed ) ;
217248 assert . equal ( Tree . status ( newChild ) , TreeStatus . InDocument ) ;
218- // TODO: test Deleted status.
219- } ) ;
220249
221- it ( "key2" , ( ) => {
222- class Child extends schema . object ( "Child" , {
223- x : Point ,
224- y : schema . optional ( Point , { key : "stable-y" } ) ,
225- } ) { }
226- const Root = schema . array ( Child ) ;
227- const config = new TreeViewConfiguration ( { schema : Root } ) ;
228- const view = getView ( config ) ;
229- view . initialize ( [
230- { x : { } , y : undefined } ,
231- { x : { } , y : { } } ,
232- ] ) ;
233- const { root } = view ;
234- assert . equal ( TreeAlpha . key2 ( root ) , undefined ) ;
235- assert . equal ( TreeAlpha . key2 ( root [ 0 ] ) , 0 ) ;
236- assert . equal ( TreeAlpha . key2 ( root [ 0 ] . x ) , "x" ) ;
237- assert . equal ( TreeAlpha . key2 ( root [ 1 ] ) , 1 ) ;
238- assert . equal ( TreeAlpha . key2 ( root [ 1 ] . x ) , "x" ) ;
239- assert ( root [ 1 ] . y !== undefined ) ;
240- assert . equal ( TreeAlpha . key2 ( root [ 1 ] . y ) , "y" ) ;
250+ view . dispose ( ) ;
251+ assert . equal ( Tree . status ( root ) , TreeStatus . Deleted ) ;
252+ assert . equal ( Tree . status ( child ) , TreeStatus . Deleted ) ;
253+ assert . equal ( Tree . status ( newChild ) , TreeStatus . Deleted ) ;
254+
255+ // TODO: test Deleted status when caused by removal from the tree + expiring from removed status.
241256 } ) ;
242257
243258 describe ( "shortID" , ( ) => {
0 commit comments