debian-mirror-gitlab/spec/frontend/header_search/init_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

75 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-08-13 15:12:31 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import initHeaderSearch, { eventHandler, cleanEventListeners } from '~/header_search/init';
describe('Header Search EventListener', () => {
beforeEach(() => {
jest.resetModules();
jest.restoreAllMocks();
setHTMLFixture(`
<div class="js-header-content">
<div class="header-search" id="js-header-search" data-autocomplete-path="/search/autocomplete" data-issues-path="/dashboard/issues" data-mr-path="/dashboard/merge_requests" data-search-context="{}" data-search-path="/search">
<input autocomplete="off" class="form-control gl-form-input gl-search-box-by-type-input" data-qa-selector="search_box" id="search" name="search" placeholder="Search GitLab" type="text">
</div>
</div>`);
});
afterEach(() => {
resetHTMLFixture();
jest.clearAllMocks();
});
it('attached event listener', () => {
const searchInputBox = document?.querySelector('#search');
const addEventListenerSpy = jest.spyOn(searchInputBox, 'addEventListener');
initHeaderSearch();
2022-10-11 01:57:18 +05:30
expect(addEventListenerSpy).toHaveBeenCalledTimes(2);
2022-08-13 15:12:31 +05:30
});
2022-10-11 01:57:18 +05:30
it('removes event listener', async () => {
2022-08-13 15:12:31 +05:30
const searchInputBox = document?.querySelector('#search');
const removeEventListenerSpy = jest.spyOn(searchInputBox, 'removeEventListener');
jest.mock('~/header_search', () => ({ initHeaderSearchApp: jest.fn() }));
await eventHandler.apply(
{
newHeaderSearchFeatureFlag: true,
searchInputBox: document.querySelector('#search'),
},
[cleanEventListeners],
);
2022-10-11 01:57:18 +05:30
expect(removeEventListenerSpy).toHaveBeenCalledTimes(2);
2022-08-13 15:12:31 +05:30
});
it('attaches new vue dropdown when feature flag is enabled', async () => {
const mockVueApp = jest.fn();
jest.mock('~/header_search', () => ({ initHeaderSearchApp: mockVueApp }));
await eventHandler.apply(
{
newHeaderSearchFeatureFlag: true,
searchInputBox: document.querySelector('#search'),
},
() => {},
);
2022-10-11 01:57:18 +05:30
expect(mockVueApp).toHaveBeenCalled();
2022-08-13 15:12:31 +05:30
});
it('attaches old vue dropdown when feature flag is disabled', async () => {
const mockLegacyApp = jest.fn(() => ({
onSearchInputFocus: jest.fn(),
}));
jest.mock('~/search_autocomplete', () => mockLegacyApp);
await eventHandler.apply(
{
newHeaderSearchFeatureFlag: false,
searchInputBox: document.querySelector('#search'),
},
() => {},
);
2022-10-11 01:57:18 +05:30
expect(mockLegacyApp).toHaveBeenCalled();
2022-08-13 15:12:31 +05:30
});
});