Skip to content

Commit d4d4435

Browse files
committed
Added some tests
1 parent fb8fc7a commit d4d4435

3 files changed

Lines changed: 76 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: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,80 @@ 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) => {
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+
document.body.appendChild(node);
36+
return node;
37+
}
2638

2739
test('should expose lazyframe()', (t) => {
28-
t.true(typeof dom.window.lazyframe === 'function');
40+
t.true(typeof window.lazyframe === 'function');
2941
});
42+
43+
test('should initialize one node with a string selector', (t) => {
44+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0')
45+
window.lazyframe('.lazyframe');
46+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 1);
47+
})
48+
49+
test('should initialize mulitple nodes with a string selector', (t) => {
50+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
51+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0')
52+
window.lazyframe('.lazyframe');
53+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 2);
54+
})
55+
56+
test('should initialize with a single node', (t) => {
57+
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
58+
window.lazyframe(node);
59+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 1);
60+
})
61+
62+
test('should initialize with a nodelist', (t) => {
63+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
64+
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0')
65+
const nodes = document.querySelectorAll('.lazyframe')
66+
window.lazyframe(nodes);
67+
t.is(document.querySelectorAll('.lazyframe--loaded').length, 2);
68+
})
69+
70+
test('should append an iframe on click', (t) => {
71+
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
72+
window.lazyframe('.lazyframe');
73+
node.click();
74+
75+
t.assert(node.querySelector('iframe'))
76+
})
77+
78+
test('should call onAppend callback function', (t) => {
79+
let i = 0;
80+
const node1 = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
81+
const node2 = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
82+
window.lazyframe('.lazyframe', {
83+
onAppend() {
84+
i++;
85+
}
86+
});
87+
node1.click();
88+
node2.click();
89+
90+
t.is(i, 2)
91+
})

0 commit comments

Comments
 (0)