queryClient.spec.tsx 2.8 KB

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