All files / json-patch/applyPatch/__tests__/ops inc.ts

100% Statements 55/55
100% Branches 0/0
100% Functions 17/17
100% Lines 53/53

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        3x 44x   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 {ApplyPatch} from '../../types';
import type {Operation} from '../../../types';
import type {OperationInc} from '../../..';
 
export const testIncOp = (applyPatch: ApplyPatch) => {
  const applyOperations = (doc: unknown, ops: Operation[]) => applyPatch(doc, ops, {mutate: true});
 
  describe('inc', () => {
    test('casts values and them increments them', () => {
      const doc = {
        val1: true,
        val2: false,
        val3: 1,
        val4: 0,
      };
      const operations: OperationInc[] = [
        {op: 'inc', path: '/val1', inc: 1},
        {op: 'inc', path: '/val2', inc: 1},
        {op: 'inc', path: '/val3', inc: 1},
        {op: 'inc', path: '/val4', inc: 1},
      ];
      const result = applyOperations(doc, operations).doc;
      expect(result).toEqual({
        val1: 2,
        val2: 1,
        val3: 2,
        val4: 1,
      });
    });
 
    test('can use arbitrary increment value, and can decrement', () => {
      const doc = {
        foo: 1,
      };
      const operations: OperationInc[] = [
        {op: 'inc', path: '/foo', inc: 10},
        {op: 'inc', path: '/foo', inc: -3},
      ];
      const result = applyOperations(doc, operations).doc;
      expect(result).toEqual({
        foo: 8,
      });
    });
 
    test('increment can be a floating point number', () => {
      const doc = {
        foo: 1,
      };
      const operations: OperationInc[] = [{op: 'inc', path: '/foo', inc: 0.1}];
      const result = applyOperations(doc, operations).doc;
      expect(result).toEqual({
        foo: 1.1,
      });
    });
 
    describe('root', () => {
      test('increments from 0 to 5', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '',
          inc: 5,
        };
        const result = applyOperations(0, [operation]).doc;
        expect(result).toEqual(5);
      });
 
      test('increments from -0 to 5', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '',
          inc: 5,
        };
        const result = applyOperations(-0, [operation]).doc;
        expect(result).toEqual(5);
      });
    });
 
    describe('object', () => {
      test('increments from 0 to 5', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '/lala',
          inc: 5,
        };
        const result = applyOperations({lala: 0}, [operation]).doc;
        expect(result).toEqual({lala: 5});
      });
 
      test('increments from -0 to 5', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '/lala',
          inc: 5,
        };
        const result = applyOperations({lala: -0}, [operation]).doc;
        expect(result).toEqual({lala: 5});
      });
 
      test('casts string to number', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '/lala',
          inc: 5,
        };
        const result = applyOperations({lala: '4'}, [operation]).doc;
        expect(result).toEqual({lala: 9});
      });
 
      test('can increment twice', () => {
        const operations: OperationInc[] = [
          {
            op: 'inc',
            path: '/lala',
            inc: 1,
          },
          {
            op: 'inc',
            path: '/lala',
            inc: 2,
          },
        ];
        const result = applyOperations({lala: 0}, operations).doc;
        expect(result).toEqual({lala: 3});
      });
    });
 
    describe('array', () => {
      test('increments from 0 to -3', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '/0',
          inc: -3,
        };
        const result = applyOperations([0], [operation]).doc;
        expect(result).toEqual([-3]);
      });
 
      test('increments from -0 to -3', () => {
        const operation: OperationInc = {
          op: 'inc',
          path: '/0',
          inc: -3,
        };
        const result = applyOperations([-0], [operation]).doc;
        expect(result).toEqual([-3]);
      });
    });
  });
};