queryClient.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {Fragment} from 'react';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {useApiQuery} from 'sentry/utils/queryClient';
  4. import RequestError from 'sentry/utils/requestError/requestError';
  5. import * as useApi from 'sentry/utils/useApi';
  6. type ResponseData = {
  7. value: number;
  8. };
  9. beforeEach(() => {
  10. jest.restoreAllMocks();
  11. });
  12. describe('queryClient', function () {
  13. describe('useQuery', function () {
  14. it('can do a simple fetch', async function () {
  15. const mock = MockApiClient.addMockResponse({
  16. url: '/some/test/path/',
  17. body: {value: 5},
  18. headers: {'Custom-Header': 'header value'},
  19. });
  20. function TestComponent() {
  21. const {data, getResponseHeader} = useApiQuery<ResponseData>(
  22. ['/some/test/path/'],
  23. {staleTime: 0}
  24. );
  25. if (!data) {
  26. return null;
  27. }
  28. return (
  29. <Fragment>
  30. <div>{data.value}</div>
  31. <div>{getResponseHeader?.('Custom-Header')}</div>
  32. </Fragment>
  33. );
  34. }
  35. render(<TestComponent />);
  36. expect(await screen.findByText('5')).toBeInTheDocument();
  37. expect(screen.getByText('header value')).toBeInTheDocument();
  38. expect(mock).toHaveBeenCalledWith('/some/test/path/', expect.anything());
  39. });
  40. it('can do a fetch with provided query object', async function () {
  41. const mock = MockApiClient.addMockResponse({
  42. url: '/some/test/path/',
  43. body: {value: 5},
  44. });
  45. function TestComponent() {
  46. const {data} = useApiQuery<ResponseData>(
  47. ['/some/test/path/', {query: {filter: 'red'}}],
  48. {staleTime: 0}
  49. );
  50. if (!data) {
  51. return null;
  52. }
  53. return <div>{data.value}</div>;
  54. }
  55. render(<TestComponent />);
  56. expect(await screen.findByText('5')).toBeInTheDocument();
  57. expect(mock).toHaveBeenCalledWith(
  58. '/some/test/path/',
  59. expect.objectContaining({query: {filter: 'red'}})
  60. );
  61. });
  62. it('can return error state', async function () {
  63. const requestError = new RequestError('GET', '/some/test/path', new Error());
  64. requestError.message = 'something bad happened';
  65. const api = new MockApiClient();
  66. jest.spyOn(useApi, 'default').mockReturnValue(api);
  67. jest.spyOn(api, 'requestPromise').mockRejectedValue(requestError);
  68. function TestComponent() {
  69. const {isError, error} = useApiQuery<ResponseData>(['/some/test/path'], {
  70. staleTime: 0,
  71. });
  72. if (!isError) {
  73. return null;
  74. }
  75. return <div>{error.message}</div>;
  76. }
  77. render(<TestComponent />);
  78. expect(await screen.findByText('something bad happened')).toBeInTheDocument();
  79. });
  80. });
  81. });