useApi.spec.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {mountWithTheme} from 'sentry-test/reactTestingLibrary';
  2. import {Client} from 'app/api';
  3. import useApi from 'app/utils/useApi';
  4. describe('useApi', function () {
  5. let apiInstance: Client;
  6. type Props = {
  7. /**
  8. * Test passthrough API clients
  9. */
  10. api?: Client;
  11. /**
  12. * Test persistInFlight
  13. */
  14. persistInFlight?: boolean;
  15. };
  16. const MyComponent: React.FC<Props> = ({api, persistInFlight}) => {
  17. apiInstance = useApi({api, persistInFlight});
  18. return <div />;
  19. };
  20. it('renders MyComponent with an api prop', function () {
  21. mountWithTheme(<MyComponent />);
  22. expect(apiInstance).toBeInstanceOf(Client);
  23. });
  24. it('cancels pending API requests when component is unmounted', function () {
  25. const {unmount} = mountWithTheme(<MyComponent />);
  26. jest.spyOn(apiInstance, 'clear');
  27. unmount();
  28. expect(apiInstance.clear).toHaveBeenCalled();
  29. });
  30. it('does not cancel inflights when persistInFlight is true', function () {
  31. const {unmount} = mountWithTheme(<MyComponent persistInFlight />);
  32. jest.spyOn(apiInstance, 'clear');
  33. unmount();
  34. expect(apiInstance.clear).not.toHaveBeenCalled();
  35. });
  36. it('uses pass through API when provided', function () {
  37. const myClient = new Client();
  38. const {unmount} = mountWithTheme(<MyComponent api={myClient} />);
  39. jest.spyOn(myClient, 'clear');
  40. unmount();
  41. expect(myClient.clear).toHaveBeenCalled();
  42. });
  43. });