-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
1e7d53c
commit 8e14994
Showing
4 changed files
with
50 additions
and
0 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,8 +1,41 @@ | ||
import test from 'ava'; | ||
import cliSpinners from '.'; | ||
|
||
function mockMathRandom(fixedResult) { | ||
unMockMathRandom(); | ||
const originalImplementation = Math.random; | ||
Math.random = () => fixedResult; | ||
Math.random.originalImplementation = originalImplementation; | ||
} | ||
|
||
function unMockMathRandom() { | ||
if (Math.random.originalImplementation) { | ||
Math.random = Math.random.originalImplementation; | ||
} | ||
} | ||
|
||
test('main', t => { | ||
t.is(typeof cliSpinners, 'object'); | ||
t.is(cliSpinners.dots.interval, 80); | ||
t.true(Array.isArray(cliSpinners.dots.frames)); | ||
}); | ||
|
||
test('random getter', t => { | ||
const spinnersList = Object.keys(cliSpinners) | ||
// TODO: Remove this filter when "module.exports.default = spinners" is removed from index.js. | ||
.filter(key => key !== 'default') | ||
.map(key => cliSpinners[key]); | ||
|
||
// Should always return an item from the spinners list. | ||
t.true(spinnersList.includes(cliSpinners.random)); | ||
|
||
// Should return the first spinner when `Math.random()` is the minimum value. | ||
mockMathRandom(0); | ||
t.is(cliSpinners.random, spinnersList[0]); | ||
|
||
mockMathRandom(0.99); | ||
// Should return the last spinner when `Math.random()` is the maximum value. | ||
t.is(cliSpinners.random, spinnersList[spinnersList.length - 1]); | ||
|
||
unMockMathRandom(); | ||
}); |