Skip to content

Commit 715dd51

Browse files
RajPrakash681denniskigen
authored andcommitted
test: add unit tests for type guards and lodash.unset fix
- Add tests to verify type guards prevent TypeError on null/undefined values - Add tests to verify immutable state update pattern with lodash.unset - Ensures changes follow best practices and prevent store corruption
1 parent 6b99055 commit 715dd51

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
3+
describe('config-subtree type guards', () => {
4+
it('should handle null value without throwing TypeError', () => {
5+
const value = null;
6+
const isLeaf = value && typeof value === 'object' && Object.hasOwn(value, '_value');
7+
expect(isLeaf).toBe(false);
8+
});
9+
10+
it('should handle undefined value without throwing TypeError', () => {
11+
const value = undefined;
12+
const isLeaf = value && typeof value === 'object' && Object.hasOwn(value, '_value');
13+
expect(isLeaf).toBe(false);
14+
});
15+
16+
it('should return true for object with _value property', () => {
17+
const value = { _value: 'test', _source: 'test' };
18+
const isLeaf = value && typeof value === 'object' && Object.hasOwn(value, '_value');
19+
expect(isLeaf).toBe(true);
20+
});
21+
22+
it('should return false for object without _value property', () => {
23+
const value = { nested: { _value: 'test' } };
24+
const isLeaf = value && typeof value === 'object' && Object.hasOwn(value, '_value');
25+
expect(isLeaf).toBe(false);
26+
});
27+
28+
it('should return false for primitive values', () => {
29+
const stringValue = 'test';
30+
const numberValue = 123;
31+
const boolValue = true;
32+
33+
const isLeafString = stringValue && typeof stringValue === 'object' && Object.hasOwn(stringValue as any, '_value');
34+
const isLeafNumber = numberValue && typeof numberValue === 'object' && Object.hasOwn(numberValue as any, '_value');
35+
const isLeafBool = boolValue && typeof boolValue === 'object' && Object.hasOwn(boolValue as any, '_value');
36+
37+
expect(isLeafString).toBe(false);
38+
expect(isLeafNumber).toBe(false);
39+
expect(isLeafBool).toBe(false);
40+
});
41+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, it, expect } from '@jest/globals';
2+
import { cloneDeep, unset } from 'lodash-es';
3+
4+
describe('lodash.unset immutable pattern', () => {
5+
it('should properly use unset with immutable state updates', () => {
6+
const initialState = {
7+
config: {
8+
'@openmrs/mario': {
9+
hasHat: true,
10+
numberFingers: 8,
11+
},
12+
},
13+
};
14+
15+
// Clone first, mutate the clone, then use the clone
16+
const state = cloneDeep(initialState);
17+
const result = unset(state, ['config', '@openmrs/mario', 'hasHat']);
18+
19+
// unset returns true if the property existed
20+
expect(result).toBe(true);
21+
22+
// The cloned state should be mutated
23+
expect(state.config['@openmrs/mario'].hasHat).toBeUndefined();
24+
25+
// The original state should be unchanged
26+
expect(initialState.config['@openmrs/mario'].hasHat).toBe(true);
27+
28+
// The mutated state should still have other properties
29+
expect(state.config['@openmrs/mario'].numberFingers).toBe(8);
30+
});
31+
32+
it('should return false when unsetting non-existent property', () => {
33+
const initialState = {
34+
config: {
35+
'@openmrs/mario': {
36+
hasHat: true,
37+
},
38+
},
39+
};
40+
41+
const state = cloneDeep(initialState);
42+
const result = unset(state, ['config', '@openmrs/mario', 'nonExistent']);
43+
44+
// unset returns false if the property did not exist
45+
expect(result).toBe(false);
46+
});
47+
48+
it('should handle nested property removal correctly', () => {
49+
const initialState = {
50+
config: {
51+
'@openmrs/mario': {
52+
weapons: {
53+
gloves: 2,
54+
parasol: 1,
55+
},
56+
},
57+
},
58+
};
59+
60+
const state = cloneDeep(initialState);
61+
unset(state, ['config', '@openmrs/mario', 'weapons', 'gloves']);
62+
63+
expect(state.config['@openmrs/mario'].weapons.gloves).toBeUndefined();
64+
expect(state.config['@openmrs/mario'].weapons.parasol).toBe(1);
65+
});
66+
});

0 commit comments

Comments
 (0)