-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
161 lines (104 loc) · 4.16 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const expect = require('chai').expect;
const { hasKeys, hasAnyKey } = require('./lib');
describe('ALL KEYS: Simple Positive Test', () => {
it ('should return true for single string key', () => {
const object = { key: 'value' };
const result = hasKeys(object, 'key');
expect(result).to.equal(true);
});
it ('should return true for array of single key', () => {
const object = { key: 'value' };
const result = hasKeys(object, ['key']);
expect(result).to.equal(true);
});
it ('should return true for array of keys', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, ['key1', 'key2']);
expect(result).to.equal(true);
});
});
describe('ALL KEYS: Simple Negative Test', () => {
it ('should return false for single string key', () => {
const object = { key: 'value' };
const result = hasKeys(object, 'key1');
expect(result).to.equal(false);
});
it ('should return false for array of single key', () => {
const object = { key: 'value' };
const result = hasKeys(object, ['key1']);
expect(result).to.equal(false);
});
it ('should return true for array of keys', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, ['key0', 'key3']);
expect(result).to.equal(false);
});
});
describe('ALL KEYS: Various use cases', () => {
it ('should throw error when input is not object', () => {
const object = 'Hello';
expect(() => hasKeys(object, ['key'])).to.throw(Error);
});
it ('should return true when object has may keys and one key is called', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, ['key1']);
expect(result).to.equal(true);
});
it ('should return true when object has may keys and some keys are called', () => {
const object = { key1: 'value', key2: 'value', key3: 'value' };
const result = hasKeys(object, ['key1', 'key3']);
expect(result).to.equal(true);
});
it ('should return false when object has may keys and no keys are called', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, ['key3']);
expect(result).to.equal(false);
});
it ('should return false when object has may keys and some keys are called and some aren\'t', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, ['key1', 'key3']);
expect(result).to.equal(false);
});
it ('should return false when called key is undefined', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object);
expect(result).to.equal(false);
});
it ('should return false when called key is null', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, null);
expect(result).to.equal(false);
});
it ('should return true when called key is empty Array', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, []);
expect(result).to.equal(true);
});
it ('should return false when called key is object', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasKeys(object, {key: 'value'});
expect(result).to.equal(false);
});
it ('should return true when object has function', () => {
const object = { key1: 'value', key2: 'value', key3: f => f };
const result = hasKeys(object, 'key1');
expect(result).to.equal(true);
});
it ('should return true when object has function and same key is called', () => {
const object = { key1: 'value', key2: 'value', key3: f => f };
const result = hasKeys(object, 'key3');
expect(result).to.equal(true);
});
});
describe('ANY KEY: Simple Test', () => {
it ('should return true if any key exists', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasAnyKey(object, ['key3', 'key1']);
expect(result).to.equal(true);
});
it ('should return false if no keys exist', () => {
const object = { key1: 'value', key2: 'value' };
const result = hasAnyKey(object, ['key3', 'key4']);
expect(result).to.equal(false);
});
});