3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { KeyCodes } from 'sonar-ui-common/helpers/keycodes';
23 import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
24 import { searchUsers } from '../../../../../api/users';
25 import { mockLoggedInUser, mockUser } from '../../../../../helpers/testMocks';
26 import AssigneeSelection from '../AssigneeSelection';
28 jest.mock('../../../../../api/users', () => ({
29 searchUsers: jest.fn().mockResolvedValue([])
32 it('should render correctly', () => {
33 expect(shallowRender()).toMatchSnapshot();
36 it('should handle keydown', () => {
37 const mockEvent = (keyCode: number) => ({ preventDefault: jest.fn(), keyCode });
38 const suggestedUsers = [
39 mockUser({ login: '1' }) as T.UserActive,
40 mockUser({ login: '2' }) as T.UserActive,
41 mockUser({ login: '3' }) as T.UserActive
44 const onSelect = jest.fn();
45 const wrapper = shallowRender({ onSelect });
47 wrapper.instance().handleKeyDown(mockEvent(KeyCodes.UpArrow) as any);
48 expect(wrapper.state().highlighted).toBeUndefined();
50 wrapper.setState({ suggestedUsers });
52 // press down to highlight the first
53 wrapper.instance().handleKeyDown(mockEvent(KeyCodes.DownArrow) as any);
54 expect(wrapper.state().highlighted).toBe(suggestedUsers[0]);
56 // press up to loop around to last
57 wrapper.instance().handleKeyDown(mockEvent(KeyCodes.UpArrow) as any);
58 expect(wrapper.state().highlighted).toBe(suggestedUsers[2]);
60 // press down to loop around to first
61 wrapper.instance().handleKeyDown(mockEvent(KeyCodes.DownArrow) as any);
62 expect(wrapper.state().highlighted).toBe(suggestedUsers[0]);
64 // press down highlight the next
65 wrapper.instance().handleKeyDown(mockEvent(KeyCodes.DownArrow) as any);
66 expect(wrapper.state().highlighted).toBe(suggestedUsers[1]);
68 // press enter to select the highlighted user
69 wrapper.instance().handleKeyDown(mockEvent(KeyCodes.Enter) as any);
70 expect(onSelect).toBeCalledWith(suggestedUsers[1]);
73 it('should handle search', async () => {
74 const users = [mockUser({ login: '1' }), mockUser({ login: '2' }), mockUser({ login: '3' })];
75 (searchUsers as jest.Mock).mockResolvedValueOnce({ users });
77 const onSelect = jest.fn();
79 const wrapper = shallowRender({ onSelect });
80 expect(wrapper.state().suggestedUsers.length).toBe(0);
81 wrapper.instance().handleSearch('j');
83 expect(searchUsers).not.toBeCalled();
84 expect(wrapper.state().open).toBe(false);
86 wrapper.instance().handleSearch('jo');
87 expect(wrapper.state().loading).toBe(true);
88 expect(searchUsers).toBeCalledWith({ q: 'jo' });
90 await waitAndUpdate(wrapper);
92 expect(wrapper.state().loading).toBe(false);
93 expect(wrapper.state().open).toBe(true);
94 expect(wrapper.state().suggestedUsers).toHaveLength(3);
98 wrapper.instance().handleSearch('');
99 expect(searchUsers).not.toBeCalled();
100 expect(wrapper.state().suggestedUsers.length).toBe(0);
103 it('should allow current user selection', async () => {
104 const loggedInUser = mockLoggedInUser();
105 const users = [mockUser({ login: '1' }), mockUser({ login: '2' }), mockUser({ login: '3' })];
106 (searchUsers as jest.Mock).mockResolvedValueOnce({ users });
108 const wrapper = shallowRender({ allowCurrentUserSelection: true, loggedInUser });
109 expect(wrapper.state().suggestedUsers[0]).toBe(loggedInUser);
111 wrapper.instance().handleSearch('jo');
112 await waitAndUpdate(wrapper);
113 expect(wrapper.state().suggestedUsers).toHaveLength(3);
115 wrapper.instance().handleSearch('');
116 expect(wrapper.state().suggestedUsers[0]).toBe(loggedInUser);
119 function shallowRender(props?: Partial<AssigneeSelection['props']>) {
120 return shallow<AssigneeSelection>(
122 allowCurrentUserSelection={false}
123 loggedInUser={mockLoggedInUser()}