globalSelectionHeader.spec.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. router: {
  22. location: {query: {}},
  23. },
  24. });
  25. beforeAll(function() {
  26. jest.spyOn(globalActions, 'updateDateTime');
  27. jest.spyOn(globalActions, 'updateEnvironments');
  28. jest.spyOn(globalActions, 'updateProjects');
  29. MockApiClient.addMockResponse({
  30. url: `/organizations/${organization.slug}/environments/`,
  31. body: TestStubs.Environments(),
  32. });
  33. });
  34. beforeEach(function() {
  35. GlobalSelectionStore.reset();
  36. [
  37. globalActions.updateDateTime,
  38. globalActions.updateProjects,
  39. globalActions.updateEnvironments,
  40. router.push,
  41. ].forEach(mock => mock.mockClear());
  42. });
  43. it('does not update router if there is custom routing', function() {
  44. mount(
  45. <GlobalSelectionHeader organization={organization} hasCustomRouting />,
  46. routerContext
  47. );
  48. expect(router.push).not.toHaveBeenCalled();
  49. });
  50. it('updates URL with values from store when mounted with no query params', function() {
  51. mount(<GlobalSelectionHeader organization={organization} />, routerContext);
  52. expect(router.push).toHaveBeenCalledWith(
  53. expect.objectContaining({
  54. query: {
  55. environment: [],
  56. project: [],
  57. statsPeriod: '14d',
  58. utc: 'true',
  59. },
  60. })
  61. );
  62. });
  63. it('only updates GlobalSelection store when mounted with query params', async function() {
  64. mount(
  65. <GlobalSelectionHeader organization={organization} />,
  66. changeQuery(routerContext, {
  67. statsPeriod: '7d',
  68. })
  69. );
  70. expect(router.push).not.toHaveBeenCalled();
  71. expect(globalActions.updateDateTime).toHaveBeenCalledWith({
  72. period: '7d',
  73. utc: null,
  74. start: null,
  75. end: null,
  76. });
  77. expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
  78. expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
  79. await tick();
  80. expect(GlobalSelectionStore.get()).toEqual({
  81. datetime: {
  82. period: '7d',
  83. utc: null,
  84. start: null,
  85. end: null,
  86. },
  87. environments: [],
  88. projects: [],
  89. });
  90. });
  91. it('updates GlobalSelection store when re-rendered with different query params', async function() {
  92. let wrapper = mount(
  93. <GlobalSelectionHeader organization={organization} />,
  94. changeQuery(routerContext, {
  95. statsPeriod: '7d',
  96. })
  97. );
  98. wrapper.setContext(
  99. changeQuery(routerContext, {
  100. statsPeriod: '21d',
  101. }).context
  102. );
  103. await tick();
  104. wrapper.update();
  105. expect(globalActions.updateDateTime).toHaveBeenCalledWith({
  106. period: '21d',
  107. utc: null,
  108. start: null,
  109. end: null,
  110. });
  111. expect(globalActions.updateProjects).toHaveBeenCalledWith([]);
  112. expect(globalActions.updateEnvironments).toHaveBeenCalledWith([]);
  113. expect(GlobalSelectionStore.get()).toEqual({
  114. datetime: {
  115. period: '21d',
  116. utc: null,
  117. start: null,
  118. end: null,
  119. },
  120. environments: [],
  121. projects: [],
  122. });
  123. });
  124. it('does not update store if url params have not changed', async function() {
  125. let wrapper = mount(
  126. <GlobalSelectionHeader organization={organization} />,
  127. changeQuery(routerContext, {
  128. statsPeriod: '7d',
  129. })
  130. );
  131. [
  132. globalActions.updateDateTime,
  133. globalActions.updateProjects,
  134. globalActions.updateEnvironments,
  135. ].forEach(mock => mock.mockClear());
  136. wrapper.setContext(
  137. changeQuery(routerContext, {
  138. statsPeriod: '7d',
  139. }).context
  140. );
  141. await tick();
  142. wrapper.update();
  143. expect(globalActions.updateDateTime).not.toHaveBeenCalled();
  144. expect(globalActions.updateProjects).not.toHaveBeenCalled();
  145. expect(globalActions.updateEnvironments).not.toHaveBeenCalled();
  146. expect(GlobalSelectionStore.get()).toEqual({
  147. datetime: {
  148. period: '7d',
  149. utc: null,
  150. start: null,
  151. end: null,
  152. },
  153. environments: [],
  154. projects: [],
  155. });
  156. });
  157. });