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.

background-tasks-test.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 React from 'react';
  21. import { shallow } from 'enzyme';
  22. import Stats from '../components/Stats';
  23. import Search from '../components/Search';
  24. import { STATUSES, CURRENTS, DEBOUNCE_DELAY, DEFAULT_FILTERS } from '../constants';
  25. import { formatDuration } from '../utils';
  26. import { change, click } from '../../../helpers/testUtils';
  27. const stub = jest.fn();
  28. describe('Constants', () => {
  29. it('should have STATUSES', () => {
  30. expect(Object.keys(STATUSES).length).toBe(7);
  31. });
  32. it('should have CURRENTS', () => {
  33. expect(Object.keys(CURRENTS).length).toBe(2);
  34. });
  35. });
  36. describe('Search', () => {
  37. const defaultProps = {
  38. ...DEFAULT_FILTERS,
  39. loading: false,
  40. types: [],
  41. onFilterUpdate: () => true,
  42. onReload: () => true
  43. };
  44. it('should render search form', () => {
  45. const component = shallow(<Search {...defaultProps} />);
  46. expect(component.find('SearchBox').exists()).toBeTruthy();
  47. });
  48. it('should not render search form', () => {
  49. const component = shallow(<Search {...defaultProps} component={{ id: 'ABCD' }} />);
  50. expect(component.find('SearchBox').exists()).toBeFalsy();
  51. });
  52. it('should search', done => {
  53. const searchSpy = jest.fn();
  54. const component = shallow(<Search {...defaultProps} onFilterUpdate={searchSpy} />);
  55. const searchInput = component.find('SearchBox');
  56. searchInput.prop('onChange')('some search query');
  57. setTimeout(() => {
  58. expect(searchSpy).toBeCalledWith({ query: 'some search query' });
  59. done();
  60. }, DEBOUNCE_DELAY);
  61. });
  62. it('should reload', () => {
  63. const reloadSpy = jest.fn();
  64. const component = shallow(<Search {...defaultProps} onReload={reloadSpy} />);
  65. const reloadButton = component.find('.js-reload');
  66. expect(reloadSpy).not.toBeCalled();
  67. click(reloadButton);
  68. expect(reloadSpy).toBeCalled();
  69. });
  70. });
  71. describe('Stats', () => {
  72. describe('Pending', () => {
  73. it('should show zero pending', () => {
  74. const result = shallow(
  75. <Stats pendingCount={0} onCancelAllPending={stub} onShowFailing={stub} />
  76. );
  77. expect(result.find('.js-pending-count').text()).toContain('0');
  78. });
  79. it('should show 5 pending', () => {
  80. const result = shallow(
  81. <Stats pendingCount={5} onCancelAllPending={stub} onShowFailing={stub} />
  82. );
  83. expect(result.find('.js-pending-count').text()).toContain('5');
  84. });
  85. it('should not show cancel pending button', () => {
  86. const result = shallow(
  87. <Stats pendingCount={0} onCancelAllPending={stub} onShowFailing={stub} />
  88. );
  89. expect(result.find('.js-cancel-pending').length).toBe(0);
  90. });
  91. it('should show cancel pending button', () => {
  92. const result = shallow(
  93. <Stats
  94. isSystemAdmin={true}
  95. pendingCount={5}
  96. onCancelAllPending={stub}
  97. onShowFailing={stub}
  98. />
  99. );
  100. expect(result.find('.js-cancel-pending').length).toBe(1);
  101. });
  102. it('should trigger cancelling pending', () => {
  103. const spy = jest.fn();
  104. const result = shallow(
  105. <Stats
  106. isSystemAdmin={true}
  107. pendingCount={5}
  108. onCancelAllPending={spy}
  109. onShowFailing={stub}
  110. />
  111. );
  112. expect(spy).not.toBeCalled();
  113. click(result.find('.js-cancel-pending'));
  114. expect(spy).toBeCalled();
  115. });
  116. });
  117. describe('Failures', () => {
  118. it('should show zero failures', () => {
  119. const result = shallow(
  120. <Stats failingCount={0} onCancelAllPending={stub} onShowFailing={stub} />
  121. );
  122. expect(result.find('.js-failures-count').text()).toContain('0');
  123. });
  124. it('should show 5 failures', () => {
  125. const result = shallow(
  126. <Stats failingCount={5} onCancelAllPending={stub} onShowFailing={stub} />
  127. );
  128. expect(result.find('.js-failures-count').text()).toContain('5');
  129. });
  130. it('should not show link to failures', () => {
  131. const result = shallow(
  132. <Stats failingCount={0} onCancelAllPending={stub} onShowFailing={stub} />
  133. );
  134. expect(result.find('.js-failures-count').is('a')).toBeFalsy();
  135. });
  136. it('should show link to failures', () => {
  137. const result = shallow(
  138. <Stats failingCount={5} onCancelAllPending={stub} onShowFailing={stub} />
  139. );
  140. expect(result.find('.js-failures-count').is('a')).toBeTruthy();
  141. });
  142. it('should trigger filtering failures', () => {
  143. const spy = jest.fn();
  144. const result = shallow(
  145. <Stats failingCount={5} onCancelAllPending={stub} onShowFailing={spy} />
  146. );
  147. expect(spy).not.toBeCalled();
  148. click(result.find('.js-failures-count'));
  149. expect(spy).toBeCalled();
  150. });
  151. });
  152. });
  153. describe('Helpers', () => {
  154. describe('#formatDuration()', () => {
  155. it('should format 173ms', () => {
  156. expect(formatDuration(173)).toBe('173ms');
  157. });
  158. it('should format 999ms', () => {
  159. expect(formatDuration(999)).toBe('999ms');
  160. });
  161. it('should format 1s 0ms', () => {
  162. expect(formatDuration(1000)).toBe('1.0s');
  163. });
  164. it('should format 1s 1ms', () => {
  165. expect(formatDuration(1001)).toBe('1.1s');
  166. });
  167. it('should format 1s 501ms', () => {
  168. expect(formatDuration(1501)).toBe('1.501s');
  169. });
  170. it('should format 59s', () => {
  171. expect(formatDuration(59000)).toBe('59s');
  172. });
  173. it('should format 1min 0s', () => {
  174. expect(formatDuration(60000)).toBe('1min 0s');
  175. });
  176. it('should format 1min 2s', () => {
  177. expect(formatDuration(62757)).toBe('1min 2s');
  178. });
  179. it('should format 3min 44s', () => {
  180. expect(formatDuration(224567)).toBe('3min 44s');
  181. });
  182. it('should format 1h 20m', () => {
  183. expect(formatDuration(80 * 60 * 1000)).toBe('1h 20min');
  184. });
  185. });
  186. });