UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

115 lines (80 loc) 3.12 kB
'use strict'; import * as chai from 'chai'; import {Script} from "../../../../source/dom/resource/script.mjs"; import {DataUrl} from "../../../../source/types/dataurl.mjs"; import {ID} from "../../../../source/types/id.mjs"; import {chaiDom} from "../../../util/chai-dom.mjs"; import {cleanupDOMFromTesting, initMutationObserverForTesting} from "../../../util/cleanupdom.mjs"; import {initJSDOM} from "../../../util/jsdom.mjs"; let expect = chai.expect; chai.use(chaiDom); let html1 = ` `; describe('Script', function () { before(function (done) { initJSDOM().then(() => { done() }); }); beforeEach(() => { initMutationObserverForTesting() }) afterEach(() => { cleanupDOMFromTesting(); }) describe('Script()', function () { it('setEventTypes()', function (done) { const script = new Script({ src: new DataUrl('', 'text/javascript').toString(), async: true, crossOrigin: 'anonymous', defer: false, integrity: undefined, nomodule: false, nonce: undefined, referrerpolicy: undefined, type: 'text/javascript' }); script.connect().available().then(() => { const id = script.getOption('id') done() }).catch(e => done(e)); }) }); describe('External JS', () => { let id = new ID('script').toString(); let server, script, url = 'https://cdnjs.cloudflare.com/ajax/libs/layzr.js/2.2.2/layzr.min.js'; beforeEach(() => { script = new Script({ src: url, async: true, crossOrigin: 'anonymous', defer: false, integrity: undefined, nomodule: false, nonce: undefined, referrerpolicy: undefined, type: 'text/javascript', id: id }); }); it('append and remove script ', (done) => { expect(script.isConnected()).to.be.false; script.connect().available().then(() => { expect(script.isConnected()).to.be.true; expect(document.querySelector('[src="' + url + '"]')).to.exist; document.getElementById(id).remove(); expect(script.isConnected()).to.be.false; expect(document.querySelector('[src="' + url + '"]')).not.to.exist; script.connect().available().then(() => { expect(script.isConnected()).to.be.true; expect(document.querySelector('[src="' + url + '"]')).to.exist; document.getElementById(id).remove(); expect(document.querySelector('[src="' + url + '"]')).not.to.exist; expect(script.isConnected()).to.be.false; done() }).catch(e => done(e)); }).catch(e => done(e)); }); }); });