-
-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathcaptains-log.spec.js
More file actions
86 lines (72 loc) · 1.94 KB
/
captains-log.spec.js
File metadata and controls
86 lines (72 loc) · 1.94 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { describe, expect, test, beforeEach, afterEach } from '@jest/globals';
import {
randomShipRegistryNumber,
randomStardate,
randomPlanetClass,
} from './captains-log';
describe('randomShipRegistryNumber', () => {
test('registry numbers are valid', () => {
for (let i = 0; i < 4; i++) {
expect(randomShipRegistryNumber()).toMatch(/NCC-[1-9][0-9]{3}/);
}
});
test('is a random registry number', () => {
expect(randomShipRegistryNumber()).not.toEqual(randomShipRegistryNumber());
});
});
function loadDie(...values) {
const originalRandom = Math.random;
Math.random = function loadedDie() {
if (values.length === 0) {
return originalRandom();
}
return values.shift();
};
return () => {
Math.random = originalRandom;
};
}
describe('randomStardate', () => {
let restore;
beforeEach(() => {
const min = 0;
const max = 1 - Number.EPSILON * 32;
// prettier-ignore
restore = loadDie(
min, min, min, min, min, min,
max, max, max, max, max, max,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5
);
});
afterEach(() => {
if (restore) {
restore();
restore = undefined;
}
});
test('stardate is between 41000 and 42000', () => {
for (let i = 0; i < 10_000; i++) {
const starDate = randomStardate();
expect(starDate).toBeGreaterThanOrEqual(41_000);
expect(starDate).toBeLessThan(42_000);
}
});
});
describe('randomPlanetClass', () => {
test('planet classes are valid', () => {
const expected = 'DHJKLMNRTY';
for (let i = 0; i < 1_000; i++) {
const actual = randomPlanetClass();
expect(expected).toContain(actual);
}
});
test('all planet classes can be returned', () => {
const expected = 'DHJKLMNRTY';
const seen = {};
for (let i = 0; i < 1_000; i++) {
const actual = randomPlanetClass();
seen[actual] = true;
}
expect(Object.keys(seen).length).toBe(expected.length);
});
});