useUrlParams.spec.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {browserHistory} from 'react-router';
  2. import type {Location} from 'history';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import useUrlParams from './useUrlParams';
  6. jest.mock('react-router');
  7. jest.mock('sentry/utils/useLocation');
  8. const mockUseLocation = useLocation as jest.MockedFunction<typeof useLocation>;
  9. const mockBrowserHistoryPush = browserHistory.push as jest.MockedFunction<
  10. typeof browserHistory.push
  11. >;
  12. type Query = {limit: string; page: string};
  13. describe('useUrlParams', () => {
  14. beforeEach(() => {
  15. mockBrowserHistoryPush.mockReset();
  16. mockUseLocation.mockReturnValue({
  17. query: {
  18. page: '3',
  19. limit: '50',
  20. },
  21. } as Location<Query>);
  22. });
  23. it('should read query values from the url', () => {
  24. const {result} = reactHooks.renderHook(useUrlParams);
  25. expect(result.current.getParamValue('page')).toBe('3');
  26. expect(result.current.getParamValue('limit')).toBe('50');
  27. });
  28. it('should read a specific query value if the defaultKey is passed along', () => {
  29. const {result} = reactHooks.renderHook((args: [string]) => useUrlParams(args[0]), {
  30. initialProps: ['page'],
  31. });
  32. expect(result.current.getParamValue()).toBe('3');
  33. });
  34. it('should read the default value for the defaultKey', () => {
  35. const {result} = reactHooks.renderHook(
  36. (args: [string, string]) => useUrlParams(args[0], args[1]),
  37. {
  38. initialProps: ['foo', 'bar'],
  39. }
  40. ); // Prefer TS function overloading, not initialProps
  41. expect(result.current.getParamValue()).toBe('bar');
  42. });
  43. it('should update browser history with new values', () => {
  44. const {result} = reactHooks.renderHook(useUrlParams);
  45. result.current.setParamValue('page', '4');
  46. expect(browserHistory.push).toHaveBeenCalledWith({
  47. query: {
  48. page: '4',
  49. limit: '50',
  50. },
  51. });
  52. });
  53. it('should update browser history with new values for the defaultKey', () => {
  54. const {result} = reactHooks.renderHook((args: [string]) => useUrlParams(args[0]), {
  55. initialProps: ['page'],
  56. });
  57. result.current.setParamValue('4');
  58. expect(browserHistory.push).toHaveBeenCalledWith({
  59. query: {
  60. page: '4',
  61. limit: '50',
  62. },
  63. });
  64. });
  65. it('uses the same function reference after each render', () => {
  66. const {result, rerender} = reactHooks.renderHook(useUrlParams);
  67. const firstResult = result.current;
  68. rerender();
  69. const secondResult = result.current;
  70. expect(firstResult.getParamValue).toBe(secondResult.getParamValue);
  71. expect(firstResult.setParamValue).toBe(secondResult.setParamValue);
  72. });
  73. });