|
| 1 | +import { assert } from 'ts-essentials' |
| 2 | +import { flattenAllFields } from './flattenAllFields.js' |
| 3 | +import { getFieldByPath } from './getFieldByPath.js' |
| 4 | +import type { FlattenedArrayField, FlattenedGroupField } from '../fields/config/types.js' |
| 5 | + |
| 6 | +const fields = flattenAllFields({ |
| 7 | + fields: [ |
| 8 | + { |
| 9 | + type: 'text', |
| 10 | + name: 'text', |
| 11 | + }, |
| 12 | + { |
| 13 | + type: 'text', |
| 14 | + name: 'textLocalized', |
| 15 | + localized: true, |
| 16 | + }, |
| 17 | + { |
| 18 | + type: 'array', |
| 19 | + name: 'array', |
| 20 | + fields: [ |
| 21 | + { |
| 22 | + name: 'text', |
| 23 | + type: 'text', |
| 24 | + }, |
| 25 | + { |
| 26 | + name: 'textLocalized', |
| 27 | + localized: true, |
| 28 | + type: 'text', |
| 29 | + }, |
| 30 | + { |
| 31 | + name: 'group', |
| 32 | + type: 'group', |
| 33 | + fields: [ |
| 34 | + { |
| 35 | + name: 'text', |
| 36 | + type: 'text', |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + { |
| 43 | + type: 'tabs', |
| 44 | + tabs: [ |
| 45 | + { |
| 46 | + name: 'tab', |
| 47 | + fields: [ |
| 48 | + { |
| 49 | + type: 'array', |
| 50 | + name: 'localizedArray', |
| 51 | + localized: true, |
| 52 | + fields: [ |
| 53 | + { |
| 54 | + name: 'text', |
| 55 | + type: 'text', |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + ], |
| 64 | +}) |
| 65 | + |
| 66 | +describe('getFieldByPath', () => { |
| 67 | + it('asserts getFieldByPath', () => { |
| 68 | + const assert_1 = getFieldByPath({ fields, path: 'text' }) |
| 69 | + assert(assert_1) |
| 70 | + expect(assert_1.field).toBe(fields[0]) |
| 71 | + expect(assert_1.pathHasLocalized).toBe(false) |
| 72 | + |
| 73 | + const assert_2 = getFieldByPath({ fields, path: 'textLocalized' }) |
| 74 | + assert(assert_2) |
| 75 | + expect(assert_2.field).toBe(fields[1]) |
| 76 | + expect(assert_2.pathHasLocalized).toBe(true) |
| 77 | + expect(assert_2.localizedPath).toBe('textLocalized.<locale>') |
| 78 | + |
| 79 | + const arrayField = fields[2] as FlattenedArrayField |
| 80 | + const assert_3 = getFieldByPath({ fields, path: 'array' }) |
| 81 | + assert(assert_3) |
| 82 | + expect(assert_3.field).toBe(arrayField) |
| 83 | + expect(assert_3.pathHasLocalized).toBe(false) |
| 84 | + |
| 85 | + const assert_4 = getFieldByPath({ fields, path: 'array.text' }) |
| 86 | + assert(assert_4) |
| 87 | + expect(assert_4.field).toBe(arrayField.flattenedFields[0]) |
| 88 | + expect(assert_4.pathHasLocalized).toBe(false) |
| 89 | + |
| 90 | + const assert_5 = getFieldByPath({ fields, path: 'array.textLocalized' }) |
| 91 | + assert(assert_5) |
| 92 | + expect(assert_5.field).toBe(arrayField.flattenedFields[1]) |
| 93 | + expect(assert_5.pathHasLocalized).toBe(true) |
| 94 | + expect(assert_5.localizedPath).toBe('array.textLocalized.<locale>') |
| 95 | + |
| 96 | + const groupWithinArray = arrayField.flattenedFields[2] as FlattenedGroupField |
| 97 | + const assert_6 = getFieldByPath({ fields, path: 'array.group.text' }) |
| 98 | + assert(assert_6) |
| 99 | + expect(assert_6.field).toBe(groupWithinArray.flattenedFields[0]) |
| 100 | + expect(assert_6.pathHasLocalized).toBe(false) |
| 101 | + |
| 102 | + const assert_7 = getFieldByPath({ fields, path: 'tab.localizedArray.text' }) |
| 103 | + assert(assert_7) |
| 104 | + expect(assert_7.field).toBe((fields[3] as any).flattenedFields[0].flattenedFields[0]) |
| 105 | + expect(assert_7.pathHasLocalized).toBe(true) |
| 106 | + expect(assert_7.localizedPath).toBe('tab.localizedArray.<locale>.text') |
| 107 | + }) |
| 108 | +}) |
0 commit comments