UNPKG

deep-freeze-plus

Version:

An ultra-fast and efficient library for deeply freezing JavaScript objects and preventing unintended mutations. Supports all data types, including complex objects like maps and sets, and guarantees immutability without recursion. Ideal for applications th

73 lines (64 loc) 2.57 kB
import { expect } from 'chai'; import deepFreeze from '../index.js'; describe('deepFreeze', () => { it('should freeze a simple object', () => { const obj = {a: 1, b: 2}; const frozenObj = deepFreeze(obj); expect(frozenObj).to.deep.equal(obj); expect(Object.isFrozen(frozenObj)).to.be.true; }); it('should freeze a simple array', () => { const arr = [1, 2, 3]; const frozenArr = deepFreeze(arr); expect(frozenArr).to.deep.equal(arr); expect(Object.isFrozen(frozenArr)).to.be.true; }); it('should freeze a complex object', () => { const obj = {a: [1, 2], b: {c: {d: 3}}}; const frozenObj = deepFreeze(obj); expect(frozenObj).to.deep.equal(obj); expect(Object.isFrozen(frozenObj)).to.be.true; expect(Object.isFrozen(obj.a)).to.be.true; expect(Object.isFrozen(obj.b)).to.be.true; expect(Object.isFrozen(obj.b.c)).to.be.true; }); it('should freeze a complex array', () => { const arr = [[1, 2], {a: {b: [3, 4]}}]; const frozenArr = deepFreeze(arr); expect(frozenArr).to.deep.equal(arr); expect(Object.isFrozen(frozenArr)).to.be.true; expect(Object.isFrozen(arr[0])).to.be.true; expect(Object.isFrozen(arr[1])).to.be.true; expect(Object.isFrozen(arr[1].a)).to.be.true; expect(Object.isFrozen(arr[1].a.b)).to.be.true; }); it('should freeze a Map', () => { const map = new Map([['a', 1], ['b', 2]]); const frozenMap = deepFreeze(map); expect(frozenMap).to.deep.equal(map); expect(Object.isFrozen(frozenMap)).to.be.true; expect(Object.isFrozen(map.get('a'))).to.be.true; expect(Object.isFrozen(map.get('b'))).to.be.true; }); it('should freeze a Set', () => { const set = new Set([1, 2, 3]); const frozenSet = deepFreeze(set); expect(frozenSet).to.deep.equal(set); expect(Object.isFrozen(frozenSet)).to.be.true; expect(Object.isFrozen([...set][0])).to.be.true; expect(Object.isFrozen([...set][1])).to.be.true; expect(Object.isFrozen([...set][2])).to.be.true; }); it('should not freeze primitive values', () => { expect(deepFreeze(1)).to.equal(1); expect(deepFreeze('hello')).to.equal('hello'); expect(deepFreeze(true)).to.be.true; expect(deepFreeze(null)).to.be.null; expect(deepFreeze(undefined)).to.be.undefined; }); it('should not freeze already frozen objects', () => { const obj = {a: 1}; const frozenObj = Object.freeze(obj); expect(deepFreeze(frozenObj)).to.equal(frozenObj); }); });