navigation.spec.tsx 4.0 KB

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