|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @emails oncall+javascript_foundation |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +describe('DeltaPatcher', () => { |
| 14 | + const window = (global.window = {}); |
| 15 | + require('../DeltaPatcher'); |
| 16 | + |
| 17 | + it('should initialize to an empty bundle', () => { |
| 18 | + global.Date = jest.fn(); |
| 19 | + const dp = new window.DeltaPatcher(); |
| 20 | + expect(dp.getLastRevisionId()).toBe(undefined); |
| 21 | + expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]); |
| 22 | + expect(dp.getLastNumModifiedFiles()).toBe(0); |
| 23 | + // Empty pre and post. |
| 24 | + expect(dp.getAllModules()).toEqual(['', '']); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should expect a base bundle at initialization', () => { |
| 28 | + const dp = new window.DeltaPatcher(); |
| 29 | + expect(() => { |
| 30 | + dp.applyDelta({ |
| 31 | + base: false, |
| 32 | + revisionId: 'hello', |
| 33 | + modules: [], |
| 34 | + deleted: [], |
| 35 | + }); |
| 36 | + }).toThrow(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should accept a base bundle at initialization', () => { |
| 40 | + const dp = new window.DeltaPatcher(); |
| 41 | + global.Date = jest.fn(); |
| 42 | + dp.applyDelta({ |
| 43 | + base: true, |
| 44 | + revisionId: 'rev0', |
| 45 | + pre: 'pre0', |
| 46 | + post: 'post0', |
| 47 | + modules: [[0, '__d(0);']], |
| 48 | + }); |
| 49 | + expect(dp.getLastRevisionId()).toBe('rev0'); |
| 50 | + expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]); |
| 51 | + expect(dp.getLastNumModifiedFiles()).toBe(1); |
| 52 | + expect(dp.getAllModules()).toEqual(['pre0', '__d(0);', 'post0']); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should accept a delta bundle after a base bundle', () => { |
| 56 | + const dp = new window.DeltaPatcher(); |
| 57 | + dp.applyDelta({ |
| 58 | + base: true, |
| 59 | + revisionId: 'rev0', |
| 60 | + pre: 'pre0', |
| 61 | + post: 'post0', |
| 62 | + modules: [[0, '__d(0);'], [1, '__d(1);'], [2, '__d(2);']], |
| 63 | + }); |
| 64 | + global.Date = jest.fn(); |
| 65 | + dp.applyDelta({ |
| 66 | + base: false, |
| 67 | + revisionId: 'rev1', |
| 68 | + modules: [[1, '__d(1.1);'], [3, '__d(3);']], |
| 69 | + deleted: [0], |
| 70 | + }); |
| 71 | + expect(dp.getLastRevisionId()).toBe('rev1'); |
| 72 | + expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]); |
| 73 | + expect(dp.getLastNumModifiedFiles()).toBe(3); |
| 74 | + expect(dp.getAllModules()).toEqual([ |
| 75 | + 'pre0', |
| 76 | + '__d(1.1);', |
| 77 | + '__d(2);', |
| 78 | + '__d(3);', |
| 79 | + 'post0', |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should accept a base bundle after initialization', () => { |
| 84 | + const dp = new window.DeltaPatcher(); |
| 85 | + dp.applyDelta({ |
| 86 | + base: true, |
| 87 | + revisionId: 'rev0', |
| 88 | + pre: 'pre0', |
| 89 | + post: 'post0', |
| 90 | + modules: [[0, '__d(0);'], [1, '__d(1);'], [2, '__d(2);']], |
| 91 | + }); |
| 92 | + dp.applyDelta({ |
| 93 | + base: false, |
| 94 | + revisionId: 'rev1', |
| 95 | + modules: [[1, '__d(1.1);'], [3, '__d(3);']], |
| 96 | + deleted: [0], |
| 97 | + }); |
| 98 | + global.Date = jest.fn(); |
| 99 | + dp.applyDelta({ |
| 100 | + base: true, |
| 101 | + revisionId: 'rev2', |
| 102 | + pre: 'pre2', |
| 103 | + post: 'post2', |
| 104 | + modules: [[4, '__d(4);'], [5, '__d(5);']], |
| 105 | + }); |
| 106 | + expect(dp.getLastRevisionId()).toBe('rev2'); |
| 107 | + expect(dp.getLastModifiedDate()).toBe(global.Date.mock.instances[0]); |
| 108 | + expect(dp.getLastNumModifiedFiles()).toBe(2); |
| 109 | + expect(dp.getAllModules()).toEqual(['pre2', '__d(4);', '__d(5);', 'post2']); |
| 110 | + }); |
| 111 | +}); |
0 commit comments