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

Add more tests #7

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions arrayStringMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ describe("Empty map", () => {
it ("Empty map entries returns empty array", () => {
assert([...arrayStringMap.entries()].length === 0);
})
it ("Empty map forEach", () => {
arrayStringMap.forEach(() => {
assert(false, "forEach should not be called");
});
})
})

describe("Map with one object", () => {
Expand Down Expand Up @@ -97,6 +102,16 @@ describe("Map with one object", () => {
// works as expected
assert(copiedMap._converterInfo.size === 0, "Converter map size is 0");
})
it ("Map forEach is called once", () => {
let count = 0;
arrayStringMap.forEach((value, key, map) => {
count++;
assert(value === sampleValue1, "Value is sampleValue1");
assert(key === sampleArray1, "Key is sampleArray1");
assert(map === arrayStringMap, "Map is arrayStringMap");
});
assert(count === 1, "ForEach is called once");
})
})

describe("Map with one object and different separator", () => {
Expand Down Expand Up @@ -138,6 +153,16 @@ describe("Map with one object and alternate array", () => {
assert([...arrayStringMap.values()].length === 1, "Array length is 1");
assert([...arrayStringMap.values()][0] === sampleValue2, "Value is sampleValue2");
})
it ("Map forEach is called once", () => {
let count = 0;
arrayStringMap.forEach((value, key, map) => {
count++;
assert(value === sampleValue2, "Value is sampleValue2");
assert(key === sampleArray2, "Key is sampleArray2");
assert(map === arrayStringMap, "Map is arrayStringMap");
});
assert(count === 1, "ForEach is called once");
})
})

describe("Map with two objects", () => {
Expand Down Expand Up @@ -222,4 +247,20 @@ describe("Map with two objects", () => {
// works as expected
assert(copiedMap._converterInfo.size === 1, "Converter map size is 1");
})
it("Map forEach is called twice", () => {
let count = 0;
arrayStringMap.forEach((value, key, map) => {
count++;
assert(map === arrayStringMap, "Map is arrayStringMap");
if (count === 0){
assert(key === sampleArray1, "Key is sampleArray1");
assert(value === sampleValue1, "Value is sampleValue1");
}
else if (count === 1){
assert(key === sampleArray3, "Key is sampleArray3");
assert(value === sampleValue2, "Value is sampleValue2");
}
});
assert(count === 2, "ForEach is called twice");
})
})
2 changes: 1 addition & 1 deletion arrayStringMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class ArrayStringMap<K extends any[], V> implements Map<K, V> {
}
}

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