index.spec.jsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import * as projectsActions from 'app/actionCreators/projects';
  3. import ProjectsStatsStore from 'app/stores/projectsStatsStore';
  4. import {Dashboard} from 'app/views/projectsDashboard';
  5. jest.unmock('lodash/debounce');
  6. jest.mock('lodash/debounce', () => {
  7. const debounceMap = new Map();
  8. const mockDebounce =
  9. (fn, timeout) =>
  10. (...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. return mockDebounce;
  23. });
  24. describe('ProjectsDashboard', function () {
  25. const org = TestStubs.Organization();
  26. const routerContext = TestStubs.routerContext([
  27. {router: TestStubs.router({params: {orgId: org.slug}})},
  28. ]);
  29. const team = TestStubs.Team();
  30. const teams = [team];
  31. beforeEach(function () {
  32. MockApiClient.addMockResponse({
  33. url: `/teams/${org.slug}/${team.slug}/members/`,
  34. body: [],
  35. });
  36. ProjectsStatsStore.reset();
  37. });
  38. afterEach(function () {
  39. MockApiClient.clearMockResponses();
  40. });
  41. describe('empty state', function () {
  42. it('renders with no projects', function () {
  43. const noProjectTeams = [TestStubs.Team({projects: []})];
  44. const wrapper = mountWithTheme(
  45. <Dashboard
  46. teams={noProjectTeams}
  47. organization={org}
  48. params={{orgId: org.slug}}
  49. />,
  50. routerContext
  51. );
  52. expect(wrapper.find('Button[data-test-id="create-project"]').exists()).toBe(false);
  53. expect(wrapper.find('NoProjectMessage').exists()).toBe(true);
  54. });
  55. it('renders with 1 project, with no first event', function () {
  56. const projects = [TestStubs.Project({teams})];
  57. const teamsWithOneProject = [TestStubs.Team({projects})];
  58. const wrapper = mountWithTheme(
  59. <Dashboard
  60. teams={teamsWithOneProject}
  61. organization={org}
  62. params={{orgId: org.slug}}
  63. />,
  64. routerContext
  65. );
  66. expect(wrapper.find('Button[data-test-id="create-project"]').exists()).toBe(true);
  67. expect(wrapper.find('TeamSection').exists()).toBe(true);
  68. expect(wrapper.find('Resources').exists()).toBe(true);
  69. });
  70. });
  71. describe('with projects', function () {
  72. it('renders TeamSection with two projects', function () {
  73. const projects = [
  74. TestStubs.Project({
  75. teams,
  76. firstEvent: true,
  77. }),
  78. TestStubs.Project({
  79. slug: 'project2',
  80. teams,
  81. isBookmarked: true,
  82. firstEvent: true,
  83. }),
  84. ];
  85. const teamsWithTwoProjects = [TestStubs.Team({projects})];
  86. const wrapper = mountWithTheme(
  87. <Dashboard
  88. teams={teamsWithTwoProjects}
  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. const teamsWithFavProjects = [TestStubs.Team({projects})];
  145. MockApiClient.addMockResponse({
  146. url: `/organizations/${org.slug}/projects/`,
  147. body: [
  148. TestStubs.Project({
  149. teams,
  150. stats: [
  151. [1517281200, 2],
  152. [1517310000, 1],
  153. ],
  154. }),
  155. ],
  156. });
  157. jest.useFakeTimers();
  158. const wrapper = mountWithTheme(
  159. <Dashboard
  160. teams={teamsWithFavProjects}
  161. organization={org}
  162. params={{orgId: org.slug}}
  163. />,
  164. routerContext
  165. );
  166. jest.runAllTimers();
  167. jest.useRealTimers();
  168. const projectCards = wrapper.find('LazyLoadMock ProjectCard');
  169. expect(projectCards.at(0).prop('data-test-id')).toBe('a-fave');
  170. expect(projectCards.at(1).prop('data-test-id')).toBe('m-fave');
  171. expect(projectCards.at(2).prop('data-test-id')).toBe('z-fave');
  172. expect(projectCards.at(3).prop('data-test-id')).toBe('a');
  173. expect(projectCards.at(4).prop('data-test-id')).toBe('m');
  174. expect(projectCards.at(5).prop('data-test-id')).toBe('z');
  175. });
  176. });
  177. describe('ProjectsStatsStore', function () {
  178. const projects = [
  179. TestStubs.Project({
  180. id: '1',
  181. slug: 'm',
  182. teams,
  183. isBookmarked: false,
  184. }),
  185. TestStubs.Project({
  186. id: '2',
  187. slug: 'm-fave',
  188. teams,
  189. isBookmarked: true,
  190. }),
  191. TestStubs.Project({
  192. id: '3',
  193. slug: 'a-fave',
  194. teams,
  195. isBookmarked: true,
  196. }),
  197. TestStubs.Project({
  198. id: '4',
  199. slug: 'z-fave',
  200. teams,
  201. isBookmarked: true,
  202. }),
  203. TestStubs.Project({
  204. id: '5',
  205. slug: 'a',
  206. teams,
  207. isBookmarked: false,
  208. }),
  209. TestStubs.Project({
  210. id: '6',
  211. slug: 'z',
  212. teams,
  213. isBookmarked: false,
  214. }),
  215. ];
  216. const teamsWithStatTestProjects = [TestStubs.Team({projects})];
  217. it('uses ProjectsStatsStore to load stats', async function () {
  218. jest.useFakeTimers();
  219. ProjectsStatsStore.onStatsLoadSuccess([{...projects[0], stats: [[1517281200, 2]]}]);
  220. const loadStatsSpy = jest.spyOn(projectsActions, 'loadStatsForProject');
  221. const mock = MockApiClient.addMockResponse({
  222. url: `/organizations/${org.slug}/projects/`,
  223. body: projects.map(project => ({
  224. ...project,
  225. stats: [
  226. [1517281200, 2],
  227. [1517310000, 1],
  228. ],
  229. })),
  230. });
  231. const wrapper = mountWithTheme(
  232. <Dashboard
  233. teams={teamsWithStatTestProjects}
  234. organization={org}
  235. params={{orgId: org.slug}}
  236. />,
  237. routerContext
  238. );
  239. expect(loadStatsSpy).toHaveBeenCalledTimes(6);
  240. expect(mock).not.toHaveBeenCalled();
  241. // Has 5 Loading Cards because 1 project has been loaded in store already
  242. expect(wrapper.find('Placeholder')).toHaveLength(5);
  243. // Advance timers so that batched request fires
  244. jest.advanceTimersByTime(51);
  245. expect(mock).toHaveBeenCalledTimes(1);
  246. // query ids = 3, 2, 4 = bookmarked
  247. // 1 - already loaded in store so shouldn't be in query
  248. expect(mock).toHaveBeenCalledWith(
  249. expect.anything(),
  250. expect.objectContaining({
  251. query: expect.objectContaining({
  252. query: 'id:3 id:2 id:4 id:5 id:6',
  253. }),
  254. })
  255. );
  256. jest.useRealTimers();
  257. await tick();
  258. await tick();
  259. wrapper.update();
  260. expect(wrapper.find('Placeholder')).toHaveLength(0);
  261. expect(wrapper.find('Chart')).toHaveLength(6);
  262. // Resets store when it unmounts
  263. wrapper.unmount();
  264. expect(ProjectsStatsStore.getAll()).toEqual({});
  265. });
  266. it('renders an error from withTeamsForUser', function () {
  267. const wrapper = mountWithTheme(
  268. <Dashboard error={Error('uhoh')} organization={org} params={{orgId: org.slug}} />,
  269. routerContext
  270. );
  271. expect(wrapper.find('LoadingError').exists()).toBe(true);
  272. });
  273. });
  274. });