Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | import type {OperationStrIns} from '../../..'; import type {ApplyPatch} from '../../types'; export const testStrInsOp = (applyPatch: ApplyPatch) => { describe('str_ins', () => { describe('root', () => { test('can add text to empty string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '', pos: 0, str: 'bar', }; const result = applyPatch('', [operation], {mutate: true}).doc; expect(result).toEqual('bar'); }); }); describe('object', () => { test('can add text to empty string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo', pos: 0, str: 'bar', }; const result = applyPatch({foo: ''}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: 'bar'}); }); test('throws if target is not a string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo', pos: 0, str: 'bar', }; expect(() => applyPatch({foo: 123}, [operation], {mutate: true})).toThrow(); expect(() => applyPatch({foo: true}, [operation], {mutate: true})).toThrow(); expect(() => applyPatch({foo: {}}, [operation], {mutate: true})).toThrow(); expect(() => applyPatch({foo: []}, [operation], {mutate: true})).toThrow(); expect(() => applyPatch({foo: null}, [operation], {mutate: true})).toThrow(); }); test('can add text to empty string at position greater than host string length', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo', pos: 25, str: 'bar', }; const result = applyPatch({foo: ''}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: 'bar'}); }); test('can insert text into a string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo/bar', pos: 1, str: 'b', }; const result = applyPatch({foo: {bar: 'ac'}}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: {bar: 'abc'}}); }); test('can insert text at the end of the string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo/bar', pos: 2, str: 'haha', }; const result = applyPatch({foo: {bar: 'ac'}}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: {bar: 'achaha'}}); }); test('can insert text into a string at position greater than host string length', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo/bar', pos: 123, str: 'b', }; const result = applyPatch({foo: {bar: 'ac'}}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: {bar: 'acb'}}); }); }); describe('array', () => { test('can add text to empty string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/0', pos: 0, str: 'bar', }; const result = applyPatch([''], [operation], {mutate: true}).doc; expect(result).toEqual(['bar']); }); test('can add text to empty string at position greater than host string length', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/0', pos: 25, str: 'bar', }; const result = applyPatch([''], [operation], {mutate: true}).doc; expect(result).toEqual(['bar']); }); test('can insert text into a string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo/1', pos: 1, str: 'b', }; const result = applyPatch({foo: [0, 'ac']}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: [0, 'abc']}); }); test('can insert text at the end of the string', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/foo/2', pos: 2, str: 'haha', }; const result = applyPatch({foo: [1, 2, 'ac']}, [operation], {mutate: true}).doc; expect(result).toEqual({foo: [1, 2, 'achaha']}); }); test('can insert text into a string at position greater than host string length', () => { const operation: OperationStrIns = { op: 'str_ins', path: '/1', pos: 123, str: 'b', }; const result = applyPatch([true, 'ac'], [operation], {mutate: true}).doc; expect(result).toEqual([true, 'acb']); }); }); describe('scenarios', () => { test('can create new string key and add content to it (if pos = 0 and there was nothing before)', () => { const operations: OperationStrIns[] = [ { op: 'str_ins', path: '/baz', pos: 0, str: 'H', }, { op: 'str_ins', path: '/baz', pos: 1, str: 'ello', }, ]; const result = applyPatch({foo: '123'}, operations, {mutate: true}).doc; expect(result).toEqual({foo: '123', baz: 'Hello'}); }); test('throws if new string is create at position other than 0 (pos != 0)', () => { const operations: OperationStrIns[] = [ { op: 'str_ins', path: '/baz', pos: 1, str: 'H', }, { op: 'str_ins', path: '/baz', pos: 2, str: 'ello', }, ]; expect(() => applyPatch({foo: '123'}, operations, {mutate: true})).toThrow(); }); }); }); }; |