All files / json-patch/applyPatch/__tests__ testApplyPatchAutomated.ts

96.15% Statements 25/26
60% Branches 6/10
100% Functions 6/6
95.83% Lines 23/24

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 455x 5x 5x 5x     5x                     5x 6x 6x 12x 12x 12x 150x 138x 138x 120x 120x 120x 120x   18x 18x 18x 18x 18x                  
import tests_json from '../../__tests__/tests.json';
import spec_json from '../../__tests__/spec.json';
import {validateOperation} from '../../validate';
import {clone} from '@jsonjoy.com/util/lib/json-clone/clone';
import type {ApplyPatch} from '../types';
 
const testSuites = [
  {
    name: 'tests.json',
    tests: tests_json,
  },
  {
    name: 'spec.json',
    tests: spec_json,
  },
];
 
export const testApplyPatchAutomated = (applyPatch: ApplyPatch) => {
  describe('applyPatch [automated]', () => {
    for (const s of testSuites) {
      const suite = clone(s) as any;
      describe(suite.name, () => {
        for (const test of suite.tests) {
          if (test.disabled) return;
          const testName = test.comment || test.error || JSON.stringify(test.patch);
          if (test.expected) {
            it('should succeed: ' + testName, () => {
              test.patch.forEach(validateOperation);
              const {doc} = applyPatch(test.doc, test.patch, {mutate: true});
              expect(doc).toEqual(test.expected);
            });
          } else if (test.error || test.patch[0].op === 'test') {
            it('should throw an error: ' + testName, () => {
              expect(() => {
                test.patch.forEach(validateOperation);
                applyPatch(test.doc, test.patch, {mutate: true});
              }).toThrow();
            });
          } else Ethrow new Error('invalid test case');
        }
      });
    }
  });
};