-
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.
Added forEach implementation & extended every
- Loading branch information
1 parent
bbb6472
commit c1c43a7
Showing
6 changed files
with
199 additions
and
1 deletion.
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
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
86 changes: 86 additions & 0 deletions
86
src/collections/tests/ObservableCollection/ObservableCollection.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,86 @@ | ||
import { ObservableCollection } from '../../ObservableCollection'; | ||
import { testBlankMutatingOperation } from './common'; | ||
|
||
describe('ObserableCollection.forEach', (): void => { | ||
it('iterating over an empty collection does not invoke the callback', (): void => { | ||
let arrayInvocationCount = 0; | ||
let collectionInvocationCount = 0; | ||
|
||
testBlankMutatingOperation<number>({ | ||
initialState: [], | ||
|
||
applyOperation: { | ||
applyArrayOperation: array => array.forEach(_ => arrayInvocationCount++), | ||
applyCollectionOperation: collection => collection.forEach(_ => collectionInvocationCount++) | ||
} | ||
}); | ||
|
||
expect(arrayInvocationCount).toBe(0); | ||
expect(collectionInvocationCount).toBe(0); | ||
}); | ||
|
||
it('iterating over a collection invokes the callback for each item', (): void => { | ||
const arrayItems: number[] = []; | ||
const collectionItems: number[] = []; | ||
|
||
testBlankMutatingOperation<number>({ | ||
initialState: [1, 2, 3], | ||
|
||
applyOperation: { | ||
applyArrayOperation: array => array.forEach(item => arrayItems.push(item)), | ||
applyCollectionOperation: collection => collection.forEach(item => collectionItems.push(item)) | ||
} | ||
}); | ||
|
||
expect(collectionItems).toEqual(arrayItems); | ||
}); | ||
|
||
it('calling forEach passes arguments to each parameter accordingly', (): void => { | ||
let invocationCount = 0; | ||
const observableCollection = new ObservableCollection<number>(1); | ||
observableCollection.forEach((item, index, collection) => { | ||
invocationCount++; | ||
|
||
expect(item).toBe(1); | ||
expect(index).toBe(0); | ||
expect(collection).toStrictEqual(observableCollection); | ||
|
||
return true; | ||
}); | ||
|
||
expect(invocationCount).toBe(1); | ||
}); | ||
|
||
it('calling forEach with context passes it to the callback', (): void => { | ||
let invocationCount = 0; | ||
const context = {}; | ||
const observableCollection = new ObservableCollection<number>(1); | ||
observableCollection.forEach( | ||
function (item, index, collection) { | ||
invocationCount++; | ||
|
||
expect(this).toStrictEqual(context); | ||
expect(item).toBe(1); | ||
expect(index).toBe(0); | ||
expect(collection).toStrictEqual(observableCollection); | ||
|
||
return true; | ||
}, | ||
context | ||
); | ||
|
||
expect(invocationCount).toBe(1); | ||
}); | ||
|
||
it('calling forEach while iterating will not break iterators', (): void => { | ||
expect( | ||
() => { | ||
const observableCollection = new ObservableCollection<number>(1, 2, 3); | ||
|
||
for (const _ of observableCollection) | ||
observableCollection.forEach(_ => {}); | ||
}) | ||
.not | ||
.toThrow(); | ||
}); | ||
}); |