useCleanQueryParamsOnRouteLeave.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {browserHistory} from 'react-router';
  2. import type {Location} from 'history';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import useCleanQueryParamsOnRouteLeave, {
  5. handleRouteLeave,
  6. } from './useCleanQueryParamsOnRouteLeave';
  7. import {useLocation} from './useLocation';
  8. jest.mock('react-router');
  9. jest.mock('./useLocation');
  10. const MockBrowserHistoryListen = browserHistory.listen as jest.MockedFunction<
  11. typeof browserHistory.listen
  12. >;
  13. const MockBrowserHistoryReplace = browserHistory.replace as jest.MockedFunction<
  14. typeof browserHistory.replace
  15. >;
  16. const MockUseLocation = useLocation as jest.MockedFunction<typeof useLocation>;
  17. MockUseLocation.mockReturnValue({
  18. pathname: '/home',
  19. } as Location);
  20. type QueryParams = {cursor: string; limit: number; project: string};
  21. describe('useCleanQueryParamsOnRouteLeave', () => {
  22. beforeEach(() => {
  23. MockBrowserHistoryListen.mockReset();
  24. MockBrowserHistoryReplace.mockReset();
  25. });
  26. it('should listen to browserHistory changes and stop on unmount', () => {
  27. const unsubscriber = jest.fn();
  28. MockBrowserHistoryListen.mockReturnValue(unsubscriber);
  29. const {unmount} = reactHooks.renderHook(useCleanQueryParamsOnRouteLeave, {
  30. initialProps: {
  31. fieldsToClean: ['cursor'],
  32. },
  33. });
  34. expect(MockBrowserHistoryListen).toHaveBeenCalled();
  35. expect(unsubscriber).not.toHaveBeenCalled();
  36. unmount();
  37. expect(unsubscriber).toHaveBeenCalled();
  38. });
  39. it('should not update the history if the pathname is unchanged', () => {
  40. handleRouteLeave({
  41. fieldsToClean: ['cursor'],
  42. newLocation: {
  43. pathname: '/home',
  44. query: {},
  45. } as Location,
  46. oldPathname: '/home',
  47. });
  48. expect(MockBrowserHistoryReplace).not.toHaveBeenCalled();
  49. });
  50. it('should not update the history if the pathname is changing, but fieldsToClean are undefined', () => {
  51. handleRouteLeave({
  52. fieldsToClean: ['cursor'],
  53. newLocation: {
  54. pathname: '/next',
  55. query: {},
  56. } as Location,
  57. oldPathname: '/home',
  58. });
  59. expect(MockBrowserHistoryReplace).not.toHaveBeenCalled();
  60. });
  61. it('should update the history when the path is changing and some fieldsToClean are set', () => {
  62. handleRouteLeave({
  63. fieldsToClean: ['cursor', 'limit'],
  64. newLocation: {
  65. pathname: '/next',
  66. query: {
  67. cursor: '0:1:0',
  68. limit: 5,
  69. },
  70. } as Location<QueryParams>,
  71. oldPathname: '/home',
  72. });
  73. expect(MockBrowserHistoryReplace).toHaveBeenCalledWith({
  74. pathname: '/next',
  75. query: {},
  76. });
  77. });
  78. it('should leave other query params alone when the path is changing and something is filtered out', () => {
  79. handleRouteLeave({
  80. fieldsToClean: ['cursor', 'limit'],
  81. newLocation: {
  82. pathname: '/next',
  83. query: {
  84. cursor: '0:1:0',
  85. limit: 5,
  86. project: '123',
  87. },
  88. } as Location<QueryParams>,
  89. oldPathname: '/home',
  90. });
  91. expect(MockBrowserHistoryReplace).toHaveBeenCalledWith({
  92. pathname: '/next',
  93. query: {
  94. project: '123',
  95. },
  96. });
  97. });
  98. });