blob: eb3b6900a2533677f1ca96233ed6378402b32a15 (
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
44
45
46
47
48
49
50
|
import htmlRedirectListbox from 'test_fixtures/listbox/redirect_listbox.html';
import { initListbox } from '~/listbox';
import { initRedirectListboxBehavior } from '~/listbox/redirect_behavior';
import { redirectTo } from '~/lib/utils/url_utility'; // eslint-disable-line import/no-deprecated
import { setHTMLFixture } from 'helpers/fixtures';
jest.mock('~/lib/utils/url_utility');
jest.mock('~/listbox', () => ({
initListbox: jest.fn().mockReturnValue({ foo: true }),
}));
describe('initRedirectListboxBehavior', () => {
let instances;
beforeEach(() => {
setHTMLFixture(`
${htmlRedirectListbox}
${htmlRedirectListbox}
`);
instances = initRedirectListboxBehavior();
});
it('calls initListbox for each .js-redirect-listbox', () => {
expect(instances).toEqual([{ foo: true }, { foo: true }]);
expect(initListbox).toHaveBeenCalledTimes(2);
initListbox.mock.calls.forEach((callArgs, i) => {
const elements = document.querySelectorAll('.js-redirect-listbox');
expect(callArgs[0]).toBe(elements[i]);
expect(callArgs[1]).toEqual({
onChange: expect.any(Function),
});
});
});
it('passes onChange handler to initListbox that calls redirectTo', () => {
const [firstCallArgs] = initListbox.mock.calls;
const { onChange } = firstCallArgs[1];
const mockItem = { href: '/foo' };
expect(redirectTo).not.toHaveBeenCalled(); // eslint-disable-line import/no-deprecated
onChange(mockItem);
expect(redirectTo).toHaveBeenCalledWith(mockItem.href); // eslint-disable-line import/no-deprecated
});
});
|