asyncComponent.spec.jsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React from 'react';
  2. import {mount, shallow} from '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. let wrapper = shallow(<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. let wrapper = mount(<TestAsyncComponent />);
  43. expect(wrapper.find('LoadingError')).toHaveLength(1);
  44. expect(
  45. wrapper
  46. .find('LoadingError')
  47. .find('p')
  48. .text()
  49. ).toEqual('oops there was a problem');
  50. });
  51. });