debian-mirror-gitlab/spec/frontend/behaviors/autosize_spec.js

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

43 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import '~/behaviors/autosize';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2016-09-13 17:45:13 +05:30
2020-11-24 15:15:51 +05:30
jest.mock('~/helpers/startup_css_helper', () => {
return {
2021-03-11 19:13:27 +05:30
waitForCSSLoaded: jest.fn().mockImplementation((cb) => {
// This is a hack:
// autosize.js will execute and modify the DOM
// whenever waitForCSSLoaded calls its callback function.
// This setTimeout is here because everything within setTimeout will be queued
// as async code until the current call stack is executed.
// If we would not do this, the mock for waitForCSSLoaded would call its callback
// before the fixture in the beforeEach is set and the Test would fail.
// more on this here: https://johnresig.com/blog/how-javascript-timers-work/
setTimeout(() => {
cb.apply();
}, 0);
}),
2020-11-24 15:15:51 +05:30
};
});
2018-03-17 18:26:18 +05:30
describe('Autosize behavior', () => {
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture('<textarea class="js-autosize"></textarea>');
});
afterEach(() => {
resetHTMLFixture();
2018-03-17 18:26:18 +05:30
});
2020-11-24 15:15:51 +05:30
it('is applied to the textarea', () => {
2021-03-11 19:13:27 +05:30
// This is the second part of the Hack:
// Because we are forcing the mock for WaitForCSSLoaded and the very end of our callstack
// to call its callback. This querySelector needs to go to the very end of our callstack
2022-07-16 23:28:13 +05:30
// as well, if we would not have this jest.runOnlyPendingTimers here, the querySelector
// would not run and the test would fail.
jest.runOnlyPendingTimers();
const textarea = document.querySelector('textarea');
expect(textarea.classList).toContain('js-autosize-initialized');
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});