summaryrefslogtreecommitdiff
path: root/spec/frontend/behaviors/autosize_spec.js
blob: 7008b7b2eb6ab0d887fa92e8e284f2c8e5805e36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import '~/behaviors/autosize';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';

jest.mock('~/helpers/startup_css_helper', () => {
  return {
    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);
    }),
  };
});

describe('Autosize behavior', () => {
  beforeEach(() => {
    setHTMLFixture('<textarea class="js-autosize"></textarea>');
  });

  afterEach(() => {
    resetHTMLFixture();
  });

  it('is applied to the textarea', () => {
    // 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 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');
  });
});