navigation.spec.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {openModal} from 'sentry/actionCreators/modal';
  3. import {navigateTo} from 'sentry/actionCreators/navigation';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  7. import type {Config} from 'sentry/types/system';
  8. jest.mock('sentry/actionCreators/modal');
  9. describe('navigation ActionCreator', () => {
  10. let router: InjectedRouter;
  11. let configState: Config;
  12. beforeEach(() => {
  13. ProjectsStore.init();
  14. const initialData = initializeOrg({
  15. router: {
  16. location: {query: {}, search: ''},
  17. push: jest.fn(),
  18. },
  19. });
  20. router = initialData.router;
  21. ProjectsStore.loadInitialData(initialData.projects);
  22. configState = ConfigStore.getState();
  23. });
  24. afterEach(() => {
  25. ProjectsStore.reset();
  26. jest.resetAllMocks();
  27. ConfigStore.loadInitialData(configState);
  28. });
  29. it('should get project from query parameters', () => {
  30. router.location.query.project = '2';
  31. navigateTo('/settings/:projectId/alert', router);
  32. expect(openModal).not.toHaveBeenCalled();
  33. expect(router.push).toHaveBeenCalledWith('/settings/project-slug/alert');
  34. });
  35. it('should get project id from query parameters', () => {
  36. router.location.query.project = '2';
  37. navigateTo(
  38. '/organizations/albertos-apples/performance/?project=:project#performance-sidequest',
  39. router
  40. );
  41. expect(openModal).not.toHaveBeenCalled();
  42. expect(router.push).toHaveBeenCalledWith(
  43. '/organizations/albertos-apples/performance/?project=2#performance-sidequest'
  44. );
  45. });
  46. it('should get project and project id from query parameters', () => {
  47. router.location.query.project = '2';
  48. navigateTo(
  49. '/settings/:projectId/alert?project=:project#performance-sidequest',
  50. router
  51. );
  52. expect(openModal).not.toHaveBeenCalled();
  53. expect(router.push).toHaveBeenCalledWith(
  54. '/settings/project-slug/alert?project=2#performance-sidequest'
  55. );
  56. });
  57. it('should open modal if the store is somehow missing selected projectId', () => {
  58. router.location.query.project = '911';
  59. navigateTo('/settings/:projectId/alert', router);
  60. expect(openModal).toHaveBeenCalled();
  61. });
  62. it('should open modal when no project is selected', () => {
  63. navigateTo('/settings/:projectId/alert', router);
  64. expect(openModal).toHaveBeenCalled();
  65. });
  66. it('should open modal when no project id is selected', () => {
  67. navigateTo(
  68. '/organizations/albertos-apples/performance/?project=:project#performance-sidequest',
  69. router
  70. );
  71. expect(openModal).toHaveBeenCalled();
  72. });
  73. it('should open modal if more than one project is selected', () => {
  74. router.location.query.project = ['1', '2', '3'];
  75. navigateTo('/settings/:projectId/alert', router);
  76. expect(openModal).toHaveBeenCalled();
  77. });
  78. it('should not open modal if url does not require project id', () => {
  79. navigateTo('/settings/alert', router);
  80. expect(openModal).not.toHaveBeenCalled();
  81. expect(router.push).toHaveBeenCalledWith('/settings/alert');
  82. });
  83. it('should open modal for orgId', () => {
  84. navigateTo('/settings/:orgId', router);
  85. expect(openModal).toHaveBeenCalled();
  86. });
  87. it('normalizes URLs for customer domains', function () {
  88. ConfigStore.set('customerDomain', {
  89. subdomain: 'albertos-apples',
  90. organizationUrl: 'https://albertos-apples.sentry.io',
  91. sentryUrl: 'https://sentry.io',
  92. });
  93. navigateTo('/settings/org-slug/projects/', router);
  94. expect(openModal).not.toHaveBeenCalled();
  95. expect(router.push).toHaveBeenCalledWith('/settings/projects/');
  96. router.location.query.project = '2';
  97. navigateTo('/settings/org-slug/projects/:projectId/alerts/', router);
  98. expect(openModal).not.toHaveBeenCalled();
  99. expect(router.push).toHaveBeenCalledWith('/settings/projects/project-slug/alerts/');
  100. });
  101. it('preserves query parameters in path object', function () {
  102. router.location.query.project = '2';
  103. navigateTo(
  104. {
  105. pathname: '/settings/:projectId/alert?project=:project#performance-sidequest',
  106. query: {referrer: 'onboarding_task'},
  107. },
  108. router
  109. );
  110. expect(openModal).not.toHaveBeenCalled();
  111. expect(router.push).toHaveBeenCalledWith({
  112. pathname: '/settings/project-slug/alert?project=2#performance-sidequest',
  113. query: {referrer: 'onboarding_task'},
  114. });
  115. });
  116. });