File: test.js

package info (click to toggle)
node-cli-spinners 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 644 kB
  • sloc: javascript: 124; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,324 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const test = require('tape');
const cliSpinners = require('.');

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;
	}
}

console.log('Spinner count:', Object.keys(cliSpinners).length);

test('main', t => {
	t.is(typeof cliSpinners, 'object');
	t.is(cliSpinners.dots.interval, 80);
	t.true(Array.isArray(cliSpinners.dots.frames));
	t.end();
});

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();
	t.end();
});