Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { nameToFieldNodeSegments } from '../src/utils.lib'
3β
4describe('nameToFieldNodeSegments', () => {
5 it('returns no segments for the root name', () => {
6 const segments = nameToFieldNodeSegments('')
7β
8 expect(segments).toEqual([])
9 })
10β
11 it('splits object field names into segments', () => {
12 const topLevel = nameToFieldNodeSegments('foo')
13 const nested = nameToFieldNodeSegments('foo.bar')
14 const deeplyNested = nameToFieldNodeSegments('foo.bar.foobar')
15β
16 expect(topLevel).toEqual(['foo'])
17 expect(nested).toEqual(['foo', 'bar'])
18 expect(deeplyNested).toEqual(['foo', 'bar', 'foobar'])
19 })
20β
21 it('keeps array indices numeric in mixed object and array paths', () => {
22 const item = nameToFieldNodeSegments('foo[0]')
23 const property = nameToFieldNodeSegments('foo[0].bar')
24 const nestedArray = nameToFieldNodeSegments('foo.bar[0].baz')
25β
26 expect(item).toEqual(['foo', 0])
27 expect(property).toEqual(['foo', 0, 'bar'])
28 expect(nestedArray).toEqual(['foo', 'bar', 0, 'baz'])
29 })
30β
31 it('splits consecutive array indices', () => {
32 const segments = nameToFieldNodeSegments('foo[0][1].bar')
33β
34 expect(segments).toEqual(['foo', 0, 1, 'bar'])
35 })
36β
37 it('keeps supplied segments reusable after changing a parsed path', () => {
38 const supplied = ['items', 0, 'name']
39 const firstPath = nameToFieldNodeSegments(supplied)
40β
41 firstPath[1] = 1
42 const nextPath = nameToFieldNodeSegments(supplied)
43β
44 expect(supplied).toEqual(['items', 0, 'name'])
45 expect(nextPath).toEqual(['items', 0, 'name'])
46 })
47})