2020-10-06 14:08:49 -04:00
|
|
|
import { waitForCSSLoaded } from '~/helpers/startup_css_helper';
|
2020-09-08 20:08:42 -04:00
|
|
|
|
|
|
|
describe('waitForCSSLoaded', () => {
|
2020-09-17 17:09:39 -04:00
|
|
|
let mockedCallback;
|
|
|
|
|
2020-09-08 20:08:42 -04:00
|
|
|
beforeEach(() => {
|
2020-09-17 17:09:39 -04:00
|
|
|
mockedCallback = jest.fn();
|
2020-09-08 20:08:42 -04:00
|
|
|
});
|
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
describe('Promise-like api', () => {
|
|
|
|
it('can be used with a callback', async () => {
|
|
|
|
await waitForCSSLoaded(mockedCallback);
|
|
|
|
expect(mockedCallback).toHaveBeenCalledTimes(1);
|
2020-09-08 20:08:42 -04:00
|
|
|
});
|
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
it('can be used as a promise', async () => {
|
|
|
|
await waitForCSSLoaded().then(mockedCallback);
|
|
|
|
expect(mockedCallback).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
2020-09-08 20:08:42 -04:00
|
|
|
|
2020-11-13 04:09:30 -05:00
|
|
|
describe('when gon features is not provided', () => {
|
|
|
|
let originalGon;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
originalGon = window.gon;
|
|
|
|
window.gon = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
window.gon = originalGon;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should invoke the action right away', async () => {
|
|
|
|
const events = waitForCSSLoaded(mockedCallback);
|
|
|
|
await events;
|
|
|
|
|
|
|
|
expect(mockedCallback).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
describe('with startup css disabled', () => {
|
|
|
|
gon.features = {
|
|
|
|
startupCss: false,
|
|
|
|
};
|
2020-09-08 20:08:42 -04:00
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
it('should invoke the action right away', async () => {
|
|
|
|
const events = waitForCSSLoaded(mockedCallback);
|
2020-09-08 20:08:42 -04:00
|
|
|
await events;
|
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
expect(mockedCallback).toHaveBeenCalledTimes(1);
|
2020-09-08 20:08:42 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with startup css enabled', () => {
|
2020-09-17 17:09:39 -04:00
|
|
|
gon.features = {
|
|
|
|
startupCss: true,
|
|
|
|
};
|
2020-09-08 20:08:42 -04:00
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
it('should dispatch CSSLoaded when the assets are cached or already loaded', async () => {
|
|
|
|
setFixtures(`
|
2020-09-08 20:08:42 -04:00
|
|
|
<link href="one.css" data-startupcss="loaded">
|
|
|
|
<link href="two.css" data-startupcss="loaded">
|
2020-09-17 17:09:39 -04:00
|
|
|
`);
|
|
|
|
await waitForCSSLoaded(mockedCallback);
|
2020-09-08 20:08:42 -04:00
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
expect(mockedCallback).toHaveBeenCalledTimes(1);
|
2020-09-08 20:08:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should wait to call CssLoaded until the assets are loaded', async () => {
|
2020-09-17 17:09:39 -04:00
|
|
|
setFixtures(`
|
2020-09-08 20:08:42 -04:00
|
|
|
<link href="one.css" data-startupcss="loading">
|
|
|
|
<link href="two.css" data-startupcss="loading">
|
2020-09-17 17:09:39 -04:00
|
|
|
`);
|
|
|
|
const events = waitForCSSLoaded(mockedCallback);
|
2020-09-08 20:08:42 -04:00
|
|
|
document
|
|
|
|
.querySelectorAll('[data-startupcss="loading"]')
|
2020-12-23 16:10:24 -05:00
|
|
|
.forEach((elem) => elem.setAttribute('data-startupcss', 'loaded'));
|
2020-09-08 20:08:42 -04:00
|
|
|
document.dispatchEvent(new CustomEvent('CSSStartupLinkLoaded'));
|
|
|
|
await events;
|
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
expect(mockedCallback).toHaveBeenCalledTimes(1);
|
2020-09-08 20:08:42 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|