2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
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', () => {
|
|
|
|
describe('with input invisible on component render', () => {
|
|
|
|
let el;
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
beforeEach(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture('<div id="container" style="display: none;"><input id="inputel"/></div>');
|
2019-12-04 20:38:33 +05:30
|
|
|
el = document.querySelector('#inputel');
|
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|