|
| 1 | +import expect from 'expect'; |
| 2 | +import { inferTypeFromValues } from './inferTypeFromValues'; |
| 3 | + |
| 4 | +describe('inferTypeFromValues', () => { |
| 5 | + it('should return an InferredElement', () => { |
| 6 | + expect(inferTypeFromValues('id', ['foo'])).toEqual({ |
| 7 | + type: 'id', |
| 8 | + props: { source: 'id' }, |
| 9 | + }); |
| 10 | + }); |
| 11 | + it('should return an id field for field named id', () => { |
| 12 | + expect(inferTypeFromValues('id', ['foo', 'bar'])).toEqual({ |
| 13 | + type: 'id', |
| 14 | + props: { source: 'id' }, |
| 15 | + }); |
| 16 | + }); |
| 17 | + it('should return a reference field for field named *_id', () => { |
| 18 | + expect(inferTypeFromValues('foo_id', ['foo', 'bar'])).toEqual({ |
| 19 | + type: 'reference', |
| 20 | + props: { source: 'foo_id', reference: 'foos' }, |
| 21 | + children: { type: 'referenceChild' }, |
| 22 | + }); |
| 23 | + }); |
| 24 | + it('should return a reference field for field named *Id', () => { |
| 25 | + expect(inferTypeFromValues('fooId', ['foo', 'bar'])).toEqual({ |
| 26 | + type: 'reference', |
| 27 | + props: { source: 'fooId', reference: 'foos' }, |
| 28 | + children: { type: 'referenceChild' }, |
| 29 | + }); |
| 30 | + }); |
| 31 | + it('should return a reference array field for field named *_ids', () => { |
| 32 | + expect(inferTypeFromValues('foo_ids', ['foo', 'bar'])).toEqual({ |
| 33 | + type: 'referenceArray', |
| 34 | + props: { source: 'foo_ids', reference: 'foos' }, |
| 35 | + children: { type: 'referenceArrayChild' }, |
| 36 | + }); |
| 37 | + }); |
| 38 | + it('should return a reference array field for field named *Ids', () => { |
| 39 | + expect(inferTypeFromValues('fooIds', ['foo', 'bar'])).toEqual({ |
| 40 | + type: 'referenceArray', |
| 41 | + props: { source: 'fooIds', reference: 'foos' }, |
| 42 | + children: { type: 'referenceArrayChild' }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + it('should return a string field for no values', () => { |
| 46 | + expect(inferTypeFromValues('foo', [])).toEqual({ |
| 47 | + type: 'string', |
| 48 | + props: { source: 'foo' }, |
| 49 | + }); |
| 50 | + }); |
| 51 | + it('should return an array field for array of object values', () => { |
| 52 | + expect( |
| 53 | + inferTypeFromValues('foo', [ |
| 54 | + [{ bar: 1 }, { bar: 2 }], |
| 55 | + [{ bar: 3 }, { bar: 4 }], |
| 56 | + ]) |
| 57 | + ).toEqual({ |
| 58 | + type: 'array', |
| 59 | + props: { source: 'foo' }, |
| 60 | + children: [{ type: 'number', props: { source: 'bar' } }], |
| 61 | + }); |
| 62 | + }); |
| 63 | + it('should return a string field for array of non-object values', () => { |
| 64 | + expect( |
| 65 | + inferTypeFromValues('foo', [ |
| 66 | + [1, 2], |
| 67 | + [3, 4], |
| 68 | + ]) |
| 69 | + ).toEqual({ |
| 70 | + type: 'string', |
| 71 | + props: { source: 'foo' }, |
| 72 | + }); |
| 73 | + }); |
| 74 | + it('should return a boolean field for boolean values', () => { |
| 75 | + expect(inferTypeFromValues('foo', [true, false, true])).toEqual({ |
| 76 | + type: 'boolean', |
| 77 | + props: { source: 'foo' }, |
| 78 | + }); |
| 79 | + }); |
| 80 | + it('should return a date field for date values', () => { |
| 81 | + expect( |
| 82 | + inferTypeFromValues('foo', [ |
| 83 | + new Date('2018-10-01'), |
| 84 | + new Date('2018-12-03'), |
| 85 | + ]) |
| 86 | + ).toEqual({ |
| 87 | + type: 'date', |
| 88 | + props: { source: 'foo' }, |
| 89 | + }); |
| 90 | + }); |
| 91 | + it('should return an email field for email name', () => { |
| 92 | + expect(inferTypeFromValues('email', ['whatever'])).toEqual({ |
| 93 | + type: 'email', |
| 94 | + props: { source: 'email' }, |
| 95 | + }); |
| 96 | + }); |
| 97 | + it.skip('should return an email field for email string values', () => { |
| 98 | + expect( |
| 99 | + inferTypeFromValues('foo', ['me@example.com', 'you@foo.co.uk']) |
| 100 | + ).toEqual({ |
| 101 | + type: 'email', |
| 102 | + props: { source: 'foo' }, |
| 103 | + }); |
| 104 | + }); |
| 105 | + it('should return an url field for url name', () => { |
| 106 | + expect(inferTypeFromValues('url', ['whatever', 'whatever'])).toEqual({ |
| 107 | + type: 'url', |
| 108 | + props: { source: 'url' }, |
| 109 | + }); |
| 110 | + }); |
| 111 | + it.skip('should return an url field for url string values', () => { |
| 112 | + expect( |
| 113 | + inferTypeFromValues('foo', [ |
| 114 | + 'http://foo.com/bar', |
| 115 | + 'https://www.foo.com/index.html#foo', |
| 116 | + ]) |
| 117 | + ).toEqual({ |
| 118 | + type: 'url', |
| 119 | + props: { source: 'foo' }, |
| 120 | + }); |
| 121 | + }); |
| 122 | + it('should return a date field for date string values', () => { |
| 123 | + expect( |
| 124 | + inferTypeFromValues('foo', ['2018-10-01', '2018-12-03']) |
| 125 | + ).toEqual({ |
| 126 | + type: 'date', |
| 127 | + props: { source: 'foo' }, |
| 128 | + }); |
| 129 | + }); |
| 130 | + it('should return a rich text field for HTML values', () => { |
| 131 | + expect( |
| 132 | + inferTypeFromValues('foo', [ |
| 133 | + 'This is <h1>Good</h1>', |
| 134 | + '<body><h1>hello</h1>World</body>', |
| 135 | + ]) |
| 136 | + ).toEqual({ |
| 137 | + type: 'richText', |
| 138 | + props: { source: 'foo' }, |
| 139 | + }); |
| 140 | + }); |
| 141 | + it('should return a string field for string values', () => { |
| 142 | + expect( |
| 143 | + inferTypeFromValues('foo', ['This is Good', 'hello, World!']) |
| 144 | + ).toEqual({ |
| 145 | + type: 'string', |
| 146 | + props: { source: 'foo' }, |
| 147 | + }); |
| 148 | + }); |
| 149 | + it('should return a number field for number values', () => { |
| 150 | + expect(inferTypeFromValues('foo', [12, 1e23, 653.56])).toEqual({ |
| 151 | + type: 'number', |
| 152 | + props: { source: 'foo' }, |
| 153 | + }); |
| 154 | + }); |
| 155 | + it('should return a typed field for object values', () => { |
| 156 | + expect( |
| 157 | + inferTypeFromValues('foo', [ |
| 158 | + { bar: 1, baz: 2 }, |
| 159 | + { bar: 3, baz: 4 }, |
| 160 | + ]) |
| 161 | + ).toEqual({ |
| 162 | + type: 'number', |
| 163 | + props: { source: 'foo.bar' }, |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments