navigation.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import type {InjectedRouter} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {openModal} from 'sentry/actionCreators/modal';
  4. import {navigateTo} from 'sentry/actionCreators/navigation';
  5. import ConfigStore from 'sentry/stores/configStore';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  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. expect(navigateTo('/settings/:projectId/alert', router)).toBe(undefined);
  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. expect(
  38. navigateTo(
  39. '/organizations/albertos-apples/performance/?project=:project#performance-sidequest',
  40. router
  41. )
  42. ).toBe(undefined);
  43. expect(openModal).not.toHaveBeenCalled();
  44. expect(router.push).toHaveBeenCalledWith(
  45. '/organizations/albertos-apples/performance/?project=2#performance-sidequest'
  46. );
  47. });
  48. it('should get project and project id from query parameters', () => {
  49. router.location.query.project = '2';
  50. expect(
  51. navigateTo(
  52. '/settings/:projectId/alert?project=:project#performance-sidequest',
  53. router
  54. )
  55. ).toBe(undefined);
  56. expect(openModal).not.toHaveBeenCalled();
  57. expect(router.push).toHaveBeenCalledWith(
  58. '/settings/project-slug/alert?project=2#performance-sidequest'
  59. );
  60. });
  61. it('should open modal if the store is somehow missing selected projectId', () => {
  62. router.location.query.project = '911';
  63. expect(navigateTo('/settings/:projectId/alert', router)).toBe(undefined);
  64. expect(openModal).toHaveBeenCalled();
  65. });
  66. it('should open modal when no project is selected', () => {
  67. expect(navigateTo('/settings/:projectId/alert', router)).toBe(undefined);
  68. expect(openModal).toHaveBeenCalled();
  69. });
  70. it('should open modal when no project id is selected', () => {
  71. expect(
  72. navigateTo(
  73. '/organizations/albertos-apples/performance/?project=:project#performance-sidequest',
  74. router
  75. )
  76. ).toBe(undefined);
  77. expect(openModal).toHaveBeenCalled();
  78. });
  79. it('should open modal if more than one project is selected', () => {
  80. router.location.query.project = ['1', '2', '3'];
  81. expect(navigateTo('/settings/:projectId/alert', router)).toBe(undefined);
  82. expect(openModal).toHaveBeenCalled();
  83. });
  84. it('should not open modal if url does not require project id', () => {
  85. expect(navigateTo('/settings/alert', router)).toBe(undefined);
  86. expect(openModal).not.toHaveBeenCalled();
  87. expect(router.push).toHaveBeenCalledWith('/settings/alert');
  88. });
  89. it('should open modal for orgId', () => {
  90. expect(navigateTo('/settings/:orgId', router)).toBe(undefined);
  91. expect(openModal).toHaveBeenCalled();
  92. });
  93. it('normalizes URLs for customer domains', function () {
  94. ConfigStore.set('customerDomain', {
  95. subdomain: 'albertos-apples',
  96. organizationUrl: 'https://albertos-apples.sentry.io',
  97. sentryUrl: 'https://sentry.io',
  98. });
  99. navigateTo('/settings/org-slug/projects/', router);
  100. expect(openModal).not.toHaveBeenCalled();
  101. expect(router.push).toHaveBeenCalledWith('/settings/projects/');
  102. router.location.query.project = '2';
  103. navigateTo('/settings/org-slug/projects/:projectId/alerts/', router);
  104. expect(openModal).not.toHaveBeenCalled();
  105. expect(router.push).toHaveBeenCalledWith('/settings/projects/project-slug/alerts/');
  106. });
  107. });