|
| 1 | +import {s} from '../../../json-crdt-patch'; |
| 2 | +import {Model} from '../../model'; |
| 3 | +import {JsonNodeToSchema, SchemaToJsonNode} from '../types'; |
| 4 | + |
| 5 | +describe('can infer schema of JSON CRDT nodes', () => { |
| 6 | + test('con', () => { |
| 7 | + const schema1 = s.con(123); |
| 8 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 9 | + }); |
| 10 | + |
| 11 | + test('val', () => { |
| 12 | + const schema1 = s.val(s.con(true)); |
| 13 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 14 | + }); |
| 15 | + |
| 16 | + test('obj', () => { |
| 17 | + const schema1 = s.obj({ |
| 18 | + hello: s.con('world'), |
| 19 | + }); |
| 20 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 21 | + }); |
| 22 | + |
| 23 | + test('vec', () => { |
| 24 | + const schema1 = s.vec(s.con(1), s.val(s.con(2))); |
| 25 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 26 | + }); |
| 27 | + |
| 28 | + test('str', () => { |
| 29 | + const schema1 = s.str('asdf'); |
| 30 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 31 | + }); |
| 32 | + |
| 33 | + test('bin', () => { |
| 34 | + const schema1 = s.bin(new Uint8Array([1, 2, 3])); |
| 35 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 36 | + }); |
| 37 | + |
| 38 | + test('arr', () => { |
| 39 | + const schema1 = s.arr([s.con(1), s.val(s.con(2))]); |
| 40 | + const schema2: JsonNodeToSchema<SchemaToJsonNode<typeof schema1>> = schema1; |
| 41 | + }); |
| 42 | + |
| 43 | + test('from typed model', () => { |
| 44 | + const model = Model.withLogicalClock().setSchema(s.obj({ |
| 45 | + id: s.con('asdf'), |
| 46 | + age: s.val(s.con(42)), |
| 47 | + })); |
| 48 | + type Node = ReturnType<typeof model.root.node>; |
| 49 | + type Schema = JsonNodeToSchema<Node>; |
| 50 | + const schema: Schema = s.obj({ |
| 51 | + id: s.con('asdf'), |
| 52 | + age: s.val(s.con(42)), |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments