|
| 1 | +import { describe, it, expect } from '@jest/globals'; |
| 2 | +import { _cloneObject, cloneBumpInfo } from '../../publish/cloneBumpInfo'; |
| 3 | +import type { BumpInfo } from '../../types/BumpInfo'; |
| 4 | +import { makePackageInfos } from '../../__fixtures__/packageInfos'; |
| 5 | +import { getChange } from '../../__fixtures__/changeFiles'; |
| 6 | + |
| 7 | +describe('_cloneObject', () => { |
| 8 | + it.each<[string, object | unknown[]]>([ |
| 9 | + ['empty object', {}], |
| 10 | + ['empty object with null prototype', Object.create(null)], |
| 11 | + ['object', { a: 1, b: '2', c: true }], |
| 12 | + ['object with null prototype', Object.assign(Object.create(null), { a: 1, b: '2', c: true })], |
| 13 | + ['empty array', []], |
| 14 | + ['array', [1, '2', true]], |
| 15 | + ['set', new Set([1, 2, 3])], |
| 16 | + ['object of sets', { a: new Set([1, 2, 3]), b: new Set(['a', 'b', 'c']) }], |
| 17 | + ])('clones %s', (desc, val) => { |
| 18 | + const cloned = _cloneObject(val); |
| 19 | + expect(cloned).toEqual(val); |
| 20 | + expect(cloned).not.toBe(val); |
| 21 | + }); |
| 22 | + |
| 23 | + it('deeply clones nested object', () => { |
| 24 | + const orig = { a: { b: { c: 1 } } }; |
| 25 | + const cloned = _cloneObject(orig); |
| 26 | + expect(cloned).toEqual(orig); |
| 27 | + expect(cloned).not.toBe(orig); |
| 28 | + expect(cloned.a).not.toBe(orig.a); |
| 29 | + expect(cloned.a.b).not.toBe(orig.a.b); |
| 30 | + }); |
| 31 | + |
| 32 | + it('deep clones array of objects and arrays', () => { |
| 33 | + const orig = [{ a: 1 }, [2, 3, 4]]; |
| 34 | + const cloned = _cloneObject(orig); |
| 35 | + expect(cloned).toEqual(orig); |
| 36 | + expect(cloned).not.toBe(orig); |
| 37 | + expect(cloned[0]).not.toBe(orig[0]); |
| 38 | + expect(cloned[1]).not.toBe(orig[1]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('throws on other object types', () => { |
| 42 | + expect(() => _cloneObject(new Date())).toThrow('Unsupported object type found while cloning bump info: Date'); |
| 43 | + expect(() => _cloneObject(/abc/)).toThrow('Unsupported object type found while cloning bump info: RegExp'); |
| 44 | + expect(() => _cloneObject(new Map())).toThrow('Unsupported object type found while cloning bump info: Map'); |
| 45 | + class Foo {} |
| 46 | + expect(() => _cloneObject(new Foo())).toThrow('Unsupported object type found while cloning bump info: Foo'); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('cloneBumpInfo', () => { |
| 51 | + it('clones bump info structure', () => { |
| 52 | + const original: BumpInfo = { |
| 53 | + // There's no attempt at consistency because it doesn't matter here |
| 54 | + calculatedChangeTypes: { pkgA: 'minor', pkgB: 'patch' }, |
| 55 | + packageInfos: makePackageInfos({ a: { dependencies: { b: '^1.0.0' } }, b: {} }), |
| 56 | + changeFileChangeInfos: [ |
| 57 | + { change: getChange('a'), changeFile: '' }, |
| 58 | + { change: getChange('b'), changeFile: '' }, |
| 59 | + ], |
| 60 | + packageGroups: { group1: { packageNames: ['a', 'b'], disallowedChangeTypes: null } }, |
| 61 | + dependentChangedBy: { a: new Set(['b']) }, |
| 62 | + modifiedPackages: new Set(['a']), |
| 63 | + scopedPackages: new Set(['a', 'b']), |
| 64 | + }; |
| 65 | + |
| 66 | + const cloned = cloneBumpInfo(original); |
| 67 | + expect(cloned).toEqual(original); |
| 68 | + expect(cloned).not.toBe(original); |
| 69 | + expect(cloned.packageInfos).not.toBe(original.packageInfos); |
| 70 | + expect(cloned.packageInfos.a).not.toBe(original.packageInfos.a); |
| 71 | + expect(cloned.changeFileChangeInfos).not.toBe(original.changeFileChangeInfos); |
| 72 | + expect(cloned.changeFileChangeInfos[0]).not.toBe(original.changeFileChangeInfos[0]); |
| 73 | + expect(cloned.changeFileChangeInfos[0].change).not.toBe(original.changeFileChangeInfos[0].change); |
| 74 | + expect(cloned.packageGroups).not.toBe(original.packageGroups); |
| 75 | + expect(cloned.dependentChangedBy).not.toBe(original.dependentChangedBy); |
| 76 | + expect(cloned.dependentChangedBy.a).not.toBe(original.dependentChangedBy.a); |
| 77 | + expect(cloned.modifiedPackages).not.toBe(original.modifiedPackages); |
| 78 | + expect(cloned.scopedPackages).not.toBe(original.scopedPackages); |
| 79 | + }); |
| 80 | +}); |
0 commit comments