Skip to content

Commit

Permalink
test: rewrite tests to match new version
Browse files Browse the repository at this point in the history
  • Loading branch information
josex2r committed Nov 3, 2022
1 parent 5ea5a01 commit 9504537
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 269 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ tests
webpack.config.js
jest.config.js
coverage
public
docs
28 changes: 3 additions & 25 deletions tests/acceptance/constructor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import SlotMachine, { Direction } from '../../lib/slot-machine';
import SlotMachine from '../../lib';
import { MACHINE_ID, render } from '../setup';

describe('Constructor', () => {
let machine: SlotMachine;
let shuffleSpy;
let runSpy;

beforeEach(() => {
shuffleSpy = jest.spyOn(SlotMachine.prototype, 'shuffle');
runSpy = jest.spyOn(SlotMachine.prototype, 'run');
});

afterEach(() => {
Expand Down Expand Up @@ -49,33 +47,13 @@ describe('Constructor', () => {
it('wraps tiles and adds offsets', () => {
machine = render();

expect(machine.container.classList.contains('slotMachineContainer')).toBeTruthy();
expect(machine.container.children.length).toBe(5);
});

(['up', 'down'] as Direction[]).forEach((direction) => {
it(`sets direction: ${direction}`, () => {
machine = render({
direction: direction,
});

expect(machine.direction).toBe(direction);
});
});

it('sets randomize', () => {
const randomize = () => 1;
machine = render({
randomize,
});

expect(machine.randomize).toBe(randomize);
expect(machine.container.element.classList.contains('slot-machine__container')).toBeTruthy();
expect(machine.container.element.children.length).toBe(5);
});

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

expect(shuffleSpy).not.toHaveBeenCalled();
expect(runSpy).not.toHaveBeenCalled();
});
});
113 changes: 14 additions & 99 deletions tests/acceptance/getters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SlotMachine, { Direction } from '../../lib/slot-machine';
import SlotMachine from '../../lib';
import { Direction } from '../../lib/types';
import { getVisibleTile, render } from '../setup';

describe('Getters', () => {
Expand All @@ -17,36 +18,22 @@ describe('Getters', () => {
});
const tile = getVisibleTile(machine);

expect(machine.visibleTile).toBe(index);
expect(machine.active).toBe(index);
expect(tile?.innerHTML).toBe(text);
expect(tile?.element?.innerHTML).toBe(text);
});
});
});

describe('random', () => {
it(`gets random index between min and max tiles length`, () => {
machine = render();

for (let i = 0; i < 1000; i++) {
const random = machine.random;

expect(random).toBeGreaterThanOrEqual(0);
expect(random).toBeLessThanOrEqual(2);
}
});
});

describe('custom', () => {
it(`receives active element when calling randomize`, () => {
const randomize = jest.fn().mockReturnValue(1);

machine = render({
randomize,
});
machine.shuffle(2);

expect(machine.custom).toBeGreaterThanOrEqual(0);
expect(randomize).toHaveBeenCalledWith(machine.active);
expect(randomize).toHaveBeenCalledWith(machine.active, 3);
});

it(`gets custom element from randomize function`, () => {
Expand All @@ -56,60 +43,32 @@ describe('Getters', () => {
machine = render({
randomize,
});
machine.shuffle(2);

expect(machine.custom).toBe(index);
expect(randomize).toHaveBeenCalled();
});

[-1, 9].forEach((index) => {
it(`sets 0 when custom element is out of bounds: ${index}`, () => {
const randomize = jest.fn().mockReturnValue(index);

machine = render({
randomize,
});

expect(machine.custom).toBe(0);
expect(randomize).toHaveBeenCalled();
});
});

it(`gets random element`, () => {
machine = render();

for (let i = 0; i < 1000; i++) {
const custom = machine.custom;

expect(custom).toBeGreaterThanOrEqual(0);
expect(custom).toBeLessThanOrEqual(2);
}
});
});

describe('Direction', () => {
[
{
direction: 'up' as Direction,
result: {
key: 'up',
initial: -20,
first: 0,
last: -80,
to: -60,
firstToLast: -80,
lastToFirst: 0,
from: -60,
to: 0,
nextReset: 0,
prevReset: -80,
},
},
{
direction: 'down' as Direction,
result: {
key: 'down',
initial: -20,
first: -80,
last: 0,
to: -20,
firstToLast: -80,
lastToFirst: 0,
from: -20,
to: -80,
nextReset: 0,
prevReset: -80,
},
},
].forEach((testCase) => {
Expand Down Expand Up @@ -160,48 +119,4 @@ describe('Getters', () => {
expect(machine.prevIndex).toBe(1);
});
});

describe('visible', () => {
it('is visible', () => {
machine = render();

expect(machine.visible).toBeTruthy();
});

it.skip('is not visible when "top" is out of bounds', () => {
machine = render();

machine.element.style.position = 'absolute';
machine.element.style.top = '-100px';

expect(machine.visible).toBeFalsy();
});

it.skip('is not visible when "bottom" is out of bounds', () => {
machine = render();

machine.element.style.position = 'absolute';
machine.element.style.bottom = '-100px';

expect(machine.visible).toBeFalsy();
});

it.skip('is not visible when "left" is out of bounds', () => {
machine = render();

machine.element.style.position = 'absolute';
machine.element.style.left = '-100px';

expect(machine.visible).toBeFalsy();
});

it.skip('is not visible when "right" is out of bounds', () => {
machine = render();

machine.element.style.position = 'absolute';
machine.element.style.right = '-100px';

expect(machine.visible).toBeFalsy();
});
});
});
Loading

0 comments on commit 9504537

Please sign in to comment.