Welcome to mirror list, hosted at ThFree Co, Russian Federation.

autofocusonshow_spec.js « directives « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d052c99ec0ec250f554524dea2e68dd98ab39511 (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
43
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import autofocusonshow from '~/vue_shared/directives/autofocusonshow';

/**
 * We're testing this directive's hooks as pure functions
 * since behaviour of this directive is highly-dependent
 * on underlying DOM methods.
 */
describe('AutofocusOnShow directive', () => {
  describe('with input invisible on component render', () => {
    let el;

    beforeEach(() => {
      setHTMLFixture('<div id="container" style="display: none;"><input id="inputel"/></div>');
      el = document.querySelector('#inputel');
    });

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

    it('should bind IntersectionObserver on input element', () => {
      jest.spyOn(el, 'focus').mockImplementation(() => {});

      autofocusonshow.inserted(el);

      expect(el.visibilityObserver).toBeDefined();
      expect(el.focus).not.toHaveBeenCalled();
    });

    it('should stop IntersectionObserver on input element on unbind hook', () => {
      el.visibilityObserver = {
        disconnect: () => {},
      };
      jest.spyOn(el.visibilityObserver, 'disconnect').mockImplementation(() => {});

      autofocusonshow.unbind(el);

      expect(el.visibilityObserver).toBeDefined();
      expect(el.visibilityObserver.disconnect).toHaveBeenCalled();
    });
  });
});