-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
153 lines (126 loc) · 5.37 KB
/
test.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {expect} from 'chai'
import SlideTutorial from './slide-tutorial.js'
import {jsdom} from 'node-jsdom';
if (typeof document === 'undefined') {
global.document = jsdom("<html><head></head><body></body></html>");
global.window = document.parentWindow;
global.navigator = {userAgent: 'node.js'};
}
describe('SlideTutorial', () => {
let tutorial, elemIndex;
it('exists', () => {
expect(SlideTutorial).to.be.ok
})
beforeEach(() => {
tutorial = new SlideTutorial({slides: [
{image: 'http://placehold.it/400X200', title: 'Welcome', content: 'Lorem ipsum'},
{image: 'http://placehold.it/400X200', title: 'Second Slide', content: 'Lorem ipsum'},
{image: 'http://placehold.it/400X200', title: 'Third Slide', content: 'Lorem ipsum'}
], nextButtonText: 'Continue', finishButtonText: 'Get Started'});
tutorial.start()
elemIndex = tutorial.elemIndex;
})
afterEach(() => {
const tutorials = document.body.querySelectorAll('.slide-tutorial')
for (let i = 0; i < tutorials.length; i++) {
document.body.removeChild(tutorials[i])
}
tutorial = null;
})
it('is creatable', () => {
expect(tutorial).to.be.ok
})
describe('start', () => {
it('adds an active class to the el', () => {
tutorial.start()
expect(tutorial.el.classList.contains('active')).to.be.truthy
})
it('starts at the first slide', () => {
const activeSlide = tutorial.elemIndex(tutorial.slides.activeSlide())
expect(activeSlide).to.equal(0)
})
})
describe('finish', () => {
it('removes the active class from the el', () => {
tutorial.start().finish()
expect(tutorial.el.classList.contains('active')).to.be.falsy
})
})
describe('destroy', () => {
it('removes tutorial from the dom', () => {
expect(document.body.querySelectorAll('.slide-tutorial').length).to.equal(1)
tutorial.destroy()
expect(document.body.querySelectorAll('.slide-tutorial').length).to.equal(0)
})
})
describe('setSlide', () => {
it('adds an active class to the slide of the specified index', () => {
tutorial.setSlide(2)
const activeSlide = document.querySelectorAll('.slide-tutorial-slide.active')[0]
expect(elemIndex(activeSlide)).to.equal(2)
})
})
describe('elemIndex', () => {
it('returns the index of a dom element in it\'s parent', () => {
const aDiv = document.createElement('div')
aDiv.innerHTML = `<span></span><span id='test-me'><p></p></span><span></span>`
document.body.appendChild(aDiv)
expect(tutorial.elemIndex(document.getElementById('test-me'))).to.equal(1)
document.body.removeChild(aDiv)
})
})
describe('next button text', () => {
it('changes it\'s content to finishButtonText when on the last slide', () => {
let nextButtonText = () => tutorial.el.querySelector('.slide-tutorial-next-button').innerHTML
expect(nextButtonText()).to.equal(tutorial.nextButtonText)
tutorial.setSlide(+tutorial.slidesData.length - 1)
expect(nextButtonText()).to.equal(tutorial.finishButtonText)
tutorial.setSlide(0)
expect(nextButtonText()).to.equal(tutorial.nextButtonText)
})
})
describe('Slides', () => {
it('exists', () => {
expect(tutorial.slides).to.be.ok
})
describe('activeSlide', () => {
it('returns the index of the currently active slide', () => {
tutorial.setSlide(2)
expect(elemIndex(tutorial.slides.activeSlide())).to.equal(2)
})
})
describe('setConstantHeight (default = true)', () => {
it('sets the content area heights to be the same, equal to the largest', () => {
const bottoms = [].slice.call(
tutorial.el.querySelectorAll('.slide-tutorial-slide-bottom')
)
const heights = bottoms.map(elem => elem.getBoundingClientRect().height)
const isEqualToLargest = h => Math.floor(h) === 61 // largest height in test data
expect(heights.every(isEqualToLargest)).to.equal(true)
})
})
describe('activate', () => {
it('activates a slide according the the bool argument', () => {
let activeIndex = () => elemIndex(tutorial.slides.activeSlide())
expect(activeIndex()).to.equal(0)
tutorial.slides.activate(0, false)
tutorial.slides.activate(2, true)
expect(activeIndex()).to.equal(2)
})
})
})
describe('Dots', () => {
it('exists', () => {
expect(tutorial.dots).to.be.ok
})
describe('activate', () => {
it('activates a dot according the the bool argument', () => {
let activeDot = () => tutorial.dots.el.querySelectorAll('.slide-tutorial-dot.active')[0]
expect(elemIndex(activeDot())).to.equal(0)
tutorial.dots.activate(0, false)
tutorial.dots.activate(1, true)
expect(elemIndex(activeDot())).to.equal(1)
})
})
})
})