2017-05-16 17:01:51 -04:00
|
|
|
import '~/behaviors/autosize';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
jest.mock('~/helpers/startup_css_helper', () => {
|
|
|
|
return {
|
2021-01-25 16:09:03 -05:00
|
|
|
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-09-17 17:09:39 -04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-11-27 18:13:11 -05:00
|
|
|
describe('Autosize behavior', () => {
|
|
|
|
beforeEach(() => {
|
2020-09-17 17:09:39 -04:00
|
|
|
setFixtures('<textarea class="js-autosize"></textarea>');
|
2017-11-27 18:13:11 -05:00
|
|
|
});
|
|
|
|
|
2020-09-17 17:09:39 -04:00
|
|
|
it('is applied to the textarea', () => {
|
2021-01-25 16:09:03 -05:00
|
|
|
// 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
|
|
|
|
// as well, if we would not have this setTimeout Function here, the querySelector
|
|
|
|
// would run before the mockImplementation called its callBack Function
|
|
|
|
// the DOM Manipulation didn't happen yet and the test would fail.
|
|
|
|
setTimeout(() => {
|
|
|
|
const textarea = document.querySelector('textarea');
|
|
|
|
expect(textarea.classList).toContain('js-autosize-initialized');
|
|
|
|
}, 0);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2017-11-27 18:13:11 -05:00
|
|
|
});
|