Skip to content

Instantly share code, notes, and snippets.

@AdamMcCormick
Created August 18, 2018 22:54
Show Gist options
  • Save AdamMcCormick/618bf8e9028c9940d7b41fccd06d9294 to your computer and use it in GitHub Desktop.
Save AdamMcCormick/618bf8e9028c9940d7b41fccd06d9294 to your computer and use it in GitHub Desktop.
Test Setup Example
/* eslint-env node, mocha, chai, chai-spies */
/* eslint max-nested-callbacks: ["error", 6] */
import chai from 'chai';
import spies from 'chai-spies';
import {
mapObjIndexed,
} from 'ramda';
import './modules';
import {buildSpy, clearDOM, resetAllSpies} from 'sbg-storyline-sdk/util/chaiTestUtil';
chai.use(spies);
// eslint-disable-next-line max-statements
describe('modules', () => {
const name = 'immaname',
version = 'immaversion',
current = 'immacurrent',
plugin = {
plugin: name,
version,
},
registryPlugin = {
...plugin,
current,
version: undefined,
};
let element, fetchMod, modules;
before(() => {
// Create spies for all imports
fetchMod = buildSpy([], {
fetchJson: () => Promise.resolve({
plugins: {
[name]: registryPlugin,
},
}),
});
require('sbg-util/dist/es/fetch');
require.cache[require.resolve('sbg-util/dist/es/fetch')] = {
exports: {
default: fetchMod,
fetch: fetchMod,
...fetchMod,
},
};
element = buildSpy([], {
create (tagName, {src, id, ...attrs}) {
const tag = document.createElement(tagName);
tag.src = src;
tag.id = id;
mapObjIndexed((val, key) => tag.setAttribute(key, val), attrs);
return tag;
},
remove: el => el.remove(),
});
require('sbg-util/dist/es/element');
require.cache[require.resolve('sbg-util/dist/es/element')] = {
exports: {
default: element,
element,
...element,
},
};
// Get a fresh instance and use es5 syntax to avoid Babel's hoisting
delete require.cache[require.resolve('./modules')];
modules = require('./modules');
});
after(() => {
clearDOM();
delete require.cache[require.resolve('./modules')];
delete require.cache[require.resolve('sbg-util/dist/es/fetch')];
delete require.cache[require.resolve('sbg-util/dist/es/element')];
});
beforeEach(clearDOM);
beforeEach(() => resetAllSpies(
fetchMod,
element
));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment