Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vb committed Oct 14, 2021
1 parent 42605e4 commit dc77c52
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ test.beforeEach(t => {
global.window = dom.window;
})

const createDomNode = (vendor, src, title, thumbnail) => {
const createDomNode = (params = {}) => {
const node = document.createElement('div');
node.classList.add('lazyframe');
node.setAttribute('data-src', src)
if (vendor) {
node.setAttribute('data-vendor', vendor)
}
if (title) {
node.setAttribute('data-title', title);
}
if (thumbnail) {
node.setAttribute('data-thumbnail', thumbnail);
for (const [ key, value ] of Object.entries(params)) {
node.setAttribute(`data-${key}`, value)
}
document.body.appendChild(node);
return node;
Expand All @@ -47,34 +40,38 @@ test('should expose lazyframe()', (t) => {
});

test('should initialize one node with a string selector', (t) => {
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0')
createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });
window.lazyframe('.lazyframe');
t.is(document.querySelectorAll('.lazyframe--loaded').length, 1);
})

test('should initialize mulitple nodes with a string selector', (t) => {
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0')
createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });
createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });

window.lazyframe('.lazyframe');
t.is(document.querySelectorAll('.lazyframe--loaded').length, 2);
})

test('should initialize with a single node', (t) => {
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
const node = createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });

window.lazyframe(node);
t.is(document.querySelectorAll('.lazyframe--loaded').length, 1);
})

test('should initialize with a nodelist', (t) => {
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0')
createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0' });
createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDC/?rel=0' });

const nodes = document.querySelectorAll('.lazyframe')
window.lazyframe(nodes);
t.is(document.querySelectorAll('.lazyframe--loaded').length, 2);
})

test('should append an iframe on click', (t) => {
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
const node = createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });

window.lazyframe('.lazyframe');
node.click();

Expand All @@ -83,8 +80,9 @@ test('should append an iframe on click', (t) => {

test('should call onAppend callback function', (t) => {
let i = 0;
const node1 = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
const node2 = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0')
const node1 = createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });
const node2 = createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0' });

window.lazyframe('.lazyframe', {
onAppend() {
i++;
Expand All @@ -98,10 +96,24 @@ test('should call onAppend callback function', (t) => {

test('should use data-title', (t) => {
const title = 'custom title'
const node = createDomNode('youtube', 'http://www.youtube.com/embed/iwGFalTRHDB/?rel=0', title)
const node = createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?rel=0', title });

window.lazyframe('.lazyframe');

node.click()
t.is(document.querySelector('.lazyframe__title').textContent, title)
})

test('should append optional query params from data-src', (t) => {
const query = 'rel=0&p=1'
const node = createDomNode({ vendor: 'youtube', src: 'http://www.youtube.com/embed/iwGFalTRHDA/?' + query });

window.lazyframe('.lazyframe');

node.click()
const iframe = node.querySelector('iframe');
const src = iframe.getAttribute('src');
const [,iframQuery] = src.split('?autoplay=1&')

t.is(iframQuery, query);
})

0 comments on commit dc77c52

Please sign in to comment.