Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nikaplezhou committed Aug 11, 2023
1 parent 6d8eec6 commit 8275767
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/deep-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ describe('deepSet', () => {
});

it('should not set value if obj is not an object', () => {
expect(deepSet('test', 'key', 'value')).toBe('test');
expect(deepSet('test' as any, 'key', 'value')).toBe('test');
});
});
18 changes: 14 additions & 4 deletions src/flatten.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,19 @@ describe('flattenObject', () => {
);
});

it('should handle circular dependency', () => {
it('should handle circular dependency, circularReference = string', () => {
const obj1 = {} as Record<string, any>;
obj1.a = obj1;
const target1 = { a: '[Circular->""]' };
expect(flatten(obj1)).toEqual(target1);
expect(unflatten(target1)).toEqual(obj1);

const obj2 = { a: { b: { e: 1 } }, c: {} } as Record<string, any>;
});
it('should handle circular dependency, circularReference = symbol', () => {
const obj2 = { a: { b: { e: '1' } }, c: {} } as Record<string, any>;
obj2.c.d = obj2.a.b;
const flattened2 = flatten(obj2, { circularReference: 'symbol' });
const target2 = {
'a.b.e': 1,
'a.b.e': '1',
'c.d': `Symbol([Circular->"a.b"])`,
};
expect({
Expand All @@ -139,6 +140,15 @@ describe('flattenObject', () => {
expect(unflatten(target2, { circularReference: 'symbol' })).toEqual(obj2);
});

it('should handle circular dependency, circularReference = null', () => {
const obj3 = {} as Record<string, any>;
obj3.a = obj3;
expect(flatten(obj3, { circularReference: 'null' })).toEqual({ a: null });
expect(
unflatten({ a: null, b: '1' }, { circularReference: 'null' }),
).toEqual({ a: null, b: '1' });
});

it('should not throw on illegal input', () => {
expect(flatten('' as any)).toEqual({});
});
Expand Down

0 comments on commit 8275767

Please sign in to comment.