-
Notifications
You must be signed in to change notification settings - Fork 931
/
Copy pathmap.spec.js
156 lines (131 loc) · 3.74 KB
/
map.spec.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
const { HashMap, TreeMap } = require('../../index');
const mapImplementations = [
Map,
HashMap,
TreeMap,
];
mapImplementations.forEach((MapImplementation) => {
describe(`Map (common interface) with ${MapImplementation.name}`, () => {
let map;
beforeEach(() => {
map = new MapImplementation();
});
describe('#set', () => {
it('should save an object and increase size', () => {
expect(map.size).toBe(0);
map.set(1, 'test');
map.set({}, 2);
expect(map.size).toBe(2);
});
it('should replace existing key its content', () => {
map.set(1, 'test1');
map.set(1, 'test2');
expect(map.size).toBe(1);
expect(map.get(1)).toBe('test2');
});
});
describe('#get', () => {
it('should save an object and increase size', () => {
const obj = {};
map.set(1, 'test');
map.set(obj, 2);
expect(map.get(1)).toBe('test');
expect(map.get(obj)).toBe(2);
});
it('should handle when key is not present', () => {
expect(map.get(404)).toBe(undefined);
});
});
describe('#has', () => {
it('should return true when key exists', () => {
map.set(1, 1);
expect(map.has(1)).toBe(true);
});
it('should return false when key is not present', () => {
expect(map.has(1)).toBe(false);
});
});
describe('#delete', () => {
it('should return true if present', () => {
map.set(1, 1);
expect(map.delete(1)).toBe(true);
});
it('should return false if NOT present', () => {
expect(map.delete(1)).toBe(false);
});
});
describe('#keys', () => {
beforeEach(() => {
map.set(1, 2);
map.set(2, 'dos');
map.set(3, 3);
});
it('should return all keys', () => {
expect(Array.from(map.keys())).toEqual([1, 2, 3]);
});
it('should update on delete', () => {
expect(map.delete(1)).toBe(true);
expect(Array.from(map.keys())).toEqual([2, 3]);
});
it('should handle strings', () => {
map.set('uno', 1);
expect(Array.from(map.keys())).toEqual([1, 2, 3, 'uno']);
});
it('should handle objects', () => {
map.set({}, 1);
expect(Array.from(map.keys())).toEqual([1, 2, 3, {}]);
});
it('should handle null', () => {
map.set(null, 1);
expect([...map.keys()].sort()).toEqual([1, 2, 3, null].sort()); // ignoring order
});
});
describe('#values', () => {
beforeEach(() => {
map.set(1, 2);
map.set(2, 'dos');
map.set(3, 3);
});
it('should return all values', () => {
expect(Array.from(map.values())).toEqual([2, 'dos', 3]);
});
it('should update on delete', () => {
expect(map.delete(1)).toBe(true);
expect(Array.from(map.values())).toEqual(['dos', 3]);
});
});
describe('#entries', () => {
beforeEach(() => {
map.set(1, 2);
map.set(2, 'dos');
map.set(3, 3);
});
it('should return all entries', () => {
expect(Array.from(map.entries())).toEqual([
[1, 2],
[2, 'dos'],
[3, 3],
]);
});
it('should update on delete', () => {
expect(map.delete(1)).toBe(true);
expect(Array.from(map.entries())).toEqual([
[2, 'dos'],
[3, 3],
]);
});
});
describe('#clear', () => {
beforeEach(() => {
map.set(1, 2);
map.set(2, 'dos');
map.set(3, 3);
});
it('should work', () => {
expect(map.size).toBe(3);
expect(map.clear()).toEqual();
expect(map.size).toBe(0);
});
});
});
});