debian-mirror-gitlab/spec/frontend/helpers/startup_css_helper_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2 KiB
JavaScript
Raw Normal View History

2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-01-03 14:25:43 +05:30
import { waitForCSSLoaded } from '~/helpers/startup_css_helper';
2020-11-24 15:15:51 +05:30
describe('waitForCSSLoaded', () => {
let mockedCallback;
beforeEach(() => {
mockedCallback = jest.fn();
});
describe('Promise-like api', () => {
it('can be used with a callback', async () => {
await waitForCSSLoaded(mockedCallback);
expect(mockedCallback).toHaveBeenCalledTimes(1);
});
it('can be used as a promise', async () => {
await waitForCSSLoaded().then(mockedCallback);
expect(mockedCallback).toHaveBeenCalledTimes(1);
});
});
2021-01-29 00:20:46 +05:30
describe('when gon features is not provided', () => {
beforeEach(() => {
window.gon = null;
});
it('should invoke the action right away', async () => {
const events = waitForCSSLoaded(mockedCallback);
await events;
expect(mockedCallback).toHaveBeenCalledTimes(1);
});
});
2020-11-24 15:15:51 +05:30
describe('with startup css enabled', () => {
it('should dispatch CSSLoaded when the assets are cached or already loaded', async () => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2020-11-24 15:15:51 +05:30
<link href="one.css" data-startupcss="loaded">
<link href="two.css" data-startupcss="loaded">
`);
await waitForCSSLoaded(mockedCallback);
expect(mockedCallback).toHaveBeenCalledTimes(1);
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2020-11-24 15:15:51 +05:30
});
it('should wait to call CssLoaded until the assets are loaded', async () => {
2022-07-16 23:28:13 +05:30
setHTMLFixture(`
2020-11-24 15:15:51 +05:30
<link href="one.css" data-startupcss="loading">
<link href="two.css" data-startupcss="loading">
`);
const events = waitForCSSLoaded(mockedCallback);
2022-07-23 23:45:48 +05:30
document.querySelectorAll('[data-startupcss="loading"]').forEach((elem) => {
// eslint-disable-next-line no-param-reassign
elem.dataset.startupcss = 'loaded';
});
2020-11-24 15:15:51 +05:30
document.dispatchEvent(new CustomEvent('CSSStartupLinkLoaded'));
await events;
expect(mockedCallback).toHaveBeenCalledTimes(1);
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2020-11-24 15:15:51 +05:30
});
});
});