exMap.test.js
7.53 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint-disable no-undefined */
'use strict';
var Map = require('../src/js/map');
var ExMap = require('../src/js/exMap');
describe('module:ExMap', function() {
var map;
beforeEach(function() {
map = new ExMap();
});
describe('The tui.util.ExMap', function() {
it('is defined', function() {
expect(ExMap).toBeDefined();
});
it('is a constructor', function() {
expect(map instanceof ExMap).toBe(true);
});
describe('has an argument', function() {
it('that can be an array', function() {
var initData = [
[1, 'one'],
[2, 'two'],
[3, 'three']
];
map = new ExMap(initData);
expect(map.get(1)).toBe('one');
expect(map.get(2)).toBe('two');
expect(map.get(3)).toBe('three');
});
});
});
describe('methods from tui.util.Map', function() {
describe('set() and get()', function() {
it('for the string key', function() {
map.set('company', 'nhn ent');
map.set('team', 'FE');
expect(map.get('company')).toEqual('nhn ent');
expect(map.get('team')).toEqual('FE');
});
it('for the object key', function() {
var key1 = {},
key2 = function() {},
key3 = [];
map.set(key1, 'object');
map.set(key2, 'function');
map.set(key3, 'array');
expect(map.get(key1)).toEqual('object');
expect(map.get(key2)).toEqual('function');
expect(map.get(key3)).toEqual('array');
});
it('set returns map object', function() {
var returned = map.set(1, 'one');
expect(returned).toBe(map);
});
});
describe('has()', function() {
it('returns true if the key exists', function() {
map.set(1, 'one');
expect(map.has(1)).toBe(true);
});
it('returns false if the key does not exists', function() {
expect(map.has(1)).toBe(false);
});
});
describe('delete() removes the element', function() {
var key = {};
it('for the object key', function() {
map.set(key, 'value');
map['delete'](key);
expect(map.has(key)).toBe(false);
});
it('delete and set again', function() {
map.set(key, 'once');
map['delete'](key);
map.set(key, 'again');
expect(map.get(key)).toBe('again');
});
});
describe('size property', function() {
it('is the number of elements in a map', function() {
expect(map.size).toEqual(0);
map.set(1, 'one');
map.set(2, 'two');
expect(map.size).toEqual(2);
map.set(2, 'two again');
expect(map.size).toEqual(2);
map['delete'](2);
expect(map.size).toEqual(1);
});
});
describe('clear()', function() {
it('removes all elements', function() {
map.set(1, 'one');
map.set(2, 'two');
expect(map.size).toEqual(2);
map.clear();
expect(map.size).toEqual(0);
expect(map.has(1)).toBe(false);
expect(map.has(2)).toBe(false);
});
});
describe('forEach()', function() {
it('executes a function once per each key/value pair in insertion order', function() {
var string = '';
map.set(1, '1');
map.set(null, '2');
map.set('3', '3');
map.forEach(function(value) {
string += value;
});
expect(string).toEqual('123');
});
});
describe('keys(), values(), entries() returns Iterator object in insertion order', function() {
beforeEach(function() {
map.set(null, '1');
map.set(undefined, '2');
map.set('3', '3');
});
describe('Iterator from keys()', function() {
it('contains the keys for each element', function() {
var keys = map.keys();
expect(keys.next().value).toBe(null);
expect(keys.next().value).toBe(undefined);
expect(keys.next().value).toBe('3');
});
});
describe('Iterator from values()', function() {
it('contains the values for each element', function() {
var values = map.values();
expect(values.next().value).toBe('1');
expect(values.next().value).toBe('2');
expect(values.next().value).toBe('3');
});
});
describe('Iterator from values()', function() {
it('contains the values for each element', function() {
var entries = map.entries();
expect(entries.next().value).toEqual([null, '1']);
expect(entries.next().value).toEqual([undefined, '2']);
expect(entries.next().value).toEqual(['3', '3']);
});
});
});
});
describe('setObject()', function() {
it('sets the each key/value pair in the object to the map.', function() {
map.setObject({
'name': 'kim',
'company': 'apple'
});
expect(map.get('name')).toBe('kim');
expect(map.get('company')).toBe('apple');
});
});
describe('deleteByKeys()', function() {
it('removes the elements associated to the each keys in the array', function() {
map.set('1', 'one');
map.set(1, 'number one');
map.set(null, 'null');
expect(map.has('1')).toBe(true);
expect(map.has(1)).toBe(true);
expect(map.has(null)).toBe(true);
map.deleteByKeys(['1', 1]);
expect(map.has('1')).toBe(false);
expect(map.has(1)).toBe(false);
expect(map.has(null)).toBe(true);
});
});
describe('merge()', function() {
it('sets all of the key-value pairs in the specified map to this map', function() {
var anotherMap = new Map();
map.set(1, 'one');
map.set(2, 'two');
anotherMap.set(2, 'second');
anotherMap.set(3, 'third');
map.merge(anotherMap);
expect(map.get(1)).toBe('one');
expect(map.get(2)).toBe('second');
expect(map.get(3)).toBe('third');
});
});
describe('filter()', function() {
it('returns new ExMap of all key-value pairs that pass a truth test', function() {
var filtered;
map.set(1, 1);
map.set(2, 'two');
map.set(3, 3);
filtered = map.filter(function(value, key) {
return value === key;
});
expect(filtered.get(1)).toBe(1);
expect(filtered.get(2)).toBeUndefined();
expect(filtered.get(3)).toBe(3);
expect(filtered.size).toBe(2);
});
});
});