|
| 1 | +import { flattenTypes } from '../flattenTypes'; |
| 2 | + |
| 3 | +describe('flattenTypes util', () => { |
| 4 | + it.each(['string', 'number', ['object']])('given valid %s type, returns it', type => { |
| 5 | + expect(flattenTypes(type)).toEqual(type); |
| 6 | + }); |
| 7 | + |
| 8 | + it('returns undefined when no valid type is found', () => { |
| 9 | + expect(flattenTypes(2)).toBeUndefined(); |
| 10 | + expect(flattenTypes(void 0)).toBeUndefined(); |
| 11 | + expect(flattenTypes('foo')).toBeUndefined(); |
| 12 | + expect(flattenTypes(['test', 'foo'])).toBeUndefined(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('returns undefined when no valid type is found', () => { |
| 16 | + expect(flattenTypes(2)).toBeUndefined(); |
| 17 | + expect(flattenTypes(void 0)).toBeUndefined(); |
| 18 | + expect(flattenTypes('foo')).toBeUndefined(); |
| 19 | + expect(flattenTypes(['test', 'foo'])).toBeUndefined(); |
| 20 | + expect(flattenTypes({ type: 'bar' })).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('deduplicate types', () => { |
| 24 | + expect(flattenTypes(['null', 'string', 'string'])).toEqual(['null', 'string']); |
| 25 | + }); |
| 26 | + |
| 27 | + it('removes invalid types', () => { |
| 28 | + expect(flattenTypes(['foo', 'string'])).toEqual(['string']); |
| 29 | + expect(flattenTypes(['foo', { type: 'bar' }, 'string'])).toEqual(['string']); |
| 30 | + expect(flattenTypes(['foo', 'string'])).toEqual(['string']); |
| 31 | + expect(flattenTypes(['number', 2, null])).toEqual(['number']); |
| 32 | + }); |
| 33 | + |
| 34 | + it('flattens types', () => { |
| 35 | + expect(flattenTypes([{ type: 'array' }, { type: 'object' }, { type: 'object' }, { type: 'number' }])).toEqual([ |
| 36 | + 'array', |
| 37 | + 'object', |
| 38 | + 'number', |
| 39 | + ]); |
| 40 | + }); |
| 41 | + |
| 42 | + it.each([ |
| 43 | + [{ type: 'array', items: {} }, { type: 'object', properties: {} }], |
| 44 | + [{ type: 'string', enum: [] }], |
| 45 | + { type: 'number', minimum: 1 }, |
| 46 | + { additionalProperties: 1 }, |
| 47 | + ])('throws when complex type is met', type => { |
| 48 | + expect(flattenTypes.bind(null, type)).toThrow( |
| 49 | + 'The "type" property must be a string, or an array of strings. Objects and array of objects are not valid.', |
| 50 | + ); |
| 51 | + }); |
| 52 | +}); |
0 commit comments