withApi.spec.jsx 1.3 KB

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