import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import initHeaderSearch, { eventHandler, cleanEventListeners } from '~/header_search/init';
describe('Header Search EventListener', () => {
beforeEach(() => {
jest.resetModules();
jest.restoreAllMocks();
setHTMLFixture(`
`);
});
afterEach(() => {
resetHTMLFixture();
jest.clearAllMocks();
});
it('attached event listener', () => {
const searchInputBox = document?.querySelector('#search');
const addEventListenerSpy = jest.spyOn(searchInputBox, 'addEventListener');
initHeaderSearch();
expect(addEventListenerSpy).toHaveBeenCalledTimes(2);
});
it('removes event listener', async () => {
const searchInputBox = document?.querySelector('#search');
const removeEventListenerSpy = jest.spyOn(searchInputBox, 'removeEventListener');
jest.mock('~/header_search', () => ({ initHeaderSearchApp: jest.fn() }));
await eventHandler.apply(
{
searchInputBox: document.querySelector('#search'),
},
[cleanEventListeners],
);
expect(removeEventListenerSpy).toHaveBeenCalledTimes(2);
});
it('attaches new vue dropdown when feature flag is enabled', async () => {
const mockVueApp = jest.fn();
jest.mock('~/header_search', () => ({ initHeaderSearchApp: mockVueApp }));
await eventHandler.apply(
{
searchInputBox: document.querySelector('#search'),
},
() => {},
);
expect(mockVueApp).toHaveBeenCalled();
});
});