eventsRequest.spec.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import {mount} from 'enzyme';
  2. import React from 'react';
  3. import {doEventsRequest} from 'app/actionCreators/events';
  4. import EventsRequest from 'app/views/events/utils/eventsRequest';
  5. const COUNT_OBJ = {
  6. count: 123,
  7. };
  8. jest.mock('app/actionCreators/events', () => {
  9. return {
  10. doEventsRequest: jest.fn(),
  11. };
  12. });
  13. describe('EventsRequest', function() {
  14. const project = TestStubs.Project();
  15. const organization = TestStubs.Organization();
  16. const mock = jest.fn(() => null);
  17. const DEFAULTS = {
  18. api: {},
  19. projects: [parseInt(project.id, 10)],
  20. environments: [],
  21. period: '24h',
  22. organization,
  23. tag: 'release',
  24. includePrevious: false,
  25. includeTimeseries: true,
  26. };
  27. let wrapper;
  28. describe('with props changes', function() {
  29. beforeAll(function() {
  30. doEventsRequest.mockImplementation(() =>
  31. Promise.resolve({
  32. data: [[new Date(), [COUNT_OBJ]]],
  33. })
  34. );
  35. wrapper = mount(<EventsRequest {...DEFAULTS}>{mock}</EventsRequest>);
  36. });
  37. it('makes requests', async function() {
  38. expect(mock).toHaveBeenNthCalledWith(
  39. 1,
  40. expect.objectContaining({
  41. loading: true,
  42. })
  43. );
  44. expect(mock).toHaveBeenLastCalledWith(
  45. expect.objectContaining({
  46. loading: false,
  47. timeseriesData: [
  48. {
  49. seriesName: expect.anything(),
  50. data: [
  51. expect.objectContaining({
  52. name: expect.any(Number),
  53. value: 123,
  54. }),
  55. ],
  56. },
  57. ],
  58. originalTimeseriesData: [[expect.anything(), expect.anything()]],
  59. })
  60. );
  61. expect(doEventsRequest).toHaveBeenCalled();
  62. });
  63. it('makes a new request if projects prop changes', async function() {
  64. doEventsRequest.mockClear();
  65. wrapper.setProps({projects: [123]});
  66. await tick();
  67. wrapper.update();
  68. expect(doEventsRequest).toHaveBeenCalledWith(
  69. expect.anything(),
  70. expect.objectContaining({
  71. projects: [123],
  72. })
  73. );
  74. });
  75. it('makes a new request if environments prop changes', async function() {
  76. doEventsRequest.mockClear();
  77. wrapper.setProps({environments: ['dev']});
  78. await tick();
  79. wrapper.update();
  80. expect(doEventsRequest).toHaveBeenCalledWith(
  81. expect.anything(),
  82. expect.objectContaining({
  83. environments: ['dev'],
  84. })
  85. );
  86. });
  87. it('makes a new request if period prop changes', async function() {
  88. doEventsRequest.mockClear();
  89. wrapper.setProps({period: '7d'});
  90. await tick();
  91. wrapper.update();
  92. expect(doEventsRequest).toHaveBeenCalledWith(
  93. expect.anything(),
  94. expect.objectContaining({
  95. period: '7d',
  96. })
  97. );
  98. });
  99. });
  100. describe('transforms', function() {
  101. beforeEach(function() {
  102. doEventsRequest.mockClear();
  103. });
  104. it('expands period in query if `includePrevious`', async function() {
  105. doEventsRequest.mockImplementation(() =>
  106. Promise.resolve({
  107. data: [
  108. [new Date(), [{...COUNT_OBJ, count: 321}, {...COUNT_OBJ, count: 79}]],
  109. [new Date(), [COUNT_OBJ]],
  110. ],
  111. })
  112. );
  113. wrapper = mount(
  114. <EventsRequest {...DEFAULTS} includePrevious={true}>
  115. {mock}
  116. </EventsRequest>
  117. );
  118. await tick();
  119. wrapper.update();
  120. // actionCreator handles expanding the period when calling the API
  121. expect(doEventsRequest).toHaveBeenCalledWith(
  122. expect.anything(),
  123. expect.objectContaining({
  124. period: '24h',
  125. })
  126. );
  127. expect(mock).toHaveBeenLastCalledWith(
  128. expect.objectContaining({
  129. loading: false,
  130. allTimeseriesData: [
  131. [
  132. expect.anything(),
  133. [
  134. expect.objectContaining({count: 321}),
  135. expect.objectContaining({count: 79}),
  136. ],
  137. ],
  138. [expect.anything(), [expect.objectContaining({count: 123})]],
  139. ],
  140. timeseriesData: [
  141. {
  142. seriesName: expect.anything(),
  143. data: [
  144. expect.objectContaining({
  145. name: expect.anything(),
  146. value: 123,
  147. }),
  148. ],
  149. },
  150. ],
  151. previousTimeseriesData: {
  152. seriesName: 'Previous Period',
  153. data: [
  154. expect.objectContaining({
  155. name: expect.anything(),
  156. value: 400,
  157. }),
  158. ],
  159. },
  160. originalTimeseriesData: [
  161. [expect.anything(), [expect.objectContaining({count: 123})]],
  162. ],
  163. originalPreviousTimeseriesData: [
  164. [
  165. expect.anything(),
  166. [
  167. expect.objectContaining({count: 321}),
  168. expect.objectContaining({count: 79}),
  169. ],
  170. ],
  171. ],
  172. })
  173. );
  174. });
  175. it('aggregates counts per timestamp only when `includeTimeAggregation` prop is true', async function() {
  176. doEventsRequest.mockImplementation(() =>
  177. Promise.resolve({
  178. data: [[new Date(), [COUNT_OBJ, {...COUNT_OBJ, count: 100}]]],
  179. })
  180. );
  181. wrapper = mount(
  182. <EventsRequest {...DEFAULTS} includeTimeseries={true}>
  183. {mock}
  184. </EventsRequest>
  185. );
  186. await tick();
  187. wrapper.update();
  188. expect(mock).toHaveBeenLastCalledWith(
  189. expect.objectContaining({
  190. timeAggregatedData: {},
  191. })
  192. );
  193. wrapper.setProps({
  194. includeTimeAggregation: true,
  195. timeAggregationSeriesName: 'aggregated series',
  196. });
  197. await tick();
  198. wrapper.update();
  199. expect(mock).toHaveBeenLastCalledWith(
  200. expect.objectContaining({
  201. timeAggregatedData: {
  202. seriesName: 'aggregated series',
  203. data: [{name: expect.anything(), value: 223}],
  204. },
  205. })
  206. );
  207. });
  208. it('aggregates all counts per timestamp when category name identical', async function() {
  209. doEventsRequest.mockImplementation(() =>
  210. Promise.resolve({
  211. data: [[new Date(), [COUNT_OBJ, {...COUNT_OBJ, count: 100}]]],
  212. })
  213. );
  214. wrapper = mount(
  215. <EventsRequest {...DEFAULTS} includeTimeseries={true}>
  216. {mock}
  217. </EventsRequest>
  218. );
  219. await tick();
  220. wrapper.update();
  221. expect(mock).toHaveBeenLastCalledWith(
  222. expect.objectContaining({
  223. timeAggregatedData: {},
  224. })
  225. );
  226. wrapper.setProps({
  227. includeTimeAggregation: true,
  228. timeAggregationSeriesName: 'aggregated series',
  229. });
  230. await tick();
  231. wrapper.update();
  232. expect(mock).toHaveBeenLastCalledWith(
  233. expect.objectContaining({
  234. timeAggregatedData: {
  235. seriesName: 'aggregated series',
  236. data: [{name: expect.anything(), value: 223}],
  237. },
  238. })
  239. );
  240. });
  241. });
  242. });