-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
858166f
commit da7d4ec
Showing
5 changed files
with
119 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import type { ISetLike } from "./ISetLike"; | ||
import type { ISetLike } from './ISetLike'; | ||
|
||
export function isSetLike<TItem>(maybeSetLike: any): maybeSetLike is ISetLike<TItem> { | ||
return ( | ||
typeof maybeSetLike.size === "number" | ||
&& typeof maybeSetLike.has === "function" | ||
&& typeof maybeSetLike.keys === "function" | ||
typeof maybeSetLike.size === 'number' | ||
&& typeof maybeSetLike.has === 'function' | ||
&& typeof maybeSetLike.keys === 'function' | ||
); | ||
} |
101 changes: 101 additions & 0 deletions
101
src/collections/observableSet/tests/ObservableSet.forEach.tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { ObservableSet } from '../ObservableSet'; | ||
import { testBlankMutatingOperation } from './common'; | ||
|
||
describe('ObserableSet.forEach', (): void => { | ||
it('iterating over an empty set does not invoke the callback', (): void => { | ||
let setInvocationCount = 0; | ||
let observableSetInvocationCount = 0; | ||
|
||
testBlankMutatingOperation<number>({ | ||
initialState: [], | ||
|
||
applyOperation: { | ||
applySetOperation: set => set.forEach(_ => setInvocationCount++), | ||
applyObservableSetOperation: observableSet => observableSet.forEach(_ => observableSetInvocationCount++) | ||
}, | ||
|
||
expectedResult: undefined | ||
}); | ||
|
||
expect(setInvocationCount).toBe(0); | ||
expect(observableSetInvocationCount).toBe(0); | ||
}); | ||
|
||
it('iterating over a set invokes the callback for each item', (): void => { | ||
const setItems: number[] = []; | ||
const observableSetItems: number[] = []; | ||
|
||
testBlankMutatingOperation<number>({ | ||
initialState: [1, 2, 3], | ||
|
||
applyOperation: { | ||
applySetOperation: set => set.forEach(item => setItems.push(item)), | ||
applyObservableSetOperation: observableSet => observableSet.forEach(item => observableSetItems.push(item)) | ||
}, | ||
|
||
expectedResult: undefined | ||
}); | ||
|
||
expect(observableSetItems).toEqual(setItems); | ||
}); | ||
|
||
it('calling forEach passes arguments to each parameter accordingly', (): void => { | ||
let invocationCount = 0; | ||
const observableSet = new ObservableSet<number>([1]); | ||
observableSet.forEach((item, key, set) => { | ||
invocationCount++; | ||
|
||
expect(item).toBe(1); | ||
expect(key).toBe(item); | ||
expect(set).toStrictEqual(observableSet); | ||
|
||
return true; | ||
}); | ||
|
||
expect(invocationCount).toBe(1); | ||
}); | ||
|
||
it('calling forEach with context passes it to the callback', (): void => { | ||
let invocationCount = 0; | ||
const context = {}; | ||
const observableSet = new ObservableSet<number>([1]); | ||
observableSet.forEach( | ||
function (item, key, set) { | ||
invocationCount++; | ||
|
||
expect(this).toStrictEqual(context); | ||
expect(item).toBe(1); | ||
expect(key).toBe(item); | ||
expect(set).toStrictEqual(observableSet); | ||
|
||
return true; | ||
}, | ||
context | ||
); | ||
|
||
expect(invocationCount).toBe(1); | ||
}); | ||
|
||
it('modifying the set while executing forEach throws exception', (): void => { | ||
expect( | ||
() => { | ||
const observableSet = new ObservableSet<number>([1, 2, 3]); | ||
observableSet.forEach(_ => { | ||
observableSet.clear(); | ||
}); | ||
}) | ||
.toThrow(new Error('Set has changed while being iterated.')); | ||
}); | ||
|
||
it('calling forEach while iterating will not break iterators', (): void => { | ||
expect( | ||
() => { | ||
const observableSet = new ObservableSet<number>([1, 2, 3]); | ||
|
||
for (const _ of observableSet) | ||
observableSet.forEach(_ => {}); | ||
}) | ||
.not | ||
.toThrow(); | ||
}); | ||
}); |