Skip to content

Commit 40b2f81

Browse files
committed
test(unstable): add tests for core common utils
1 parent c2f6e16 commit 40b2f81

File tree

4 files changed

+208
-2
lines changed

4 files changed

+208
-2
lines changed

src/lib/unstable/__tests__/helpers.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,30 @@ export const mockSchemaRendererConfig: SchemaRendererConfig = {
178178
},
179179
};
180180

181+
export const createMockArraySchema = (items: JsonSchema | JsonSchema[]): JsonSchema => ({
182+
type: JsonSchemaType.Array,
183+
items,
184+
});
185+
186+
export const createMockBooleanSchema = (): JsonSchemaBoolean => ({
187+
type: JsonSchemaType.Boolean,
188+
});
189+
190+
export const createMockNumberSchema = (): JsonSchemaNumber => ({
191+
type: JsonSchemaType.Number,
192+
});
193+
194+
export const createMockStringSchema = (): JsonSchemaString => ({
195+
type: JsonSchemaType.String,
196+
});
197+
198+
export const createMockObjectSchema = (
199+
properties: Record<string, JsonSchema>,
200+
): JsonSchemaObject => ({
201+
type: JsonSchemaType.Object,
202+
properties,
203+
});
204+
181205
export function createMockSchema<T extends JsonSchema>(
182206
type: JsonSchemaType,
183207
viewType = 'base',
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import {
2+
createMockArraySchema,
3+
createMockBooleanSchema,
4+
createMockNumberSchema,
5+
createMockObjectSchema,
6+
createMockStringSchema,
7+
} from '../../../__tests__/helpers.test';
8+
import type {JsonSchema} from '../../types';
9+
import {getSchemaByFinalFormPath, parseFinalFormPath} from '../common';
10+
11+
describe('core/utils/common', () => {
12+
describe('parseFinalFormPath', () => {
13+
it('should parse a simple path', () => {
14+
const result = parseFinalFormPath('foo.bar');
15+
16+
expect(result).toEqual(['foo', 'bar']);
17+
});
18+
19+
it('should parse a path with array indixes', () => {
20+
const result = parseFinalFormPath('foo[0].bar');
21+
22+
expect(result).toEqual(['foo', '0', 'bar']);
23+
});
24+
25+
it('should parse a path with multiple array indixes', () => {
26+
const result = parseFinalFormPath('foo[0].bar[1].baz');
27+
28+
expect(result).toEqual(['foo', '0', 'bar', '1', 'baz']);
29+
});
30+
31+
it('should parse a path with only array indixes', () => {
32+
const result = parseFinalFormPath('[0][1][2]');
33+
34+
expect(result).toEqual(['0', '1', '2']);
35+
});
36+
37+
it('should handle empty path', () => {
38+
const result = parseFinalFormPath('');
39+
40+
expect(result).toEqual([]);
41+
});
42+
43+
it('should handle path with special characters', () => {
44+
const result = parseFinalFormPath('foo-bar.baz_qux');
45+
46+
expect(result).toEqual(['foo-bar', 'baz_qux']);
47+
});
48+
});
49+
50+
describe('getSchemaByFinalFormPath', () => {
51+
it('should return the main schema when path is empty', () => {
52+
const mainSchema = createMockObjectSchema({foo: createMockStringSchema()});
53+
const path = '';
54+
const headPath = '';
55+
56+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
57+
58+
expect(result).toBe(mainSchema);
59+
});
60+
61+
it('should navigate through object properties', () => {
62+
const stringSchema = createMockStringSchema();
63+
const objectSchema = createMockObjectSchema({bar: stringSchema});
64+
const mainSchema = createMockObjectSchema({foo: objectSchema});
65+
const path = 'foo.bar';
66+
const headPath = '';
67+
68+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
69+
70+
expect(result).toBe(stringSchema);
71+
});
72+
73+
it('should navigate through array items (when items is a single schema)', () => {
74+
const stringSchema = createMockStringSchema();
75+
const arraySchema = createMockArraySchema(stringSchema);
76+
const mainSchema = createMockObjectSchema({foo: arraySchema});
77+
const path = 'foo[0]';
78+
const headPath = '';
79+
80+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
81+
82+
expect(result).toBe(stringSchema);
83+
});
84+
85+
it('should navigate through array items (when items is an array of schemas)', () => {
86+
const stringSchema = createMockStringSchema();
87+
const numberSchema = createMockNumberSchema();
88+
const booleanSchema = createMockBooleanSchema();
89+
const arraySchema = createMockArraySchema([stringSchema, numberSchema, booleanSchema]);
90+
const mainSchema = createMockObjectSchema({foo: arraySchema});
91+
const path = 'foo[1]';
92+
const headPath = '';
93+
94+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
95+
96+
expect(result).toBe(numberSchema);
97+
});
98+
99+
it('should handle complex nested paths', () => {
100+
const booleanSchema = createMockBooleanSchema();
101+
const objectSchema = createMockObjectSchema({qux: booleanSchema});
102+
const arraySchema = createMockArraySchema(objectSchema);
103+
const objectSchema2 = createMockObjectSchema({baz: arraySchema});
104+
const arraySchema2 = createMockArraySchema(objectSchema2);
105+
const mainSchema = createMockObjectSchema({foo: arraySchema2});
106+
const path = 'foo[0].baz[0].qux';
107+
const headPath = '';
108+
109+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
110+
111+
expect(result).toBe(booleanSchema);
112+
});
113+
114+
it('should handle finalFormHeadPath correctly', () => {
115+
const stringSchema = createMockStringSchema();
116+
const objectSchema = createMockObjectSchema({bar: stringSchema});
117+
const mainSchema = createMockObjectSchema({foo: objectSchema});
118+
const path = 'baz.foo.bar';
119+
const headPath = 'baz';
120+
121+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
122+
123+
expect(result).toBe(stringSchema);
124+
});
125+
126+
it('should return undefined for invalid path', () => {
127+
const stringSchema = createMockStringSchema();
128+
const mainSchema = createMockObjectSchema({foo: stringSchema});
129+
const path = 'bar.baz';
130+
const headPath = 'foo';
131+
132+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
133+
134+
expect(result).toBeUndefined();
135+
});
136+
137+
it('should handle array path with string schema', () => {
138+
const stringSchema = createMockStringSchema();
139+
const mainSchema = createMockObjectSchema({foo: stringSchema});
140+
const path = 'foo[0]';
141+
const headPath = '';
142+
143+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
144+
145+
expect(result).toBeUndefined();
146+
});
147+
148+
it('should accept path as array of segments', () => {
149+
const stringSchema = createMockStringSchema();
150+
const objectSchema = createMockObjectSchema({bar: stringSchema});
151+
const mainSchema = createMockObjectSchema({foo: objectSchema});
152+
const path = ['foo', 'bar'];
153+
const headPath = '';
154+
155+
const result = getSchemaByFinalFormPath(path, headPath, mainSchema);
156+
157+
expect(result).toBe(stringSchema);
158+
});
159+
160+
it('should handle undefined schema gracefully', () => {
161+
const path = 'foo.bar';
162+
const headPath = '';
163+
164+
const result = getSchemaByFinalFormPath(
165+
path,
166+
headPath,
167+
undefined as unknown as JsonSchema,
168+
);
169+
170+
expect(result).toBeUndefined();
171+
});
172+
173+
it('should handle schema without type gracefully', () => {
174+
const invalidSchema = {} as JsonSchema;
175+
176+
const result = getSchemaByFinalFormPath('foo', '', invalidSchema);
177+
178+
expect(result).toBeUndefined();
179+
});
180+
});
181+
});

src/lib/unstable/core/utils.ts renamed to src/lib/unstable/core/utils/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import get from 'lodash/get';
22

3-
import {JsonSchemaType} from './constants';
4-
import type {JsonSchema} from './types';
3+
import {JsonSchemaType} from '../constants';
4+
import type {JsonSchema} from '../types';
55

66
export const parseFinalFormPath = (finalFormPath: string): string[] => {
77
const result: string[] = [];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './common';

0 commit comments

Comments
 (0)