Skip to content

Commit 6d89fb2

Browse files
committed
getters + setters
1 parent 86c8341 commit 6d89fb2

File tree

10 files changed

+563
-264
lines changed

10 files changed

+563
-264
lines changed

dist/slotmachine.js

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/slotmachine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/slot-machine.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ module.exports = class SlotMachine {
202202
* @return {Number} - Element index
203203
*/
204204
get prevIndex () {
205-
return this.direction === 'up' ? this._nextIndex : this._prevIndex;
205+
return this.direction.key === 'up' ? this._nextIndex : this._prevIndex;
206206
}
207207

208208
/**
209209
* @desc PUBLIC - Get the next element
210210
* @return {Number} - Element index
211211
*/
212212
get nextIndex () {
213-
return this.direction === 'up' ? this._prevIndex : this._nextIndex;
213+
return this.direction.key === 'up' ? this._prevIndex : this._nextIndex;
214214
}
215215

216216
/**
@@ -219,11 +219,13 @@ module.exports = class SlotMachine {
219219
* @return {Number} - Returns true if machine is on the screen
220220
*/
221221
get visible () {
222-
const above = this.element.offsetTop > window.scrollY + window.innerHeight;
223-
const below = window.scrollY > this.element.innerHeight + this.element.offsetTop;
222+
const rect = this.element.getBoundingClientRect();
223+
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
224+
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
225+
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
226+
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
224227

225-
// Stop animation if element is [above||below] screen, best for performance
226-
return !above && !below;
228+
return vertInView && horInView;
227229
}
228230

229231
/**

tests/acceptance/acceptance-test.js

Lines changed: 0 additions & 248 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* eslint-disable no-undef */
2+
3+
describe('Constructor', () => {
4+
let machine;
5+
let shuffleSpy;
6+
let autoSpy;
7+
8+
beforeEach(() => {
9+
shuffleSpy = sinon.spy(SlotMachine.prototype, 'shuffle');
10+
autoSpy = sinon.spy(SlotMachine.prototype, 'auto');
11+
});
12+
13+
afterEach(() => {
14+
shuffleSpy.restore();
15+
autoSpy.restore();
16+
if (machine) {
17+
machine.element.remove();
18+
}
19+
});
20+
21+
it('has element', () => {
22+
machine = render();
23+
const element = document.getElementById(id);
24+
25+
expect(machine.element).to.be.equal(element);
26+
});
27+
28+
it('element does not have overflow', () => {
29+
machine = render();
30+
31+
expect(machine.element.style.overflow).to.be.equal('hidden');
32+
});
33+
34+
it('has settings', () => {
35+
machine = render();
36+
37+
expect(machine.settings).to.exist;
38+
});
39+
40+
[
41+
{ active: 0, result: 0 },
42+
{ active: 1, result: 1 },
43+
{ active: 99, result: 0 },
44+
{ active: -99, result: 0 },
45+
{ active: '0', result: 0 },
46+
{ active: {}, result: 0 },
47+
{ active: null, result: 0 },
48+
{ active: undefined, result: 0 }
49+
].forEach((testCase) => {
50+
it(`sets active: ${testCase.active}`, () => {
51+
machine = render({
52+
active: testCase.active
53+
});
54+
55+
expect(machine.active).to.be.equal(testCase.result);
56+
});
57+
});
58+
59+
it('wraps tiles and adds offsets', () => {
60+
machine = render();
61+
62+
expect(machine.container.classList.contains('slotMachineContainer')).to.be.true;
63+
expect(machine.container.children).to.have.lengthOf(5);
64+
});
65+
66+
['up', 'down'].forEach((direction) => {
67+
it(`sets direction: ${direction}`, () => {
68+
machine = render({
69+
direction: direction
70+
});
71+
72+
expect(machine.direction.key).to.be.equal(direction);
73+
});
74+
});
75+
76+
it('does not auto start', () => {
77+
machine = render();
78+
79+
expect(shuffleSpy).to.not.have.been.called;
80+
expect(autoSpy).to.not.have.been.called;
81+
});
82+
83+
it('shuffles when auto is set to true', () => {
84+
machine = render({
85+
auto: true
86+
});
87+
88+
expect(shuffleSpy).to.have.been.called;
89+
expect(autoSpy).to.not.have.been.called;
90+
});
91+
92+
it('run auto when auto is set to number', () => {
93+
machine = render({
94+
auto: 1
95+
});
96+
97+
expect(shuffleSpy).to.not.have.been.called;
98+
expect(autoSpy).to.have.been.called;
99+
});
100+
});

0 commit comments

Comments
 (0)