asyncComponent.spec.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React from 'react';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {Client} from 'app/api';
  4. import AsyncComponent from 'app/components/asyncComponent';
  5. describe('AsyncComponent', function() {
  6. class TestAsyncComponent extends AsyncComponent {
  7. shouldRenderBadRequests = true;
  8. constructor(props) {
  9. super(props);
  10. this.state = {};
  11. }
  12. getEndpoints() {
  13. return [['data', '/some/path/to/something/']];
  14. }
  15. renderBody() {
  16. return <div>{this.state.data.message}</div>;
  17. }
  18. }
  19. it('renders on successful request', function() {
  20. Client.clearMockResponses();
  21. Client.addMockResponse({
  22. url: '/some/path/to/something/',
  23. method: 'GET',
  24. body: {
  25. message: 'hi',
  26. },
  27. });
  28. const wrapper = mountWithTheme(<TestAsyncComponent />);
  29. expect(wrapper.find('div')).toHaveLength(1);
  30. expect(wrapper.find('div').text()).toEqual('hi');
  31. });
  32. it('renders error message', function() {
  33. Client.clearMockResponses();
  34. Client.addMockResponse({
  35. url: '/some/path/to/something/',
  36. method: 'GET',
  37. body: {
  38. detail: 'oops there was a problem',
  39. },
  40. statusCode: 400,
  41. });
  42. const wrapper = mountWithTheme(<TestAsyncComponent />);
  43. expect(wrapper.find('LoadingError')).toHaveLength(1);
  44. expect(wrapper.find('LoadingError').text()).toEqual('oops there was a problem');
  45. });
  46. describe('multi-route component', () => {
  47. class MultiRouteComponent extends TestAsyncComponent {
  48. getEndpoints() {
  49. return [
  50. ['data', '/some/path/to/something/'],
  51. ['project', '/another/path/here'],
  52. ];
  53. }
  54. }
  55. it('calls onLoadAllEndpointsSuccess when all endpoints have been loaded', () => {
  56. jest.useFakeTimers();
  57. jest.spyOn(Client.prototype, 'request').mockImplementation((url, options) => {
  58. const timeout = url.includes('something') ? 100 : 50;
  59. setTimeout(
  60. () =>
  61. options.success({
  62. message: 'good',
  63. }),
  64. timeout
  65. );
  66. });
  67. const mockOnAllEndpointsSuccess = jest.spyOn(
  68. MultiRouteComponent.prototype,
  69. 'onLoadAllEndpointsSuccess'
  70. );
  71. const wrapper = mountWithTheme(<MultiRouteComponent />);
  72. expect(wrapper.state('loading')).toEqual(true);
  73. expect(wrapper.state('remainingRequests')).toEqual(2);
  74. jest.advanceTimersByTime(40);
  75. expect(wrapper.state('loading')).toEqual(true);
  76. expect(wrapper.state('remainingRequests')).toEqual(2);
  77. jest.advanceTimersByTime(40);
  78. expect(wrapper.state('loading')).toEqual(true);
  79. expect(wrapper.state('remainingRequests')).toEqual(1);
  80. expect(mockOnAllEndpointsSuccess).not.toHaveBeenCalled();
  81. jest.advanceTimersByTime(40);
  82. expect(wrapper.state('loading')).toEqual(false);
  83. expect(wrapper.state('remainingRequests')).toEqual(0);
  84. expect(mockOnAllEndpointsSuccess).toHaveBeenCalled();
  85. jest.restoreAllMocks();
  86. });
  87. });
  88. });