From a8fabad30160e542dfc3c25862fb40db54c9cde6 Mon Sep 17 00:00:00 2001 From: ronenk1 Date: Sun, 8 Dec 2024 15:33:14 +0200 Subject: [PATCH 1/3] fix: support null valued config attributes --- src/utils/helpers.ts | 2 +- tests/helpers.spec.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/helpers.spec.ts diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 6a19a4c..902a341 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -2,7 +2,7 @@ export function deepFreeze(obj: T): void { const propNames = Object.getOwnPropertyNames(obj); for (const name of propNames) { const value = obj[name as keyof T]; - if (typeof value === 'object') { + if (typeof value === 'object' && value !== null) { deepFreeze(value); } } diff --git a/tests/helpers.spec.ts b/tests/helpers.spec.ts new file mode 100644 index 0000000..bf1703f --- /dev/null +++ b/tests/helpers.spec.ts @@ -0,0 +1,35 @@ +import { deepFreeze } from '../src/utils/helpers'; + +describe('helpers', () => { + describe('#deepFreeze', () => { + it('should return object freeze', () => { + const data = { + name: 'I am parent', + child: { + name: 'I am child', + }, + }; + deepFreeze(data); + + const action = () => { + data.name = 'i try to change'; // try to change freezed object + }; + expect(action).toThrow(/Cannot assign to read only property/); + }); + + it('should return object freeze with null value', () => { + const data = { + name: 'I am parent', + child: { + name: null, + }, + }; + deepFreeze(data); + + const action = () => { + data.name = 'i try to change'; // try to change freezed object + }; + expect(action).toThrow(/Cannot assign to read only property/); + }); + }); +}); From 410ec55d6ea944a1f1e5c22f8c4abffbb6bcf653 Mon Sep 17 00:00:00 2001 From: ronenkapelian <72082238+ronenkapelian@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:13:49 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Ofer <12687466+CptSchnitz@users.noreply.github.com> --- tests/helpers.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/helpers.spec.ts b/tests/helpers.spec.ts index bf1703f..d5ed8c4 100644 --- a/tests/helpers.spec.ts +++ b/tests/helpers.spec.ts @@ -2,7 +2,7 @@ import { deepFreeze } from '../src/utils/helpers'; describe('helpers', () => { describe('#deepFreeze', () => { - it('should return object freeze', () => { + it('should return frozen object', () => { const data = { name: 'I am parent', child: { @@ -17,7 +17,7 @@ describe('helpers', () => { expect(action).toThrow(/Cannot assign to read only property/); }); - it('should return object freeze with null value', () => { + it('should return frozen object with nested null value without error', () => { const data = { name: 'I am parent', child: { From 49e6d2835ddca24eb767b4880ffb9c7215992aae Mon Sep 17 00:00:00 2001 From: ronenk1 Date: Wed, 11 Dec 2024 09:49:17 +0200 Subject: [PATCH 3/3] test: add and fix relevant tests --- tests/helpers.spec.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tests/helpers.spec.ts b/tests/helpers.spec.ts index d5ed8c4..a409ffb 100644 --- a/tests/helpers.spec.ts +++ b/tests/helpers.spec.ts @@ -1,3 +1,4 @@ +import 'jest-extended'; import { deepFreeze } from '../src/utils/helpers'; describe('helpers', () => { @@ -9,27 +10,34 @@ describe('helpers', () => { name: 'I am child', }, }; + deepFreeze(data); + expect(data).toBeFrozen(); + }); - const action = () => { - data.name = 'i try to change'; // try to change freezed object + it('should freeze the input object that include null valued property without error', () => { + const data = { + name: null, + child: { + name: 'i am child', + }, }; - expect(action).toThrow(/Cannot assign to read only property/); + + deepFreeze(data); + expect(data).toBeFrozen(); }); - it('should return frozen object with nested null value without error', () => { + it('should freeze the input object include nested property', () => { const data = { name: 'I am parent', child: { - name: null, + name: 'I am child', }, }; - deepFreeze(data); - const action = () => { - data.name = 'i try to change'; // try to change freezed object - }; - expect(action).toThrow(/Cannot assign to read only property/); + deepFreeze(data); + const child = data.child; + expect(child).toBeFrozen(); }); }); });