|
| 1 | +import { Schema } from 'effect'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { Class } from './core.js'; |
| 4 | +import { WithArrayFields, Array } from './array.js'; |
| 5 | +import { ArrayUnion, ArrayRemove } from '../schema/fields.js'; |
| 6 | + |
| 7 | +describe('WithArrayFields', () => { |
| 8 | + class TestModel extends Class<TestModel>('TestModel')({ |
| 9 | + name: Schema.String, |
| 10 | + tags: WithArrayFields(Schema.Array(Schema.String)), |
| 11 | + }) {} |
| 12 | + |
| 13 | + describe('get variant', () => { |
| 14 | + it('should decode an array', () => { |
| 15 | + const result = Schema.decodeUnknownSync(TestModel)({ |
| 16 | + name: 'Post', |
| 17 | + tags: ['a', 'b'], |
| 18 | + }); |
| 19 | + expect(result.tags).toEqual(['a', 'b']); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should encode an array', () => { |
| 23 | + const result = Schema.encodeSync(TestModel)( |
| 24 | + TestModel.make({ name: 'Post', tags: ['a', 'b'] }) |
| 25 | + ); |
| 26 | + expect(result.tags).toEqual(['a', 'b']); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('update variant', () => { |
| 31 | + it('should accept a plain array', () => { |
| 32 | + const result = Schema.decodeUnknownSync(TestModel.update)({ |
| 33 | + name: 'Post', |
| 34 | + tags: ['a', 'b'], |
| 35 | + }); |
| 36 | + expect(result.tags).toEqual(['a', 'b']); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should accept an ArrayUnion sentinel', () => { |
| 40 | + const result = Schema.decodeUnknownSync(TestModel.update)({ |
| 41 | + name: 'Post', |
| 42 | + tags: ArrayUnion.values(['c', 'd']), |
| 43 | + }); |
| 44 | + expect(result.tags).toBeInstanceOf(ArrayUnion); |
| 45 | + expect(result.tags.values).toEqual(['c', 'd']); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should accept an ArrayRemove sentinel', () => { |
| 49 | + const result = Schema.decodeUnknownSync(TestModel.update)({ |
| 50 | + name: 'Post', |
| 51 | + tags: ArrayRemove.values(['a']), |
| 52 | + }); |
| 53 | + expect(result.tags).toBeInstanceOf(ArrayRemove); |
| 54 | + expect(result.tags.values).toEqual(['a']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should encode ArrayUnion sentinel as-is (for converter to handle)', () => { |
| 58 | + const result = Schema.encodeSync(TestModel.update)({ |
| 59 | + name: 'Post', |
| 60 | + tags: ArrayUnion.values(['c']), |
| 61 | + }); |
| 62 | + expect(result.tags).toBeInstanceOf(ArrayUnion); |
| 63 | + expect(result.tags.values).toEqual(['c']); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should encode ArrayRemove sentinel as-is (for converter to handle)', () => { |
| 67 | + const result = Schema.encodeSync(TestModel.update)({ |
| 68 | + name: 'Post', |
| 69 | + tags: ArrayRemove.values(['a']), |
| 70 | + }); |
| 71 | + expect(result.tags).toBeInstanceOf(ArrayRemove); |
| 72 | + expect(result.tags.values).toEqual(['a']); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('json variant', () => { |
| 77 | + it('should decode an array', () => { |
| 78 | + const result = Schema.decodeUnknownSync(TestModel.json)({ |
| 79 | + name: 'Post', |
| 80 | + tags: ['a', 'b'], |
| 81 | + }); |
| 82 | + expect(result.tags).toEqual(['a', 'b']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should reject sentinels (not part of json variant)', () => { |
| 86 | + expect(() => |
| 87 | + Schema.decodeUnknownSync(TestModel.json)({ |
| 88 | + name: 'Post', |
| 89 | + tags: ArrayUnion.make({ values: ['c'] }), |
| 90 | + }) |
| 91 | + ).toThrow(); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('Array', () => { |
| 97 | + class TestModel extends Class<TestModel>('TestModel')({ |
| 98 | + name: Schema.String, |
| 99 | + tags: Array(Schema.String), |
| 100 | + }) {} |
| 101 | + |
| 102 | + it('get variant decodes array', () => { |
| 103 | + const result = Schema.decodeUnknownSync(TestModel)({ |
| 104 | + name: 'Post', |
| 105 | + tags: ['x'], |
| 106 | + }); |
| 107 | + expect(result.tags).toEqual(['x']); |
| 108 | + }); |
| 109 | + |
| 110 | + it('update variant accepts ArrayUnion', () => { |
| 111 | + const result = Schema.decodeUnknownSync(TestModel.update)({ |
| 112 | + name: 'Post', |
| 113 | + tags: ArrayUnion.values(['y']), |
| 114 | + }); |
| 115 | + expect(result.tags).toBeInstanceOf(ArrayUnion); |
| 116 | + }); |
| 117 | + |
| 118 | + it('update variant accepts ArrayRemove', () => { |
| 119 | + const result = Schema.decodeUnknownSync(TestModel.update)({ |
| 120 | + name: 'Post', |
| 121 | + tags: ArrayRemove.values(['x']), |
| 122 | + }); |
| 123 | + expect(result.tags).toBeInstanceOf(ArrayRemove); |
| 124 | + }); |
| 125 | +}); |
0 commit comments