useApi.spec.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import {Client} from 'sentry/api';
  3. import useApi from 'sentry/utils/useApi';
  4. describe('useApi', function () {
  5. it('provides an api client', function () {
  6. const {result} = reactHooks.renderHook(() => useApi());
  7. expect(result.current).toBeInstanceOf(Client);
  8. });
  9. it('cancels pending API requests when unmounted', function () {
  10. const {result, unmount} = reactHooks.renderHook(() => useApi());
  11. jest.spyOn(result.current, 'clear');
  12. unmount();
  13. expect(result.current.clear).toHaveBeenCalled();
  14. });
  15. it('does not cancel inflights when persistInFlight is true', function () {
  16. const {result, unmount} = reactHooks.renderHook(() =>
  17. useApi({persistInFlight: true})
  18. );
  19. jest.spyOn(result.current, 'clear');
  20. unmount();
  21. expect(result.current.clear).not.toHaveBeenCalled();
  22. });
  23. it('uses pass through API when provided', function () {
  24. const myClient = new Client();
  25. const {unmount} = reactHooks.renderHook(() => useApi({api: myClient}));
  26. jest.spyOn(myClient, 'clear');
  27. unmount();
  28. expect(myClient.clear).toHaveBeenCalled();
  29. });
  30. });