diff --git a/arrayStringMap.test.ts b/arrayStringMap.test.ts index 021656d..f9aaa42 100644 --- a/arrayStringMap.test.ts +++ b/arrayStringMap.test.ts @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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"); + }) }) \ No newline at end of file diff --git a/arrayStringMap.ts b/arrayStringMap.ts index 2b58681..c0fe4f2 100644 --- a/arrayStringMap.ts +++ b/arrayStringMap.ts @@ -50,7 +50,7 @@ export default class ArrayStringMap implements Map { } } - forEach(callbackfn: (value: V, key: K, map: Map) => void, thisArg?: any): void { + forEach(callbackfn: (value: V, key: K, map: ArrayStringMap) => 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.