-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: edge case when calling done after delay (#252)
- Loading branch information
Showing
4 changed files
with
76 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Removes first found element from array | ||
* WARNING: Mutates array | ||
* | ||
* @param {any[]} array | ||
* @param {any} element | ||
*/ | ||
const removeElementFromArray = (array, element) => { | ||
const index = array.findIndex(item => item === element); | ||
if (index !== -1) { | ||
array.splice(index, 1); | ||
} | ||
|
||
return array; | ||
}; | ||
|
||
export default removeElementFromArray; |
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,32 @@ | ||
import _ from 'lodash'; | ||
import removeElementFromArray from '.'; | ||
|
||
describe('Tests removeElementFromArray', () => { | ||
describe('When found', () => { | ||
it('Should return the same array', () => { | ||
const arr = [1, 2, 3]; | ||
|
||
expect(removeElementFromArray(arr, 3)).toBe(arr); | ||
}); | ||
|
||
it('Should remove found element', () => { | ||
const foundElement = {}; | ||
|
||
const arr = [1, 2, foundElement]; | ||
expect(removeElementFromArray(arr, foundElement)).toEqual([1, 2]); | ||
}); | ||
}); | ||
|
||
describe('When not found', () => { | ||
it('Should keep array unchanged', () => { | ||
const arr = [1, 2, {}, null]; | ||
|
||
expect(removeElementFromArray(_.cloneDeep(arr), 777)).toEqual(arr); | ||
}); | ||
|
||
it('Should return the same array', () => { | ||
const arr = [1, 2, {}, null]; | ||
expect(removeElementFromArray(arr, 777)).toBe(arr); | ||
}); | ||
}); | ||
}); |