|
| 1 | +// Required imports |
| 2 | +import traverse, { TraverseOptions } from './'; |
| 3 | +import { JSONSchema } from '@json-schema-tools/meta-schema'; |
| 4 | + |
| 5 | +// Example schemas for testing |
| 6 | +const exampleSchema: JSONSchema = { |
| 7 | + type: 'object', |
| 8 | + properties: { |
| 9 | + name: { type: 'string' }, |
| 10 | + age: { type: 'number' }, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +// Test cases for different behaviors |
| 15 | +describe('traverse mutability behavior', () => { |
| 16 | + it('should modify the original schema when mutable is true and mutation modifies properties directly', () => { |
| 17 | + const mutableSchema = { ...exampleSchema }; |
| 18 | + const mutationFn = (schema: any) => { |
| 19 | + if (typeof schema === 'object' && schema.type === 'string') { |
| 20 | + schema.type = 'number'; // Directly modify the schema |
| 21 | + } |
| 22 | + return schema; |
| 23 | + }; |
| 24 | + |
| 25 | + traverse(mutableSchema, mutationFn, { mutable: true } as TraverseOptions); |
| 26 | + |
| 27 | + expect(mutableSchema.properties?.name.type).toBe('number'); // Original schema is modified |
| 28 | + }); |
| 29 | + |
| 30 | + it('should not modify the original schema when mutable is false and mutation modifies properties directly', () => { |
| 31 | + const mutableSchema = { ...exampleSchema }; |
| 32 | + const mutationFn = (schema: any) => { |
| 33 | + if (typeof schema === 'object' && schema.type === 'string') { |
| 34 | + schema.type = 'number'; // Directly modify the schema |
| 35 | + } |
| 36 | + return schema; |
| 37 | + }; |
| 38 | + |
| 39 | + const result: any = traverse(mutableSchema, mutationFn, { mutable: false } as TraverseOptions); |
| 40 | + |
| 41 | + expect(mutableSchema.properties?.name.type).toBe('string'); // Original schema remains unchanged |
| 42 | + expect(result.properties.name.type).toBe('number'); // Modified copy |
| 43 | + }); |
| 44 | + |
| 45 | + it('should replace the schema with a new object when mutation returns a new schema (mutable true)', () => { |
| 46 | + const mutableSchema = { ...exampleSchema }; |
| 47 | + const mutationFn = (schema: any) => { |
| 48 | + if (typeof schema === 'object' && schema.type === 'string') { |
| 49 | + return { ...schema, type: 'boolean' }; // Return a new object |
| 50 | + } |
| 51 | + return schema; |
| 52 | + }; |
| 53 | + |
| 54 | + traverse(mutableSchema, mutationFn, { mutable: true } as TraverseOptions); |
| 55 | + |
| 56 | + expect(mutableSchema.properties?.name.type).toBe('boolean'); // Original schema is replaced |
| 57 | + }); |
| 58 | + |
| 59 | + it('should replace the schema with a new object when mutation returns a new schema (mutable false)', () => { |
| 60 | + const mutableSchema = { ...exampleSchema }; |
| 61 | + const mutationFn = (schema: any) => { |
| 62 | + if (typeof schema === 'object' && schema.type === 'string') { |
| 63 | + return { ...schema, type: 'boolean' }; // Return a new object |
| 64 | + } |
| 65 | + return schema; |
| 66 | + }; |
| 67 | + |
| 68 | + const result: any = traverse(mutableSchema, mutationFn, { mutable: false } as TraverseOptions); |
| 69 | + |
| 70 | + expect(mutableSchema.properties?.name.type).toBe('string'); // Original schema remains unchanged |
| 71 | + expect(result.properties.name.type).toBe('boolean'); // Modified copy |
| 72 | + }); |
| 73 | + |
| 74 | + it('should skip the first mutation when skipFirstMutation is true', () => { |
| 75 | + const mutableSchema = { ...exampleSchema }; |
| 76 | + const mutationFn = (schema: any) => { |
| 77 | + if (typeof schema === 'object' && schema.type) { |
| 78 | + schema.type = 'null'; // Directly modify the schema |
| 79 | + } |
| 80 | + return schema; |
| 81 | + }; |
| 82 | + |
| 83 | + const result: any = traverse(mutableSchema, mutationFn, { skipFirstMutation: true } as TraverseOptions); |
| 84 | + |
| 85 | + expect(result.type).toBeUndefined(); // Root is not mutated |
| 86 | + expect(result.properties.name.type).toBe('null'); // Subschemas are mutated |
| 87 | + }); |
| 88 | + |
| 89 | + it('should traverse in breadth-first manner when bfs is true', () => { |
| 90 | + const bfsSchema: JSONSchema = { |
| 91 | + type: 'object', |
| 92 | + properties: { |
| 93 | + nested: { |
| 94 | + type: 'object', |
| 95 | + properties: { |
| 96 | + deep: { type: 'string' }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }; |
| 101 | + |
| 102 | + const order: string[] = []; |
| 103 | + const mutationFn = (schema: any) => { |
| 104 | + if (typeof schema === 'object' && schema.type) { |
| 105 | + order.push(schema.type); |
| 106 | + } |
| 107 | + return schema; |
| 108 | + }; |
| 109 | + |
| 110 | + traverse(bfsSchema, mutationFn, { bfs: true } as TraverseOptions); |
| 111 | + |
| 112 | + expect(order).toEqual(['object', 'object', 'string']); // Breadth-first order |
| 113 | + }); |
| 114 | +}); |
0 commit comments