withApi.spec.jsx 948 B

12345678910111213141516171819202122232425262728293031323334
  1. import {mount} from 'enzyme';
  2. import React from 'react';
  3. import withApi from 'app/utils/withApi';
  4. describe('withApi', function() {
  5. let apiInstance;
  6. const MyComponent = jest.fn(props => {
  7. apiInstance = props.api;
  8. return <div />;
  9. });
  10. it('renders MyComponent with an api prop', function() {
  11. const MyComponentWithApi = withApi(MyComponent);
  12. mount(<MyComponentWithApi />);
  13. expect(MyComponent).toHaveBeenCalledWith(
  14. expect.objectContaining({
  15. api: expect.anything(),
  16. }),
  17. expect.anything()
  18. );
  19. });
  20. it('cancels pending API requests when component is unmounted', function() {
  21. const MyComponentWithApi = withApi(MyComponent);
  22. const wrapper = mount(<MyComponentWithApi />);
  23. jest.spyOn(apiInstance, 'clear');
  24. expect(apiInstance.clear).not.toHaveBeenCalled();
  25. wrapper.unmount();
  26. expect(apiInstance.clear).toHaveBeenCalled();
  27. apiInstance.clear.mockRestore();
  28. });
  29. });