|
| 1 | +import {Operation} from '../../../json-patch'; |
| 2 | +import {Model} from '../../model/Model'; |
| 3 | +import {JsonPatch} from '../JsonPatch'; |
| 4 | + |
| 5 | +interface TestCase { |
| 6 | + name: string; |
| 7 | + doc1?: unknown; |
| 8 | + patches: Operation[][]; |
| 9 | + doc2?: unknown; |
| 10 | + throws?: string; |
| 11 | + only?: true; |
| 12 | +} |
| 13 | + |
| 14 | +const testCases: TestCase[] = [ |
| 15 | + { |
| 16 | + name: 'can insert char in empty string', |
| 17 | + doc1: '', |
| 18 | + patches: [[{op: 'str_ins', path: '', pos: 0, str: 'a'}]], |
| 19 | + doc2: 'a', |
| 20 | + }, |
| 21 | + { |
| 22 | + name: 'can insert char at the end of string', |
| 23 | + doc1: '1', |
| 24 | + patches: [[{op: 'str_ins', path: '', pos: 1, str: '2'}]], |
| 25 | + doc2: '12', |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'can insert char beyond end of string', |
| 29 | + doc1: '1', |
| 30 | + patches: [[{op: 'str_ins', path: '', pos: 111, str: '2'}]], |
| 31 | + doc2: '12', |
| 32 | + }, |
| 33 | + { |
| 34 | + name: 'can insert char beyond end of string - 2', |
| 35 | + doc1: '1', |
| 36 | + patches: [[{op: 'str_ins', path: '', pos: 2, str: '2'}]], |
| 37 | + doc2: '12', |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'can insert char at the beginning of string', |
| 41 | + doc1: '1', |
| 42 | + patches: [[{op: 'str_ins', path: '', pos: 0, str: '0'}]], |
| 43 | + doc2: '01', |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'can insert char in the middle of string', |
| 47 | + doc1: '25', |
| 48 | + patches: [[{op: 'str_ins', path: '', pos: 1, str: '.'}]], |
| 49 | + doc2: '2.5', |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'can insert text in nested object', |
| 53 | + doc1: null, |
| 54 | + patches: [ |
| 55 | + [{op: 'add', path: '', value: {foo: [{bar: 'baz'}]}}], |
| 56 | + [{op: 'str_ins', path: '/foo/0/bar', pos: 3, str: '!'}] |
| 57 | + ], |
| 58 | + doc2: {foo: [{bar: 'baz!'}]}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'can insert text in nested object - 2', |
| 62 | + doc1: null, |
| 63 | + patches: [ |
| 64 | + [{op: 'add', path: '', value: {foo: [{bar: 'baz'}]}}], |
| 65 | + [{op: 'str_ins', path: ['foo', 0, 'bar'], pos: 3, str: '!'}] |
| 66 | + ], |
| 67 | + doc2: {foo: [{bar: 'baz!'}]}, |
| 68 | + }, |
| 69 | +]; |
| 70 | + |
| 71 | +for (const {only, name, doc1, doc2, patches, throws} of testCases) { |
| 72 | + (only ? test.only : test)(name, () => { |
| 73 | + const model = Model.withLogicalClock(); |
| 74 | + const jsonPatch = new JsonPatch(model); |
| 75 | + if (doc1 !== undefined) model.api.root(doc1).commit(); |
| 76 | + if (throws) { |
| 77 | + expect(() => { |
| 78 | + for (const patch of patches) jsonPatch.apply(patch).commit(); |
| 79 | + }).toThrow(new Error(throws)); |
| 80 | + } else { |
| 81 | + for (const patch of patches) jsonPatch.apply(patch).commit(); |
| 82 | + expect(model.toView()).toEqual(doc2); |
| 83 | + } |
| 84 | + }); |
| 85 | +} |
0 commit comments