Skip to content

Commit 9504537

Browse files
committed
test: rewrite tests to match new version
1 parent 5ea5a01 commit 9504537

File tree

8 files changed

+52
-269
lines changed

8 files changed

+52
-269
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ tests
77
webpack.config.js
88
jest.config.js
99
coverage
10+
public
11+
docs

tests/acceptance/constructor.spec.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import SlotMachine, { Direction } from '../../lib/slot-machine';
1+
import SlotMachine from '../../lib';
22
import { MACHINE_ID, render } from '../setup';
33

44
describe('Constructor', () => {
55
let machine: SlotMachine;
66
let shuffleSpy;
7-
let runSpy;
87

98
beforeEach(() => {
109
shuffleSpy = jest.spyOn(SlotMachine.prototype, 'shuffle');
11-
runSpy = jest.spyOn(SlotMachine.prototype, 'run');
1210
});
1311

1412
afterEach(() => {
@@ -49,33 +47,13 @@ describe('Constructor', () => {
4947
it('wraps tiles and adds offsets', () => {
5048
machine = render();
5149

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);
50+
expect(machine.container.element.classList.contains('slot-machine__container')).toBeTruthy();
51+
expect(machine.container.element.children.length).toBe(5);
7352
});
7453

7554
it('does not auto start', () => {
7655
machine = render();
7756

7857
expect(shuffleSpy).not.toHaveBeenCalled();
79-
expect(runSpy).not.toHaveBeenCalled();
8058
});
8159
});

tests/acceptance/getters.spec.ts

Lines changed: 14 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import SlotMachine, { Direction } from '../../lib/slot-machine';
1+
import SlotMachine from '../../lib';
2+
import { Direction } from '../../lib/types';
23
import { getVisibleTile, render } from '../setup';
34

45
describe('Getters', () => {
@@ -17,36 +18,22 @@ describe('Getters', () => {
1718
});
1819
const tile = getVisibleTile(machine);
1920

20-
expect(machine.visibleTile).toBe(index);
2121
expect(machine.active).toBe(index);
22-
expect(tile?.innerHTML).toBe(text);
22+
expect(tile?.element?.innerHTML).toBe(text);
2323
});
2424
});
2525
});
2626

27-
describe('random', () => {
28-
it(`gets random index between min and max tiles length`, () => {
29-
machine = render();
30-
31-
for (let i = 0; i < 1000; i++) {
32-
const random = machine.random;
33-
34-
expect(random).toBeGreaterThanOrEqual(0);
35-
expect(random).toBeLessThanOrEqual(2);
36-
}
37-
});
38-
});
39-
4027
describe('custom', () => {
4128
it(`receives active element when calling randomize`, () => {
4229
const randomize = jest.fn().mockReturnValue(1);
4330

4431
machine = render({
4532
randomize,
4633
});
34+
machine.shuffle(2);
4735

48-
expect(machine.custom).toBeGreaterThanOrEqual(0);
49-
expect(randomize).toHaveBeenCalledWith(machine.active);
36+
expect(randomize).toHaveBeenCalledWith(machine.active, 3);
5037
});
5138

5239
it(`gets custom element from randomize function`, () => {
@@ -56,60 +43,32 @@ describe('Getters', () => {
5643
machine = render({
5744
randomize,
5845
});
46+
machine.shuffle(2);
5947

60-
expect(machine.custom).toBe(index);
6148
expect(randomize).toHaveBeenCalled();
6249
});
63-
64-
[-1, 9].forEach((index) => {
65-
it(`sets 0 when custom element is out of bounds: ${index}`, () => {
66-
const randomize = jest.fn().mockReturnValue(index);
67-
68-
machine = render({
69-
randomize,
70-
});
71-
72-
expect(machine.custom).toBe(0);
73-
expect(randomize).toHaveBeenCalled();
74-
});
75-
});
76-
77-
it(`gets random element`, () => {
78-
machine = render();
79-
80-
for (let i = 0; i < 1000; i++) {
81-
const custom = machine.custom;
82-
83-
expect(custom).toBeGreaterThanOrEqual(0);
84-
expect(custom).toBeLessThanOrEqual(2);
85-
}
86-
});
8750
});
8851

8952
describe('Direction', () => {
9053
[
9154
{
9255
direction: 'up' as Direction,
9356
result: {
94-
key: 'up',
9557
initial: -20,
96-
first: 0,
97-
last: -80,
98-
to: -60,
99-
firstToLast: -80,
100-
lastToFirst: 0,
58+
from: -60,
59+
to: 0,
60+
nextReset: 0,
61+
prevReset: -80,
10162
},
10263
},
10364
{
10465
direction: 'down' as Direction,
10566
result: {
106-
key: 'down',
10767
initial: -20,
108-
first: -80,
109-
last: 0,
110-
to: -20,
111-
firstToLast: -80,
112-
lastToFirst: 0,
68+
from: -20,
69+
to: -80,
70+
nextReset: 0,
71+
prevReset: -80,
11372
},
11473
},
11574
].forEach((testCase) => {
@@ -160,48 +119,4 @@ describe('Getters', () => {
160119
expect(machine.prevIndex).toBe(1);
161120
});
162121
});
163-
164-
describe('visible', () => {
165-
it('is visible', () => {
166-
machine = render();
167-
168-
expect(machine.visible).toBeTruthy();
169-
});
170-
171-
it.skip('is not visible when "top" is out of bounds', () => {
172-
machine = render();
173-
174-
machine.element.style.position = 'absolute';
175-
machine.element.style.top = '-100px';
176-
177-
expect(machine.visible).toBeFalsy();
178-
});
179-
180-
it.skip('is not visible when "bottom" is out of bounds', () => {
181-
machine = render();
182-
183-
machine.element.style.position = 'absolute';
184-
machine.element.style.bottom = '-100px';
185-
186-
expect(machine.visible).toBeFalsy();
187-
});
188-
189-
it.skip('is not visible when "left" is out of bounds', () => {
190-
machine = render();
191-
192-
machine.element.style.position = 'absolute';
193-
machine.element.style.left = '-100px';
194-
195-
expect(machine.visible).toBeFalsy();
196-
});
197-
198-
it.skip('is not visible when "right" is out of bounds', () => {
199-
machine = render();
200-
201-
machine.element.style.position = 'absolute';
202-
machine.element.style.right = '-100px';
203-
204-
expect(machine.visible).toBeFalsy();
205-
});
206-
});
207122
});

0 commit comments

Comments
 (0)