Skip to content

Commit 11b0c43

Browse files
committed
test: use jest
1 parent 08bac3b commit 11b0c43

21 files changed

+2519
-3042
lines changed

jest.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const config = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'jsdom',
4+
transform: {
5+
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
6+
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
7+
'^.+\\.tsx?$': [
8+
'ts-jest',
9+
{
10+
// ts-jest configuration goes here
11+
},
12+
],
13+
},
14+
};
15+
16+
module.exports = config;

lib/slot-machine.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ export type Bound = {
1919
export type Bounds = Record<Direction, Bound>;
2020

2121
export type Options = {
22-
active: number;
23-
delay: number;
24-
auto: boolean;
25-
spins: number;
22+
active?: number;
23+
delay?: number;
24+
auto?: boolean;
25+
spins?: number;
2626
randomize?: RandomizeCallback;
2727
onComplete?: OnCompleteCallback;
28-
inViewport: boolean;
29-
direction: Direction;
30-
transition: string;
28+
inViewport?: boolean;
29+
direction?: Direction;
30+
transition?: string;
3131
};
3232

3333
const defaults: Options = {
@@ -101,7 +101,7 @@ export default class SlotMachine implements Options {
101101
// Show active element
102102
this._resetPosition();
103103
// Start auto animation
104-
if (this.auto !== false) {
104+
if (this.auto) {
105105
this.run();
106106
}
107107
}

package.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"node": ">= 16"
66
},
77
"scripts": {
8-
"acceptance": "testem ci",
98
"prepare": "husky install",
109
"start": "webpack-dev-server --mode development",
1110
"build": "webpack --mode production",
12-
"test": "yarn build && npm run acceptance && npm run unit",
13-
"unit": "mocha tests/unit/*-test.js",
11+
"test": "jest",
12+
"test:watch": "jest --watch",
13+
"test:cov": "jest --cov",
1414
"lint": "eslint .",
1515
"prettier": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,ts,json,md,yaml,yml}\""
1616
},
@@ -33,17 +33,14 @@
3333
"babel-preset-env": "^1.6.1",
3434
"babelify": "^8.0.0",
3535
"browserify": "^16.1.1",
36-
"chai": "^4.1.2",
3736
"eslint": "^8.23.0",
3837
"husky": "^8.0.0",
39-
"mocha": "^5.0.4",
38+
"jest": "^29.2.0",
39+
"jest-environment-jsdom": "^29.2.0",
4040
"open": "0.0.5",
4141
"prettier": "^2.7.1",
4242
"run-sequence": "^2.2.1",
43-
"sinon": "^4.4.6",
44-
"sinon-chai": "^3.0.0",
45-
"sinon-dist": "^1.15.4",
46-
"testem": "^2.0.0",
43+
"ts-jest": "^29.0.3",
4744
"ts-loader": "^9.4.1",
4845
"tsconfig-paths": "^3.9.0",
4946
"typescript": "^4.1.3",

testem.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/acceptance/constructor-test.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

tests/acceptance/constructor.spec.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import SlotMachine, { Direction } from '../../lib/slot-machine';
2+
import { MACHINE_ID, render } from '../setup';
3+
4+
describe('Constructor', () => {
5+
let machine: SlotMachine;
6+
let shuffleSpy;
7+
let runSpy;
8+
9+
beforeEach(() => {
10+
shuffleSpy = jest.spyOn(SlotMachine.prototype, 'shuffle');
11+
runSpy = jest.spyOn(SlotMachine.prototype, 'run');
12+
});
13+
14+
afterEach(() => {
15+
jest.clearAllMocks();
16+
machine.element?.remove();
17+
});
18+
19+
it('has element', () => {
20+
machine = render();
21+
22+
const element = document.getElementById(MACHINE_ID);
23+
24+
expect(machine.element).toBe(element);
25+
});
26+
27+
it('element does not have overflow', () => {
28+
machine = render();
29+
30+
expect(machine.element.style.overflow).toBe('hidden');
31+
});
32+
33+
[
34+
{ active: 0, result: 0 },
35+
{ active: 1, result: 1 },
36+
{ active: 99, result: 0 },
37+
{ active: -99, result: 0 },
38+
{ active: undefined, result: 0 },
39+
].forEach((testCase) => {
40+
it(`sets active: ${testCase.active}`, () => {
41+
machine = render({
42+
active: testCase.active,
43+
});
44+
45+
expect(machine.active).toBe(testCase.result);
46+
});
47+
});
48+
49+
it('wraps tiles and adds offsets', () => {
50+
machine = render();
51+
52+
expect(machine.container.classList.contains('slotMachineContainer')).toBeTruthy();
53+
expect(machine.container.children.length).toBe(5);
54+
});
55+
56+
(['up', 'down'] as Direction[]).forEach((direction) => {
57+
it(`sets direction: ${direction}`, () => {
58+
machine = render({
59+
direction: direction,
60+
});
61+
62+
expect(machine.direction).toBe(direction);
63+
});
64+
});
65+
66+
it('sets randomize', () => {
67+
const randomize = () => 1;
68+
machine = render({
69+
randomize,
70+
});
71+
72+
expect(machine.randomize).toBe(randomize);
73+
});
74+
75+
it('does not auto start', () => {
76+
machine = render();
77+
78+
expect(shuffleSpy).not.toHaveBeenCalled();
79+
expect(runSpy).not.toHaveBeenCalled();
80+
});
81+
});

0 commit comments

Comments
 (0)