Ghost/ghost/admin/tests/integration/services/lazy-loader-test.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

import Pretender from 'pretender';
2016-11-24 01:50:57 +03:00
import {describe, it} from 'mocha';
import {expect} from 'chai';
2016-11-24 01:50:57 +03:00
import {setupTest} from 'ember-mocha';
describe('Integration: Service: lazy-loader', function () {
setupTest();
2016-11-24 01:50:57 +03:00
let server;
let ghostPaths = {
adminRoot: '/assets/'
};
beforeEach(function () {
server = new Pretender();
});
afterEach(function () {
server.shutdown();
});
it('loads a script correctly and only once', async function () {
let subject = this.owner.lookup('service:lazy-loader');
subject.setProperties({
2016-11-24 01:50:57 +03:00
ghostPaths,
scriptPromises: {},
testing: false
});
// first load should add script element
await subject.loadScript('test', 'lazy-test.js')
.then(() => {})
.catch(() => {});
expect(
document.querySelectorAll('script[src="/assets/lazy-test.js"]').length,
'no of script tags on first load'
).to.equal(1);
// second load should not add another script element
await subject.loadScript('test', '/assets/lazy-test.js')
.then(() => { })
.catch(() => { });
expect(
document.querySelectorAll('script[src="/assets/lazy-test.js"]').length,
'no of script tags on second load'
).to.equal(1);
2016-11-24 01:50:57 +03:00
});
2016-11-24 01:50:57 +03:00
it('loads styles correctly', function () {
let subject = this.owner.lookup('service:lazy-loader');
subject.setProperties({
2016-11-24 01:50:57 +03:00
ghostPaths,
testing: false
});
return subject.loadStyle('testing', 'style.css').catch(() => {
// we add a catch handler here because `/assets/style.css` doesn't exist
expect(document.querySelectorAll('#testing-styles').length).to.equal(1);
expect(document.querySelector('#testing-styles').getAttribute('href')).to.equal('/assets/style.css');
});
2016-11-24 01:50:57 +03:00
});
});