globalSelectionHeader.spec.jsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import React from 'react';
  2. import {initializeOrg} from 'app-test/helpers/initializeOrg';
  3. import {mount} from 'enzyme';
  4. import GlobalSelectionHeader from 'app/components/organizations/globalSelectionHeader';
  5. import GlobalSelectionStore from 'app/stores/globalSelectionStore';
  6. import * as globalActions from 'app/actionCreators/globalSelection';
  7. const changeQuery = (routerContext, query) => ({
  8. ...routerContext,
  9. context: {
  10. ...routerContext.context,
  11. router: {
  12. ...routerContext.context.router,
  13. location: {
  14. query,
  15. },
  16. },
  17. },
  18. });
  19. describe('GlobalSelectionHeader', function() {
  20. const {organization, router, routerContext} = initializeOrg({
  21. organization: TestStubs.Organization({features: ['global-views']}),
  22. router: {
  23. location: {query: {}},
  24. },
  25. });
  26. beforeAll(function() {
  27. jest.spyOn(globalActions, 'updateDateTime');
  28. jest.spyOn(globalActions, 'updateEnvironments');
  29. jest.spyOn(globalActions, 'updateProjects');
  30. MockApiClient.addMockResponse({
  31. url: `/organizations/${organization.slug}/environments/`,
  32. body: TestStubs.Environments(),
  33. });
  34. });
  35. beforeEach(function() {
  36. GlobalSelectionStore.reset();
  37. [
  38. globalActions.updateDateTime,
  39. globalActions.updateProjects,
  40. globalActions.updateEnvironments,
  41. router.push,
  42. router.replace,
  43. ].forEach(mock => mock.mockClear());
  44. });
  45. it('does not update router if there is custom routing', function() {
  46. mount(
  47. <GlobalSelectionHeader organization={organization} hasCustomRouting />,
  48. routerContext
  49. );
  50. expect(router.push).not.toHaveBeenCalled();
  51. });
  52. it('replaces URL with values from store when mounted with no query params', function() {
  53. mount(<GlobalSelectionHeader organization={organization} />, routerContext);
  54. expect(router.replace).toHaveBeenCalledWith(
  55. expect.objectContaining({
  56. query: {
  57. environment: [],
  58. project: [],
  59. statsPeriod: '14d',
  60. utc: 'true',
  61. },
  62. })
  63. );
  64. });
  65. it('only updates GlobalSelection store when mounted with query params', async function() {
  66. mount(
  67. <GlobalSelectionHeader organization={organization} />,
  68. changeQuery(routerContext, {
  69. statsPeriod: '7d',
  70. })
  71. );
  72. expect(router.push).not.toHaveBeenCalled();
  73. expect(globalActions.updateDateTime).toHaveBeenCalledWith({
  74. period: '7d',
  75. utc: null,
  76. start: null,
  77. end: null,
  78. });
  79. expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
  80. expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
  81. await tick();
  82. expect(GlobalSelectionStore.get()).toEqual({
  83. datetime: {
  84. period: '7d',
  85. utc: null,
  86. start: null,
  87. end: null,
  88. },
  89. environments: [],
  90. projects: [],
  91. });
  92. });
  93. it('updates GlobalSelection store when re-rendered with different query params', async function() {
  94. let wrapper = mount(
  95. <GlobalSelectionHeader organization={organization} />,
  96. changeQuery(routerContext, {
  97. statsPeriod: '7d',
  98. })
  99. );
  100. wrapper.setContext(
  101. changeQuery(routerContext, {
  102. statsPeriod: '21d',
  103. }).context
  104. );
  105. await tick();
  106. wrapper.update();
  107. expect(globalActions.updateDateTime).toHaveBeenCalledWith({
  108. period: '21d',
  109. utc: null,
  110. start: null,
  111. end: null,
  112. });
  113. expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
  114. expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
  115. expect(GlobalSelectionStore.get()).toEqual({
  116. datetime: {
  117. period: '21d',
  118. utc: null,
  119. start: null,
  120. end: null,
  121. },
  122. environments: [],
  123. projects: [],
  124. });
  125. });
  126. it('does not update store if url params have not changed', async function() {
  127. let wrapper = mount(
  128. <GlobalSelectionHeader organization={organization} />,
  129. changeQuery(routerContext, {
  130. statsPeriod: '7d',
  131. })
  132. );
  133. [
  134. globalActions.updateDateTime,
  135. globalActions.updateProjects,
  136. globalActions.updateEnvironments,
  137. ].forEach(mock => mock.mockClear());
  138. wrapper.setContext(
  139. changeQuery(routerContext, {
  140. statsPeriod: '7d',
  141. }).context
  142. );
  143. await tick();
  144. wrapper.update();
  145. expect(globalActions.updateDateTime).not.toHaveBeenCalled();
  146. expect(globalActions.updateProjects).not.toHaveBeenCalled();
  147. expect(globalActions.updateEnvironments).not.toHaveBeenCalled();
  148. expect(GlobalSelectionStore.get()).toEqual({
  149. datetime: {
  150. period: '7d',
  151. utc: null,
  152. start: null,
  153. end: null,
  154. },
  155. environments: [],
  156. projects: [],
  157. });
  158. });
  159. describe('Single project selection mode', function() {
  160. it('selects first project if more than one is requested', function() {
  161. const initializationObj = initializeOrg({
  162. router: {
  163. location: {query: {project: [1, 2]}},
  164. },
  165. });
  166. mount(
  167. <GlobalSelectionHeader organization={initializationObj.organization} />,
  168. initializationObj.routerContext
  169. );
  170. expect(globalActions.updateProjects).toHaveBeenCalledWith([1]);
  171. });
  172. it('selects first project if none (i.e. all) is requested', function() {
  173. const project = TestStubs.Project({id: '3'});
  174. const org = TestStubs.Organization({projects: [project]});
  175. const initializationObj = initializeOrg({
  176. organization: org,
  177. router: {
  178. location: {query: {}},
  179. },
  180. });
  181. mount(
  182. <GlobalSelectionHeader organization={initializationObj.organization} />,
  183. initializationObj.routerContext
  184. );
  185. expect(globalActions.updateProjects).toHaveBeenCalledWith([3]);
  186. });
  187. });
  188. });