eventsGeoRequest.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {Fragment} from 'react';
  2. import {render, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import EventsGeoRequest, {
  4. EventsGeoRequestProps,
  5. } from 'sentry/components/charts/eventsGeoRequest';
  6. import * as genericDiscoverQuery from 'sentry/utils/discover/genericDiscoverQuery';
  7. describe('EventsRequest', function () {
  8. const project = TestStubs.Project();
  9. const organization = TestStubs.Organization();
  10. const makeProps = (
  11. partial: Partial<EventsGeoRequestProps> = {}
  12. ): EventsGeoRequestProps => {
  13. return {
  14. api: new MockApiClient(),
  15. organization,
  16. yAxis: ['count()'],
  17. query: 'event.type:transaction',
  18. projects: [parseInt(project.id, 10)],
  19. period: '24h',
  20. start: new Date(),
  21. end: new Date(),
  22. environments: [],
  23. children: jest.fn(() => <Fragment />),
  24. ...partial,
  25. };
  26. };
  27. describe('with props changes', () => {
  28. afterEach(() => {
  29. jest.restoreAllMocks();
  30. });
  31. it('renders with loading state', async () => {
  32. jest
  33. .spyOn(genericDiscoverQuery, 'doDiscoverQuery')
  34. .mockResolvedValue([{data: 'test'}, undefined, undefined]);
  35. const mock = jest.fn(() => <Fragment />);
  36. render(<EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>);
  37. await waitFor(() => {
  38. expect(mock).toHaveBeenNthCalledWith(
  39. 1,
  40. expect.objectContaining({
  41. errored: false,
  42. loading: true,
  43. reloading: false,
  44. tableData: undefined,
  45. })
  46. );
  47. });
  48. });
  49. it('makes requests', async () => {
  50. jest
  51. .spyOn(genericDiscoverQuery, 'doDiscoverQuery')
  52. .mockResolvedValue([{data: 'test'}, undefined, undefined]);
  53. const mock = jest.fn(() => <Fragment />);
  54. render(<EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>);
  55. await waitFor(() => {
  56. expect(mock).toHaveBeenLastCalledWith(
  57. expect.objectContaining({
  58. errored: false,
  59. loading: false,
  60. reloading: false,
  61. tableData: [{data: 'test'}],
  62. })
  63. );
  64. });
  65. });
  66. it('renders with error if request errors', async () => {
  67. jest.spyOn(genericDiscoverQuery, 'doDiscoverQuery').mockRejectedValue({});
  68. const mock = jest.fn(() => <Fragment />);
  69. render(<EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>);
  70. await waitFor(() => {
  71. expect(mock).toHaveBeenLastCalledWith(
  72. expect.objectContaining({
  73. errored: true,
  74. loading: false,
  75. reloading: false,
  76. tableData: undefined,
  77. })
  78. );
  79. });
  80. });
  81. it.each([{query: 'event.type:error'}, {yAxis: 'failure_count()'}, {period: '12h'}])(
  82. 'rerenders if %j prop changes',
  83. async (rerenderProps: Partial<EventsGeoRequestProps>) => {
  84. jest
  85. .spyOn(genericDiscoverQuery, 'doDiscoverQuery')
  86. .mockResolvedValue([{data: 'test'}, undefined, undefined]);
  87. const mock = jest.fn(() => <Fragment />);
  88. const {rerender} = render(
  89. <EventsGeoRequest {...makeProps()}>{mock}</EventsGeoRequest>
  90. );
  91. rerender(
  92. <EventsGeoRequest {...makeProps(rerenderProps)}>{mock}</EventsGeoRequest>
  93. );
  94. await waitFor(() => {
  95. expect(genericDiscoverQuery.doDiscoverQuery).toHaveBeenNthCalledWith(
  96. 2,
  97. expect.anything(),
  98. expect.anything(),
  99. expect.objectContaining(
  100. rerenderProps.period ? {statsPeriod: rerenderProps.period} : rerenderProps
  101. )
  102. );
  103. });
  104. }
  105. );
  106. });
  107. });