all files / js/ Preload.class.test.js

100% Statements 58/58
50% Branches 1/2
100% Functions 15/15
100% Lines 58/58
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            12× 12× 12× 12× 12× 12×   12× 12× 12× 12× 12× 12× 12×         12× 12× 12×     12× 12× 12×                                                              
import Preload from './Preload.class';
 
describe('preload class', () => {
    let linkStyle;
    let linkFont;
    let head;
 
    beforeEach(function() {
        linkStyle = document.createElement('link');
        linkStyle.setAttribute('rel', 'preload');
        linkStyle.setAttribute('as', 'style');
        linkStyle.setAttribute('src', 'css/style.css');
        linkStyle.setAttribute('type', 'text/css');
        linkStyle.setAttribute('onload', 'this.rel=\'stylesheet\'');
 
        linkFont = document.createElement('link');
        linkFont.setAttribute('rel', 'preload');
        linkFont.setAttribute('as', 'font');
        linkFont.setAttribute('src', 'font/font-family.woff2');
        linkFont.setAttribute('type', 'font/woff2');
        linkFont.setAttribute('data-loaded', 'false');
        linkFont.setAttribute(
            'onload',
            'this.setAttribute(\'data-loaded\',\'true\')'
        );
 
        head = document.querySelector('head');
        head.appendChild(linkStyle);
        head.appendChild(linkFont);
    });
 
    afterEach(function() {
        head = document.querySelector('head');
        head.removeChild(linkFont);
        head.removeChild(linkStyle);
    });
 
    it('preload is Object', () => {
        const preload = new Preload();
        expect(typeof preload).toBe('object');
    });
 
    it('preload.update is function', () => {
        const preload = new Preload();
        expect(typeof preload.update).toBe('function');
    });
 
    it('preload.update() is boolean', () => {
        const preload = new Preload();
        expect(typeof preload.update()).toBe('boolean');
    });
 
    it('preload.update() is true or false', () => {
        const preload = new Preload();
        expect(preload.update()).toBe(true);
    });
 
    it('preload.check is function', () => {
        const preload = new Preload();
        expect(typeof preload.check).toBe('function');
    });
 
    it('preload.check() is boolean', () => {
        const preload = new Preload();
        expect(typeof preload.check()).toBe('boolean');
    });
 
    it('preload.check() is true or false', () => {
        const preload = new Preload();
        expect(
            preload.check() === false ||
            preload.check() === true)
        .toBe(true);
    });
 
    it('preload.check() is false', () => {
        const preload = new Preload();
        // use chrome-headless to check() true instead of phantom
        expect(preload.check() === false).toBe(true);
    });
 
    it('before preload.update() <link as="style"> rel="preload"', () => {
        expect(linkStyle.rel).toBe('preload');
    });
 
    it('after preload.update() <link as="style"> rel="stylesheet"', () => {
        const preload = new Preload();
        preload.update();
        expect(linkStyle.rel).toBe('stylesheet');
    });
 
    it('before preload.update() <link as="font"> data-loaded="false"', () => {
        expect(linkFont.dataset.loaded).toBe('false');
    });
 
    it('after preload.update() <link as="font"> data-loaded="true"', () => {
        const preload = new Preload();
        preload.update();
        expect(linkFont.dataset.loaded).toBe('true');
    });
});