Update hasKey snippet to handle failing scenario and add new tests

This commit is contained in:
Rohit Rathi
2019-10-17 23:14:59 +05:30
parent 29b217e5bc
commit d85a2dc5d7
2 changed files with 40 additions and 24 deletions

View File

@ -1,7 +1,10 @@
const {hasKey} = require('./_30s.js');
const data = {
a: 1, b: { c: 4 }, 'd.e': 5
a: 1,
b: { c: 4 },
'd.e': 5,
'b.d': 2,
};
test('hasKey is a Function', () => {
@ -9,25 +12,33 @@ test('hasKey is a Function', () => {
});
test('hasKey returns true for simple property', () => {
expect(hasKey(data, 'a')).toBe(true);
expect(hasKey(data, ['a'])).toBe(true);
});
test('hasKey returns true for object property', () => {
expect(hasKey(data, 'b')).toBe(true);
expect(hasKey(data, ['b'])).toBe(true);
});
test('hasKey returns true for nested property', () => {
expect(hasKey(data, 'b.c')).toBe(true);
expect(hasKey(data, ['b', 'c'])).toBe(true);
});
test('hasKey returns true for property with dots', () => {
expect(hasKey(data, 'd.e')).toBe(true);
expect(hasKey(data, ['d.e'])).toBe(true);
});
test('hasKey returns true for property with dots and same sibling key', () => {
expect(hasKey(data, ['b.d'])).toBe(true);
});
test('hasKey returns false for non-existent property', () => {
expect(hasKey(data, 'f')).toBe(true);
expect(hasKey(data, ['f'])).toBe(false);
});
test('hasKey returns false for virtual nested property', () => {
expect(hasKey(data, 'd')).toBe(false);
expect(hasKey(data, ['d'])).toBe(false);
});
test('hasKey returns false for empty key list', () => {
expect(hasKey(data, [])).toBe(false);
});