withApi.spec.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import {render} from 'sentry-test/reactTestingLibrary';
  2. import {Client} from 'sentry/api';
  3. import withApi from 'sentry/utils/withApi';
  4. describe('withApi', function () {
  5. let apiInstance: Client | undefined;
  6. type Props = {
  7. /**
  8. * Test passthrough API clients
  9. */
  10. api?: Client;
  11. };
  12. const MyComponent = jest.fn((props: Props) => {
  13. apiInstance = props.api;
  14. return <div />;
  15. });
  16. it('renders MyComponent with an api prop', function () {
  17. const MyComponentWithApi = withApi(MyComponent);
  18. render(<MyComponentWithApi />);
  19. expect(MyComponent).toHaveBeenCalledWith(
  20. expect.objectContaining({api: apiInstance}),
  21. expect.anything()
  22. );
  23. });
  24. it('cancels pending API requests when component is unmounted', function () {
  25. const MyComponentWithApi = withApi(MyComponent);
  26. const wrapper = render(<MyComponentWithApi />);
  27. if (apiInstance === undefined) {
  28. throw new Error("apiInstance wasn't defined");
  29. }
  30. jest.spyOn(apiInstance, 'clear');
  31. expect(apiInstance?.clear).not.toHaveBeenCalled();
  32. wrapper.unmount();
  33. expect(apiInstance?.clear).toHaveBeenCalled();
  34. });
  35. });