useApi.spec.tsx 1.1 KB

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