Skip to content

Commit

Permalink
fix: support null valued config attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenkapelian committed Dec 8, 2024
1 parent 4d781b5 commit a8fabad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export function deepFreeze<T>(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);
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -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/);
});
});
});

0 comments on commit a8fabad

Please sign in to comment.