Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 9aa1953

Browse files
committed
Add more tests
1 parent 2b30852 commit 9aa1953

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

arrayStringMap.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe("Empty map", () => {
3131
it ("Empty map entries returns empty array", () => {
3232
assert([...arrayStringMap.entries()].length === 0);
3333
})
34+
it ("Empty map forEach", () => {
35+
arrayStringMap.forEach(() => {
36+
assert(false, "forEach should not be called");
37+
});
38+
})
3439
})
3540

3641
describe("Map with one object", () => {
@@ -97,6 +102,16 @@ describe("Map with one object", () => {
97102
// works as expected
98103
assert(copiedMap._converterInfo.size === 0, "Converter map size is 0");
99104
})
105+
it ("Map forEach is called once", () => {
106+
let count = 0;
107+
arrayStringMap.forEach((value, key, map) => {
108+
count++;
109+
assert(value === sampleValue1, "Value is sampleValue1");
110+
assert(key === sampleArray1, "Key is sampleArray1");
111+
assert(map === arrayStringMap, "Map is arrayStringMap");
112+
});
113+
assert(count === 1, "ForEach is called once");
114+
})
100115
})
101116

102117
describe("Map with one object and different separator", () => {
@@ -138,6 +153,16 @@ describe("Map with one object and alternate array", () => {
138153
assert([...arrayStringMap.values()].length === 1, "Array length is 1");
139154
assert([...arrayStringMap.values()][0] === sampleValue2, "Value is sampleValue2");
140155
})
156+
it ("Map forEach is called once", () => {
157+
let count = 0;
158+
arrayStringMap.forEach((value, key, map) => {
159+
count++;
160+
assert(value === sampleValue2, "Value is sampleValue2");
161+
assert(key === sampleArray2, "Key is sampleArray2");
162+
assert(map === arrayStringMap, "Map is arrayStringMap");
163+
});
164+
assert(count === 1, "ForEach is called once");
165+
})
141166
})
142167

143168
describe("Map with two objects", () => {
@@ -222,4 +247,20 @@ describe("Map with two objects", () => {
222247
// works as expected
223248
assert(copiedMap._converterInfo.size === 1, "Converter map size is 1");
224249
})
250+
it("Map forEach is called twice", () => {
251+
let count = 0;
252+
arrayStringMap.forEach((value, key, map) => {
253+
count++;
254+
assert(map === arrayStringMap, "Map is arrayStringMap");
255+
if (count === 0){
256+
assert(key === sampleArray1, "Key is sampleArray1");
257+
assert(value === sampleValue1, "Value is sampleValue1");
258+
}
259+
else if (count === 1){
260+
assert(key === sampleArray3, "Key is sampleArray3");
261+
assert(value === sampleValue2, "Value is sampleValue2");
262+
}
263+
});
264+
assert(count === 2, "ForEach is called twice");
265+
})
225266
})

arrayStringMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class ArrayStringMap<K extends any[], V> implements Map<K, V> {
5050
}
5151
}
5252

53-
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
53+
forEach(callbackfn: (value: V, key: K, map: ArrayStringMap<K, V>) => void, thisArg?: any): void {
5454
this._converterInfo.forEach((value, key) => {
5555
// TypeScript complains that this will be undefined, but the items in
5656
// `this._converterInfo` and `this._map` will always be defined in each other.

0 commit comments

Comments
 (0)