Browse Source

fix(workflow): Open project modal when multiple are selected (#25565)

Scott Cooper 3 years ago
parent
commit
6acdaddc89

+ 2 - 2
static/app/actionCreators/navigation.tsx

@@ -13,11 +13,11 @@ export function navigateTo(to: string, router: InjectedRouter & {location?: Loca
   const needOrg = to.indexOf(':orgId') > -1;
   const needProject = to.indexOf(':projectId') > -1;
   const comingFromProjectId = router?.location?.query?.project;
-  const needProjectId = !comingFromProjectId;
+  const needProjectId = !comingFromProjectId || Array.isArray(comingFromProjectId);
 
   const projectById = ProjectsStore.getById(comingFromProjectId);
 
-  if (needOrg || (needProject && needProjectId)) {
+  if (needOrg || (needProject && (needProjectId || !projectById))) {
     openModal(
       modalProps => (
         <ContextPickerModal

+ 61 - 0
tests/js/spec/actionCreators/navigation.spec.jsx

@@ -0,0 +1,61 @@
+import {initializeOrg} from 'sentry-test/initializeOrg';
+
+import {openModal} from 'app/actionCreators/modal';
+import {navigateTo} from 'app/actionCreators/navigation';
+import ProjectsStore from 'app/stores/projectsStore';
+
+jest.mock('app/actionCreators/modal');
+
+describe('navigation ActionCreator', () => {
+  let router;
+  beforeEach(() => {
+    const initialData = initializeOrg({
+      router: {
+        location: {query: {}, search: ''},
+        push: jest.fn(),
+      },
+    });
+    router = initialData.router;
+    ProjectsStore.loadInitialData(initialData.organization.projects);
+  });
+
+  afterEach(() => {
+    ProjectsStore.reset();
+    jest.resetAllMocks();
+  });
+
+  it('should get project from query parameters', () => {
+    router.location.query.project = '2';
+    expect(navigateTo('/settings/:projectId/alert', router)).toBe();
+    expect(openModal).not.toHaveBeenCalled();
+    expect(router.push).toHaveBeenCalledWith('/settings/project-slug/alert');
+  });
+
+  it('should open modal if the store is somehow missing selected projectId', () => {
+    router.location.query.project = '911';
+    expect(navigateTo('/settings/:projectId/alert', router)).toBe();
+    expect(openModal).toHaveBeenCalled();
+  });
+
+  it('should open modal when no project is selected', () => {
+    expect(navigateTo('/settings/:projectId/alert', router)).toBe();
+    expect(openModal).toHaveBeenCalled();
+  });
+
+  it('should open modal if more than one project is selected', () => {
+    router.location.query.project = ['1', '2', '3'];
+    expect(navigateTo('/settings/:projectId/alert', router)).toBe();
+    expect(openModal).toHaveBeenCalled();
+  });
+
+  it('should not open modal if url does not require project id', () => {
+    expect(navigateTo('/settings/alert', router)).toBe();
+    expect(openModal).not.toHaveBeenCalled();
+    expect(router.push).toHaveBeenCalledWith('/settings/alert');
+  });
+
+  it('should open modal for orgId', () => {
+    expect(navigateTo('/settings/:orgId', router)).toBe();
+    expect(openModal).toHaveBeenCalled();
+  });
+});