Skip to content

Commit 42605e4

Browse files
authored
Merge pull request #52 from vb/feature/tests
Feature/tests
2 parents 78b3ac8 + 0559611 commit 42605e4

3 files changed

Lines changed: 92 additions & 19 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"scripts": {
4141
"build": "./node_modules/.bin/rollup -c",
4242
"dev": "./node_modules/.bin/rollup -c -w",
43-
"test": "npm run build && ava"
43+
"test": "npm run build && ava -s",
44+
"test:watch": "npm run build && ava -s -w"
4445
},
4546
"files": [
4647
"dist",

src/lazyframe.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,12 @@ const Lazyframe = () => {
6565
for (let i = 0; i < selector.length; i++) {
6666
loop(selector[i]);
6767
}
68-
6968
} else if (typeof elements.length === 'undefined'){
7069
loop(elements);
71-
72-
} else if (elements.length > 1) {
73-
70+
} else {
7471
for (let i = 0; i < elements.length; i++) {
7572
loop(elements[i]);
7673
}
77-
78-
} else {
79-
loop(elements[0]);
8074
}
8175

8276
if (settings.lazyload) {

test/browser.js

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,96 @@ const script = new Script(
1212
readFileSync(path.join(__dirname, '..', 'dist', 'lazyframe.min.js'))
1313
);
1414

15-
const dom = new JSDOM(``, {
16-
url: 'http://localhost:3000/',
17-
referrer: 'http://localhost:3000/',
18-
contentType: 'text/html',
19-
includeNodeLocations: true,
20-
resources: 'usable',
21-
runScripts: 'dangerously',
22-
virtualConsole
23-
});
15+
test.beforeEach(t => {
16+
const dom = new JSDOM(``, {
17+
includeNodeLocations: true,
18+
resources: 'usable',
19+
runScripts: 'dangerously',
20+
virtualConsole
21+
});
22+
23+
dom.runVMScript(script);
24+
global.document = dom.window.document;
25+
global.window = dom.window;
26+
})
2427

25-
dom.runVMScript(script);
28+
const createDomNode = (vendor, src, title, thumbnail) => {
29+
const node = document.createElement('div');
30+
node.classList.add('lazyframe');
31+
node.setAttribute('data-src', src)
32+
if (vendor) {
33+
node.setAttribute('data-vendor', vendor)
34+
}
35+
if (title) {
36+
node.setAttribute('data-title', title);
37+
}
38+
if (thumbnail) {
39+
node.setAttribute('data-thumbnail', thumbnail);
40+
}
41+
document.body.appendChild(node);
42+
return node;
43+
}
2644

2745
test('should expose lazyframe()', (t) => {
28-
t.true(typeof dom.window.lazyframe === 'function');
46+
t.true(typeof window.lazyframe === 'function');
2947
});
48+
49+
test('should initialize one node with a string selector', (t) => {
50+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0')
51+
window.lazyframe('.lazyframe');
52+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 1);
53+
})
54+
55+
test('should initialize mulitple nodes with a string selector', (t) => {
56+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
57+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0')
58+
window.lazyframe('.lazyframe');
59+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 2);
60+
})
61+
62+
test('should initialize with a single node', (t) => {
63+
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
64+
window.lazyframe(node);
65+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 1);
66+
})
67+
68+
test('should initialize with a nodelist', (t) => {
69+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
70+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0')
71+
const nodes = document.querySelectorAll('.lazyframe')
72+
window.lazyframe(nodes);
73+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 2);
74+
})
75+
76+
test('should append an iframe on click', (t) => {
77+
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
78+
window.lazyframe('.lazyframe');
79+
node.click();
80+
81+
t.assert(node.querySelector('iframe'))
82+
})
83+
84+
test('should call onAppend callback function', (t) => {
85+
let i = 0;
86+
const node1 = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
87+
const node2 = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
88+
window.lazyframe('.lazyframe', {
89+
onAppend() {
90+
i++;
91+
}
92+
});
93+
node1.click();
94+
node2.click();
95+
96+
t.is(i, 2)
97+
})
98+
99+
test('should use data-title', (t) => {
100+
const title = 'custom title'
101+
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0', title)
102+
103+
window.lazyframe('.lazyframe');
104+
105+
node.click()
106+
t.is(document.querySelector('.lazyframe__title').textContent, title)
107+
})

0 commit comments

Comments
 (0)