debian-mirror-gitlab/spec/frontend/vue_shared/directives/autofocusonshow_spec.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import { useMockIntersectionObserver } from 'helpers/mock_dom_observer';
2019-12-04 20:38:33 +05:30
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', () => {
2020-10-24 23:57:45 +05:30
useMockIntersectionObserver();
2019-12-04 20:38:33 +05:30
describe('with input invisible on component render', () => {
let el;
2020-06-23 00:09:42 +05:30
beforeEach(() => {
2019-12-04 20:38:33 +05:30
setFixtures('<div id="container" style="display: none;"><input id="inputel"/></div>');
el = document.querySelector('#inputel');
});
it('should bind IntersectionObserver on input element', () => {
2020-06-23 00:09:42 +05:30
jest.spyOn(el, 'focus').mockImplementation(() => {});
2019-12-04 20:38:33 +05:30
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: () => {},
};
2020-06-23 00:09:42 +05:30
jest.spyOn(el.visibilityObserver, 'disconnect').mockImplementation(() => {});
2019-12-04 20:38:33 +05:30
autofocusonshow.unbind(el);
expect(el.visibilityObserver).toBeDefined();
expect(el.visibilityObserver.disconnect).toHaveBeenCalled();
});
});
});