asyncComponent.spec.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {Client} from 'app/api';
  3. import AsyncComponent from 'app/components/asyncComponent';
  4. describe('AsyncComponent', function () {
  5. class TestAsyncComponent extends AsyncComponent {
  6. shouldRenderBadRequests = true;
  7. constructor(props) {
  8. super(props);
  9. this.state = {};
  10. }
  11. getEndpoints() {
  12. return [['data', '/some/path/to/something/']];
  13. }
  14. renderBody() {
  15. return <div>{this.state.data.message}</div>;
  16. }
  17. }
  18. it('renders on successful request', function () {
  19. Client.clearMockResponses();
  20. Client.addMockResponse({
  21. url: '/some/path/to/something/',
  22. method: 'GET',
  23. body: {
  24. message: 'hi',
  25. },
  26. });
  27. const wrapper = mountWithTheme(<TestAsyncComponent />);
  28. expect(wrapper.find('div')).toHaveLength(1);
  29. expect(wrapper.find('div').text()).toEqual('hi');
  30. });
  31. it('renders error message', function () {
  32. Client.clearMockResponses();
  33. Client.addMockResponse({
  34. url: '/some/path/to/something/',
  35. method: 'GET',
  36. body: {
  37. detail: 'oops there was a problem',
  38. },
  39. statusCode: 400,
  40. });
  41. const wrapper = mountWithTheme(<TestAsyncComponent />);
  42. expect(wrapper.find('LoadingError')).toHaveLength(1);
  43. expect(wrapper.find('LoadingError').text()).toEqual('oops there was a problem');
  44. });
  45. it('renders only unique error message', async function () {
  46. Client.clearMockResponses();
  47. Client.addMockResponse({
  48. url: '/first/path/',
  49. method: 'GET',
  50. body: {
  51. detail: 'oops there was a problem',
  52. },
  53. statusCode: 400,
  54. });
  55. Client.addMockResponse({
  56. url: '/second/path/',
  57. method: 'GET',
  58. body: {
  59. detail: 'oops there was a problem',
  60. },
  61. statusCode: 400,
  62. });
  63. Client.addMockResponse({
  64. url: '/third/path/',
  65. method: 'GET',
  66. body: {
  67. detail: 'oops there was a different problem',
  68. },
  69. statusCode: 400,
  70. });
  71. class UniqueErrorsAsyncComponent extends AsyncComponent {
  72. shouldRenderBadRequests = true;
  73. getEndpoints() {
  74. return [
  75. ['first', '/first/path/'],
  76. ['second', '/second/path/'],
  77. ['third', '/third/path/'],
  78. ];
  79. }
  80. renderBody() {
  81. return <div>{this.state.data.message}</div>;
  82. }
  83. }
  84. const wrapper = mountWithTheme(<UniqueErrorsAsyncComponent />);
  85. expect(wrapper.find('LoadingError').text()).toEqual(
  86. 'oops there was a problem\noops there was a different problem'
  87. );
  88. });
  89. describe('multi-route component', () => {
  90. class MultiRouteComponent extends TestAsyncComponent {
  91. getEndpoints() {
  92. return [
  93. ['data', '/some/path/to/something/'],
  94. ['project', '/another/path/here'],
  95. ];
  96. }
  97. }
  98. it('calls onLoadAllEndpointsSuccess when all endpoints have been loaded', () => {
  99. jest.useFakeTimers();
  100. jest.spyOn(Client.prototype, 'request').mockImplementation((url, options) => {
  101. const timeout = url.includes('something') ? 100 : 50;
  102. setTimeout(
  103. () =>
  104. options.success({
  105. message: 'good',
  106. }),
  107. timeout
  108. );
  109. });
  110. const mockOnAllEndpointsSuccess = jest.spyOn(
  111. MultiRouteComponent.prototype,
  112. 'onLoadAllEndpointsSuccess'
  113. );
  114. const wrapper = mountWithTheme(<MultiRouteComponent />);
  115. expect(wrapper.state('loading')).toEqual(true);
  116. expect(wrapper.state('remainingRequests')).toEqual(2);
  117. jest.advanceTimersByTime(40);
  118. expect(wrapper.state('loading')).toEqual(true);
  119. expect(wrapper.state('remainingRequests')).toEqual(2);
  120. jest.advanceTimersByTime(40);
  121. expect(wrapper.state('loading')).toEqual(true);
  122. expect(wrapper.state('remainingRequests')).toEqual(1);
  123. expect(mockOnAllEndpointsSuccess).not.toHaveBeenCalled();
  124. jest.advanceTimersByTime(40);
  125. expect(wrapper.state('loading')).toEqual(false);
  126. expect(wrapper.state('remainingRequests')).toEqual(0);
  127. expect(mockOnAllEndpointsSuccess).toHaveBeenCalled();
  128. jest.restoreAllMocks();
  129. });
  130. });
  131. });