|
| 1 | +import { SchemaKind } from '../../../types'; |
| 2 | +import { walk } from '../walk'; |
| 3 | + |
| 4 | +describe('Schema Walker', () => { |
| 5 | + describe('when type equals array', () => { |
| 6 | + test.each(['[circular]', 2, null])('given invalid items, should normalize them %s', items => { |
| 7 | + const schema = { |
| 8 | + type: SchemaKind.Array, |
| 9 | + items, |
| 10 | + }; |
| 11 | + const { value: node } = walk(schema as any).next(); |
| 12 | + |
| 13 | + expect(node).toStrictEqual({ |
| 14 | + fragment: schema, |
| 15 | + node: { |
| 16 | + id: expect.any(String), |
| 17 | + type: SchemaKind.Array, |
| 18 | + annotations: {}, |
| 19 | + enum: void 0, |
| 20 | + validations: {}, |
| 21 | + additionalItems: void 0, |
| 22 | + items: void 0, |
| 23 | + }, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test.each([{ type: 'string' }, [{ type: 'number' }]])( |
| 28 | + 'given valid items, should leave them untouched %s', |
| 29 | + items => { |
| 30 | + const schema = { |
| 31 | + type: SchemaKind.Array, |
| 32 | + items, |
| 33 | + }; |
| 34 | + const { value: node } = walk(schema as any).next(); |
| 35 | + |
| 36 | + expect(node).toStrictEqual({ |
| 37 | + fragment: schema, |
| 38 | + node: { |
| 39 | + id: expect.any(String), |
| 40 | + type: SchemaKind.Array, |
| 41 | + annotations: {}, |
| 42 | + enum: void 0, |
| 43 | + validations: {}, |
| 44 | + additionalItems: void 0, |
| 45 | + items, |
| 46 | + }, |
| 47 | + }); |
| 48 | + }, |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('when type equals object', () => { |
| 53 | + test.each(['[circular]', 2, null, [{}]])('given invalid properties, should normalize them %s', properties => { |
| 54 | + const schema = { |
| 55 | + type: SchemaKind.Object, |
| 56 | + properties, |
| 57 | + }; |
| 58 | + const { value: node } = walk(schema as any).next(); |
| 59 | + |
| 60 | + expect(node).toStrictEqual({ |
| 61 | + fragment: schema, |
| 62 | + node: { |
| 63 | + id: expect.any(String), |
| 64 | + type: SchemaKind.Object, |
| 65 | + annotations: {}, |
| 66 | + enum: void 0, |
| 67 | + validations: {}, |
| 68 | + additionalProperties: void 0, |
| 69 | + patternProperties: void 0, |
| 70 | + properties: void 0, |
| 71 | + }, |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + test.each([{}, { foo: { type: 'string' } }])( |
| 76 | + 'given valid properties, should leave them untouched %s', |
| 77 | + properties => { |
| 78 | + const schema = { |
| 79 | + type: SchemaKind.Object, |
| 80 | + properties, |
| 81 | + }; |
| 82 | + const { value: node } = walk(schema as any).next(); |
| 83 | + |
| 84 | + expect(node).toStrictEqual({ |
| 85 | + fragment: schema, |
| 86 | + node: { |
| 87 | + id: expect.any(String), |
| 88 | + type: SchemaKind.Object, |
| 89 | + annotations: {}, |
| 90 | + enum: void 0, |
| 91 | + validations: {}, |
| 92 | + additionalProperties: void 0, |
| 93 | + patternProperties: void 0, |
| 94 | + properties, |
| 95 | + }, |
| 96 | + }); |
| 97 | + }, |
| 98 | + ); |
| 99 | + }); |
| 100 | +}); |
0 commit comments