|
| 1 | +import { isEqual, diffMerge } from './viewDiff' |
| 2 | + |
| 3 | +describe('isEqual', () => { |
| 4 | + it('should return true for identical primitives', () => { |
| 5 | + expect(isEqual(1, 1)).toBe(true) |
| 6 | + expect(isEqual('a', 'a')).toBe(true) |
| 7 | + expect(isEqual(true, true)).toBe(true) |
| 8 | + expect(isEqual(null, null)).toBe(true) |
| 9 | + expect(isEqual(undefined, undefined)).toBe(true) |
| 10 | + }) |
| 11 | + |
| 12 | + it('should return false for different primitives', () => { |
| 13 | + expect(isEqual(1, 2)).toBe(false) |
| 14 | + expect(isEqual('a', 'b')).toBe(false) |
| 15 | + expect(isEqual(true, false)).toBe(false) |
| 16 | + expect(isEqual(null, undefined)).toBe(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should return true for deeply equal objects', () => { |
| 20 | + expect(isEqual({ a: 1, b: { c: 2 } }, { a: 1, b: { c: 2 } })).toBe(true) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should return false for objects with different values', () => { |
| 24 | + expect(isEqual({ a: 1 }, { a: 2 })).toBe(false) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should return false for objects with different keys', () => { |
| 28 | + expect(isEqual({ a: 1 }, { b: 1 })).toBe(false) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should return true for objects with same keys in different order', () => { |
| 32 | + expect(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should return true for equal arrays', () => { |
| 36 | + expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return false for arrays with different lengths', () => { |
| 40 | + expect(isEqual([1, 2], [1, 2, 3])).toBe(false) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should return false for arrays with different values', () => { |
| 44 | + expect(isEqual([1, 2, 3], [1, 2, 4])).toBe(false) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return false when comparing array to non-array', () => { |
| 48 | + expect(isEqual([1], { 0: 1 })).toBe(false) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should return false for type mismatch', () => { |
| 52 | + expect(isEqual(1, '1')).toBe(false) |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe('diffMerge', () => { |
| 57 | + it('should return undefined when there are no changes', () => { |
| 58 | + const result = diffMerge({ a: 1, b: 'x' }, { a: 1, b: 'x' }) |
| 59 | + expect(result).toBeUndefined() |
| 60 | + }) |
| 61 | + |
| 62 | + it('should return changed primitive fields', () => { |
| 63 | + const result = diffMerge({ a: 1, b: 2 }, { a: 1, b: 1 }) |
| 64 | + expect(result).toEqual({ b: 2 }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should include new fields not present in lastSent', () => { |
| 68 | + const result = diffMerge({ a: 1, b: 2 }, { a: 1 }) |
| 69 | + expect(result).toEqual({ b: 2 }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should set null for deleted keys', () => { |
| 73 | + const result = diffMerge({ a: 1 }, { a: 1, b: 2 }) |
| 74 | + expect(result).toEqual({ b: null }) |
| 75 | + }) |
| 76 | + |
| 77 | + it('should recursively diff nested objects', () => { |
| 78 | + const result = diffMerge({ nested: { x: 1, y: 2 } }, { nested: { x: 1, y: 1 } }) |
| 79 | + expect(result).toEqual({ nested: { y: 2 } }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should return undefined for unchanged nested objects', () => { |
| 83 | + const result = diffMerge({ nested: { x: 1 } }, { nested: { x: 1 } }) |
| 84 | + expect(result).toBeUndefined() |
| 85 | + }) |
| 86 | + |
| 87 | + it('should include new nested objects', () => { |
| 88 | + const result = diffMerge({ nested: { x: 1 } }, {}) |
| 89 | + expect(result).toEqual({ nested: { x: 1 } }) |
| 90 | + }) |
| 91 | + |
| 92 | + describe('replaceKeys option', () => { |
| 93 | + it('should use full replace strategy for specified keys', () => { |
| 94 | + const result = diffMerge({ arr: [1, 2, 3] }, { arr: [1, 2] }, { replaceKeys: new Set(['arr']) }) |
| 95 | + expect(result).toEqual({ arr: [1, 2, 3] }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('should not include replace key if unchanged', () => { |
| 99 | + const result = diffMerge({ arr: [1, 2] }, { arr: [1, 2] }, { replaceKeys: new Set(['arr']) }) |
| 100 | + expect(result).toBeUndefined() |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + describe('appendKeys option', () => { |
| 105 | + it('should append only new trailing elements for array keys', () => { |
| 106 | + const result = diffMerge({ items: [1, 2, 3] }, { items: [1, 2] }, { appendKeys: new Set(['items']) }) |
| 107 | + expect(result).toEqual({ items: [3] }) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should include full array when it first appears', () => { |
| 111 | + const result = diffMerge({ items: [1, 2] }, {}, { appendKeys: new Set(['items']) }) |
| 112 | + expect(result).toEqual({ items: [1, 2] }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should not include append key if array has not grown', () => { |
| 116 | + const result = diffMerge({ items: [1, 2] }, { items: [1, 2] }, { appendKeys: new Set(['items']) }) |
| 117 | + expect(result).toBeUndefined() |
| 118 | + }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments