index.spec.jsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import React from 'react';
  2. import {shallow, mount} from 'enzyme';
  3. import {Dashboard} from 'app/views/projectsDashboard';
  4. import ProjectsStatsStore from 'app/stores/projectsStatsStore';
  5. import * as projectsActions from 'app/actionCreators/projects';
  6. jest.unmock('lodash/debounce');
  7. jest.mock('lodash/debounce', () => {
  8. const debounceMap = new Map();
  9. const mockDebounce = (fn, timeout) => {
  10. return (...args) => {
  11. if (debounceMap.has(fn)) {
  12. clearTimeout(debounceMap.get(fn));
  13. }
  14. debounceMap.set(
  15. fn,
  16. setTimeout(() => {
  17. fn.apply(fn, args);
  18. debounceMap.delete(fn);
  19. }, timeout)
  20. );
  21. };
  22. };
  23. return mockDebounce;
  24. });
  25. describe('OrganizationDashboard', function() {
  26. const org = TestStubs.Organization();
  27. const routerContext = TestStubs.routerContext([
  28. {router: TestStubs.router({params: {orgId: org.slug}})},
  29. ]);
  30. const team = TestStubs.Team();
  31. const teams = [team];
  32. beforeEach(function() {
  33. MockApiClient.addMockResponse({
  34. url: `/teams/${org.slug}/${team.slug}/members/`,
  35. body: [],
  36. });
  37. ProjectsStatsStore.reset();
  38. });
  39. afterEach(function() {
  40. MockApiClient.clearMockResponses();
  41. });
  42. describe('empty state', function() {
  43. it('renders with no projects', function() {
  44. const wrapper = mount(
  45. <Dashboard
  46. teams={teams}
  47. projects={[]}
  48. organization={org}
  49. params={{orgId: org.slug}}
  50. />,
  51. routerContext
  52. );
  53. expect(wrapper.find('Button[data-test-id="create-project"]').exists()).toBe(false);
  54. expect(wrapper.find('NoProjectMessage').exists()).toBe(true);
  55. });
  56. it('renders with 1 project, with no first event', function() {
  57. const projects = [TestStubs.Project({teams})];
  58. const wrapper = mount(
  59. <Dashboard
  60. teams={teams}
  61. projects={projects}
  62. organization={org}
  63. params={{orgId: org.slug}}
  64. />,
  65. routerContext
  66. );
  67. expect(wrapper.find('Button[data-test-id="create-project"]').exists()).toBe(true);
  68. expect(wrapper.find('TeamSection').exists()).toBe(true);
  69. expect(wrapper.find('Resources').exists()).toBe(true);
  70. });
  71. });
  72. describe('with projects', function() {
  73. it('renders TeamSection with two projects', function() {
  74. const projects = [
  75. TestStubs.Project({
  76. teams,
  77. firstEvent: true,
  78. }),
  79. TestStubs.Project({
  80. teams,
  81. isBookmarked: true,
  82. firstEvent: true,
  83. }),
  84. ];
  85. const wrapper = shallow(
  86. <Dashboard
  87. teams={teams}
  88. projects={projects}
  89. organization={org}
  90. params={{orgId: org.slug}}
  91. />,
  92. routerContext
  93. );
  94. expect(wrapper.find('Button[data-test-id="create-project"]').exists()).toBe(true);
  95. expect(wrapper.find('NoProjectMessage').exists()).toBe(false);
  96. expect(wrapper.find('TeamSection').exists()).toBe(true);
  97. expect(wrapper.find('Resources').exists()).toBe(false);
  98. });
  99. it('renders bookmarked projects first in team list', function() {
  100. const projects = [
  101. TestStubs.Project({
  102. id: '1',
  103. slug: 'm',
  104. teams,
  105. isBookmarked: false,
  106. stats: [],
  107. }),
  108. TestStubs.Project({
  109. id: '2',
  110. slug: 'm-fave',
  111. teams,
  112. isBookmarked: true,
  113. stats: [],
  114. }),
  115. TestStubs.Project({
  116. id: '3',
  117. slug: 'a-fave',
  118. teams,
  119. isBookmarked: true,
  120. stats: [],
  121. }),
  122. TestStubs.Project({
  123. id: '4',
  124. slug: 'z-fave',
  125. teams,
  126. isBookmarked: true,
  127. stats: [],
  128. }),
  129. TestStubs.Project({
  130. id: '5',
  131. slug: 'a',
  132. teams,
  133. isBookmarked: false,
  134. stats: [],
  135. }),
  136. TestStubs.Project({
  137. id: '6',
  138. slug: 'z',
  139. teams,
  140. isBookmarked: false,
  141. stats: [],
  142. }),
  143. ];
  144. MockApiClient.addMockResponse({
  145. url: `/organizations/${org.slug}/projects/`,
  146. body: [
  147. TestStubs.Project({
  148. teams,
  149. stats: [[1517281200, 2], [1517310000, 1]],
  150. }),
  151. ],
  152. });
  153. jest.useFakeTimers();
  154. const wrapper = mount(
  155. <Dashboard
  156. teams={teams}
  157. projects={projects}
  158. organization={org}
  159. params={{orgId: org.slug}}
  160. />,
  161. routerContext
  162. );
  163. jest.runAllTimers();
  164. jest.useRealTimers();
  165. const projectCards = wrapper.find('LazyLoadMock ProjectCard');
  166. expect(projectCards.at(0).prop('data-test-id')).toBe('a-fave');
  167. expect(projectCards.at(1).prop('data-test-id')).toBe('m-fave');
  168. expect(projectCards.at(2).prop('data-test-id')).toBe('z-fave');
  169. expect(projectCards.at(3).prop('data-test-id')).toBe('a');
  170. expect(projectCards.at(4).prop('data-test-id')).toBe('m');
  171. expect(projectCards.at(5).prop('data-test-id')).toBe('z');
  172. });
  173. });
  174. describe('ProjectsStatsStore', function() {
  175. const projects = [
  176. TestStubs.Project({
  177. id: '1',
  178. slug: 'm',
  179. teams,
  180. isBookmarked: false,
  181. }),
  182. TestStubs.Project({
  183. id: '2',
  184. slug: 'm-fave',
  185. teams,
  186. isBookmarked: true,
  187. }),
  188. TestStubs.Project({
  189. id: '3',
  190. slug: 'a-fave',
  191. teams,
  192. isBookmarked: true,
  193. }),
  194. TestStubs.Project({
  195. id: '4',
  196. slug: 'z-fave',
  197. teams,
  198. isBookmarked: true,
  199. }),
  200. TestStubs.Project({
  201. id: '5',
  202. slug: 'a',
  203. teams,
  204. isBookmarked: false,
  205. }),
  206. TestStubs.Project({
  207. id: '6',
  208. slug: 'z',
  209. teams,
  210. isBookmarked: false,
  211. }),
  212. ];
  213. it('uses ProjectsStatsStore to load stats', async function() {
  214. jest.useFakeTimers();
  215. ProjectsStatsStore.onStatsLoadSuccess([{...projects[0], stats: [[1517281200, 2]]}]);
  216. const loadStatsSpy = jest.spyOn(projectsActions, 'loadStatsForProject');
  217. const mock = MockApiClient.addMockResponse({
  218. url: `/organizations/${org.slug}/projects/`,
  219. body: projects.map(project => ({
  220. ...project,
  221. stats: [[1517281200, 2], [1517310000, 1]],
  222. })),
  223. });
  224. const wrapper = mount(
  225. <Dashboard
  226. teams={teams}
  227. projects={projects}
  228. organization={org}
  229. params={{orgId: org.slug}}
  230. />,
  231. routerContext
  232. );
  233. expect(loadStatsSpy).toHaveBeenCalledTimes(6);
  234. expect(mock).not.toHaveBeenCalled();
  235. // Has 5 Loading Cards because 1 project has been loaded in store already
  236. expect(wrapper.find('LoadingCard')).toHaveLength(5);
  237. // Advance timers so that batched request fires
  238. jest.advanceTimersByTime(51);
  239. expect(mock).toHaveBeenCalledTimes(1);
  240. // query ids = 3, 2, 4 = bookmarked
  241. // 1 - already loaded in store so shouldn't be in query
  242. expect(mock).toHaveBeenCalledWith(
  243. expect.anything(),
  244. expect.objectContaining({
  245. query: expect.objectContaining({
  246. query: 'id:3 id:2 id:4 id:5 id:6',
  247. }),
  248. })
  249. );
  250. jest.useRealTimers();
  251. await tick();
  252. await tick();
  253. wrapper.update();
  254. expect(wrapper.find('LoadingCard')).toHaveLength(0);
  255. expect(wrapper.find('Chart')).toHaveLength(6);
  256. // Resets store when it unmounts
  257. wrapper.unmount();
  258. expect(ProjectsStatsStore.getAll()).toEqual({});
  259. });
  260. });
  261. });