All files / tests helpers.js

89.74% Statements 35/39
50% Branches 5/10
100% Functions 13/13
89.19% Lines 33/37
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          36x 18x 47x           18x 34x           18x 18x   18x   85x     85x 85x     85x     85x         18x 18x         54x 54x         23x 23x         6x 6x       18x   12x   12x           34x 34x   34x 34x   34x 94x           34x             1x   1x            
import { readFileSync } from 'fs';
import { resolve } from 'path';
import Vue from 'vue/dist/vue.common';
import BootstrapVue from '../lib';
 
const readFile = (path) => String(readFileSync(resolve(__dirname, '../examples', path)));
const throwIfNotVueInstance = vm => {
    Iif (!vm instanceof Vue) {
        // debugging breadcrumbs in case a non-Vue instance gets erroneously passed
        // makes the error easier to fix than example: "Cannot read _prevClass of undefined"
        throw new TypeError(`The matcher function expects Vue instance. Given ${typeof vm}`)
    }
}
const throwIfNotArray = array => {
    Iif (!Array.isArray(array)) {
        throw new TypeError(`The matcher requires an array. Given ${typeof array}`)
    }
}
 
export function loadFixture(name) {
    const template = readFile(`${name}/demo.html`);
    const js = readFile(`${name}/demo.js`);
 
    return async() => {
        // Mount template
        document.body.innerHTML = template;
 
        // Install Vue and BootstrapVue
        window.Vue = Vue;
        Vue.use(BootstrapVue);
 
        // Eval js
        eval(js);
 
        // Await for Vue render
        await Vue.nextTick();
    };
}
 
export async function testVM() {
    it(`vm mounts`, async() => {
        return expect(window.app.$el).toBeDefined();
    });
}
 
export function nextTick() {
    return new Promise((resolve, reject) => {
        Vue.nextTick(resolve)
    });
}
 
export async function setData(app, key, value) {
    app[key] = value;
    await nextTick();
}
 
// Usage: await sleep(1000);
export function sleep(ms) {
    ms = ms || 0;
    return new Promise(r => setTimeout(r, ms));
}
 
// Extend Jest marchers
expect.extend({
    toHaveClass(vm, className) {
        throwIfNotVueInstance(vm)
 
        return {
            message: `expected <${vm.$options._componentTag}> to have class '${className}'`,
            pass: vm.$el._prevClass.indexOf(className) !== -1,
        };
    },
    toHaveAllClasses(vm, classList) {
        throwIfNotVueInstance(vm)
        throwIfNotArray(classList)
 
        let pass = true;
        let missingClassNames = []
 
        classList.forEach(className => {
            Iif (!vm.$el._prevClass.includes(className)) {
                pass = false
                missingClassNames.push(className)
            }
        })
 
        return {
            // more debugging breadcrumbs
            message: `Expected <${vm.$options._componentTag}> to have all classes in [ ${classList.join(', ')} ], but was missing [ ${missingClassNames.join(', ')} ] class${missingClassNames.length > 1 ? 'es' : ''}.`,
            pass
        }
    },
    toBeComponent(vm, componentTag) {
        throwIfNotVueInstance(vm)
 
        return {
            message: `expected to be <${componentTag}>`,
            pass: vm.$options._componentTag === componentTag
        };
    },
});