You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Search-test.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. import { shallow, ShallowWrapper } from 'enzyme';
  21. import * as React from 'react';
  22. import { elementKeydown } from 'sonar-ui-common/helpers/testUtils';
  23. import { mockRouter } from '../../../../helpers/testMocks';
  24. import { ComponentQualifier } from '../../../../types/component';
  25. import { Search } from '../Search';
  26. it('selects results', () => {
  27. const form = shallowRender();
  28. form.setState({
  29. more: { TRK: 15, BRC: 0 },
  30. open: true,
  31. results: {
  32. TRK: [component('foo'), component('bar')],
  33. BRC: [component('qwe', ComponentQualifier.SubProject)]
  34. },
  35. selected: 'foo'
  36. });
  37. expect(form.state().selected).toBe('foo');
  38. next(form, 'bar');
  39. next(form, 'qualifier###TRK');
  40. next(form, 'qwe');
  41. next(form, 'qwe');
  42. prev(form, 'qualifier###TRK');
  43. prev(form, 'bar');
  44. select(form, 'foo');
  45. prev(form, 'foo');
  46. });
  47. it('opens selected project on enter', () => {
  48. const router = mockRouter();
  49. const form = shallowRender({ router });
  50. const selectedKey = 'project';
  51. form.setState({
  52. open: true,
  53. results: { [ComponentQualifier.Project]: [component(selectedKey)] },
  54. selected: selectedKey
  55. });
  56. elementKeydown(form.find('SearchBox'), 13);
  57. expect(router.push).toBeCalledWith({ pathname: '/dashboard', query: { id: selectedKey } });
  58. });
  59. it('opens selected portfolio on enter', () => {
  60. const router = mockRouter();
  61. const form = shallowRender({ router });
  62. const selectedKey = 'portfolio';
  63. form.setState({
  64. open: true,
  65. results: {
  66. [ComponentQualifier.Portfolio]: [component(selectedKey, ComponentQualifier.Portfolio)]
  67. },
  68. selected: selectedKey
  69. });
  70. elementKeydown(form.find('SearchBox'), 13);
  71. expect(router.push).toBeCalledWith({ pathname: '/portfolio', query: { id: selectedKey } });
  72. });
  73. it('opens selected subportfolio on enter', () => {
  74. const router = mockRouter();
  75. const form = shallowRender({ router });
  76. const selectedKey = 'sbprtfl';
  77. form.setState({
  78. open: true,
  79. results: {
  80. [ComponentQualifier.SubPortfolio]: [component(selectedKey, ComponentQualifier.SubPortfolio)]
  81. },
  82. selected: selectedKey
  83. });
  84. elementKeydown(form.find('SearchBox'), 13);
  85. expect(router.push).toBeCalledWith({ pathname: '/portfolio', query: { id: selectedKey } });
  86. });
  87. it('shows warning about short input', () => {
  88. const form = shallowRender();
  89. form.setState({ shortQuery: true });
  90. expect(form.find('.navbar-search-input-hint')).toMatchSnapshot();
  91. form.setState({ query: 'foobar x' });
  92. expect(form.find('.navbar-search-input-hint')).toMatchSnapshot();
  93. });
  94. function shallowRender(props: Partial<Search['props']> = {}) {
  95. return shallow<Search>(
  96. // @ts-ignore
  97. <Search
  98. appState={{ organizationsEnabled: false }}
  99. currentUser={{ isLoggedIn: false }}
  100. {...props}
  101. />
  102. );
  103. }
  104. function component(key: string, qualifier = ComponentQualifier.Project) {
  105. return { key, name: key, qualifier };
  106. }
  107. function next(form: ShallowWrapper<Search['props'], Search['state']>, expected: string) {
  108. elementKeydown(form.find('SearchBox'), 40);
  109. expect(form.state().selected).toBe(expected);
  110. }
  111. function prev(form: ShallowWrapper<Search['props'], Search['state']>, expected: string) {
  112. elementKeydown(form.find('SearchBox'), 38);
  113. expect(form.state().selected).toBe(expected);
  114. }
  115. function select(form: ShallowWrapper<Search['props'], Search['state']>, expected: string) {
  116. (form.instance() as Search).handleSelect(expected);
  117. expect(form.state().selected).toBe(expected);
  118. }