docbrown/app/javascript/utilities/__tests__/codeFullscreenModeSwitcher.test.js
Mac Siri afa73e06e3
Replace Webpacker with Esbuild (#20470)
* Migrate to esbuild WIP

* Add exclude

* Remove redundant file

* Move file

* Move to javascript_include_tag

* Lint fix

* WIP

* WIP

* Add watch mode to esbuild WIP

* Get jest working

* Remove babel

* Revert "Remove babel"

This reverts commit 6da35260aa19d6f97f586deb66c0ecaf48433b73.

* More WIP

* Got image to load

* WIP

* Resolve audit

* Lint fix

* WIP

* Fix jest spec

* [CI] Remove asset-restore for test build stage

* Production compliant

* Temp disable sourcemap

* Update glob

* Add esbuild helper to stimulus

* Import fragment

* Temp disable coverage to see failing tests

* Fix broken spec

* Address lint

* Set proper es6 target

* Use esbuild for everything

* wait what

* Revert "Set proper es6 target"

This reverts commit 98f5278093421baa8ffe2ca580845b01c1a1eadf.

* Revert "Use esbuild for everything"

This reverts commit 0ac46738f07ffcb6af095ccb1ffa5e439b7fefa3.

* Replace uglifier with terser

* New compiled assets version

* Remvoe honeybadger-io/webpack

* Remove cypress coverage checks for now

* Update jsconfig.json

* Update docker-compose

* Remove public/packs-test from ci cache
2024-01-25 20:29:21 +00:00

100 lines
3.3 KiB
JavaScript

import {
addFullScreenModeControl,
getFullScreenModeStatus,
onPressEscape,
onPopstate,
} from '@utilities/codeFullscreenModeSwitcher';
describe('CodeFullScreenModeSwitcher Utility', () => {
const getFullScreenElements = () =>
['.js-fullscreen-code.is-open', '.js-code-highlight.is-fullscreen'].map(
(selector) => document.body.querySelector(selector),
);
const testFullScreenElements = ({ exists }) => {
for (const element of getFullScreenElements()) {
if (exists) expect(element).not.toBeNull();
else expect(element).toBeNull();
}
};
const testNonFullScreen = () => {
expect(getFullScreenModeStatus()).toBe(false);
testFullScreenElements({ exists: false });
expect(document.body.style.overflow).toBe('');
};
const testFullScreen = () => {
expect(getFullScreenModeStatus()).toBe(true);
testFullScreenElements({ exists: true });
expect(document.body.style.overflow).toBe('hidden');
};
const getEnterFullScreenButtons = () =>
document.getElementsByClassName('js-fullscreen-code-action');
beforeAll(() => {
global.scrollTo = jest.fn();
document.body.innerHTML = `
<div class="js-code-highlight">
<div class="js-fullscreen-code-action"><div>
</div>
<div class="js-fullscreen-code"></div>
`;
addFullScreenModeControl(getEnterFullScreenButtons());
});
// Assertions are called within the `testNonFullScreen` function.
// eslint-disable-next-line jest/expect-expect
it('starts in non-fullscreen mode', testNonFullScreen);
it('enters fullscreen mode on click', () => {
const goFullScreenButtons = getEnterFullScreenButtons();
const spyDoc = jest.spyOn(document.body, 'addEventListener');
const spyWindow = jest.spyOn(window, 'addEventListener');
goFullScreenButtons[0].click();
testFullScreen();
expect(spyDoc).toHaveBeenCalledWith('keyup', onPressEscape);
expect(spyWindow).toHaveBeenCalledWith('popstate', onPopstate);
});
it('exits fullscreen mode on Escape key', () => {
const spyDocRemove = jest.spyOn(document.body, 'removeEventListener');
const spyWindowRemove = jest.spyOn(window, 'removeEventListener');
testFullScreen();
document.body.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape' }));
testNonFullScreen();
expect(spyDocRemove).toHaveBeenCalledWith('keyup', onPressEscape);
expect(spyWindowRemove).toHaveBeenCalledWith('popstate', onPopstate);
});
it('re-enters fullscreen mode on click', () => {
const goFullScreenButtons = getEnterFullScreenButtons();
const spyDoc = jest.spyOn(document.body, 'addEventListener');
const spyWindow = jest.spyOn(window, 'addEventListener');
testNonFullScreen();
goFullScreenButtons[0].click();
testFullScreen();
expect(spyDoc).toHaveBeenCalledWith('keyup', onPressEscape);
expect(spyWindow).toHaveBeenCalledWith('popstate', onPopstate);
});
it('exits fullscreen mode on popstate event', () => {
const spyDocRemove = jest.spyOn(document.body, 'removeEventListener');
const spyWindowRemove = jest.spyOn(window, 'removeEventListener');
testFullScreen();
window.dispatchEvent(new PopStateEvent('popstate', { state: { key: '' } }));
testNonFullScreen();
expect(spyDocRemove).toHaveBeenCalledWith('keyup', onPressEscape);
expect(spyWindowRemove).toHaveBeenCalledWith('popstate', onPopstate);
});
});