From c8fa7e477a43494b52670615e5cf08000a0bab0d Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 28 May 2024 11:35:06 +0200 Subject: [PATCH] chore: allow CR selection when no envs are enabled (#7183) This PR allows you to configure change requests for all environments when no environments are enabled explicitly. This is the default state of the form and makes it so that you can configure CRs even if you want all envs enabled. Additionally, it preserves the case where you configure CRs for an environment and then disable all envs. This is logic only. It's not available in the UI yet. --- .../Project/hooks/useProjectForm.test.ts | 79 +++++++++++++------ .../project/Project/hooks/useProjectForm.ts | 20 +++-- 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/frontend/src/component/project/Project/hooks/useProjectForm.test.ts b/frontend/src/component/project/Project/hooks/useProjectForm.test.ts index fd0a0cf1b8..da89e40cf3 100644 --- a/frontend/src/component/project/Project/hooks/useProjectForm.test.ts +++ b/frontend/src/component/project/Project/hooks/useProjectForm.test.ts @@ -1,39 +1,70 @@ import { renderHook } from '@testing-library/react-hooks'; import useProjectForm from './useProjectForm'; -test('setting project environments removes any change request envs that are not in the new project env list', () => { - const { result } = renderHook(() => useProjectForm()); +describe('configuring change requests', () => { + test('setting project environments removes any change request envs that are not in the new project env list', () => { + const { result } = renderHook(() => useProjectForm()); - result.current.setProjectEnvironments(new Set(['dev', 'prod'])); - result.current.updateProjectChangeRequestConfig.enableChangeRequests( - 'prod', - 5, - ); + result.current.setProjectEnvironments(new Set(['dev', 'prod'])); + result.current.updateProjectChangeRequestConfig.enableChangeRequests( + 'prod', + 5, + ); - expect(result.current.projectChangeRequestConfiguration).toMatchObject({ - prod: { requiredApprovals: 5 }, + expect(result.current.projectChangeRequestConfiguration).toMatchObject({ + prod: { requiredApprovals: 5 }, + }); + + result.current.setProjectEnvironments(new Set(['dev'])); + + expect( + 'prod' in result.current.projectChangeRequestConfiguration, + ).toBeFalsy(); }); - result.current.setProjectEnvironments(new Set(['dev'])); + test('setting project environments to an empty set preserves change request configuration', () => { + const { result } = renderHook(() => useProjectForm()); - expect( - 'prod' in result.current.projectChangeRequestConfiguration, - ).toBeFalsy(); -}); + result.current.setProjectEnvironments(new Set(['dev', 'prod'])); + result.current.updateProjectChangeRequestConfig.enableChangeRequests( + 'prod', + 5, + ); -test(`adding a change request config for an env not in the project envs doesn't work and the change request envs is not changed`, () => { - const { result } = renderHook(() => useProjectForm()); + result.current.setProjectEnvironments(new Set([])); - result.current.setProjectEnvironments(new Set(['prod'])); + expect(result.current.projectChangeRequestConfiguration).toMatchObject({ + prod: { requiredApprovals: 5 }, + }); + }); - result.current.updateProjectChangeRequestConfig.enableChangeRequests( - 'dev', - 5, - ); + test(`if specific project envs are selected, adding a change request config for an env not in the project envs doesn't work and the change request envs is not changed`, () => { + const { result } = renderHook(() => useProjectForm()); - expect( - 'dev' in result.current.projectChangeRequestConfiguration, - ).toBeFalsy(); + result.current.setProjectEnvironments(new Set(['prod'])); + + result.current.updateProjectChangeRequestConfig.enableChangeRequests( + 'dev', + 5, + ); + + expect( + 'dev' in result.current.projectChangeRequestConfiguration, + ).toBeFalsy(); + }); + + test(`if no project envs are selected, you can add a change request for any env you want`, () => { + const { result } = renderHook(() => useProjectForm()); + + result.current.updateProjectChangeRequestConfig.enableChangeRequests( + 'dev', + 5, + ); + + expect( + 'dev' in result.current.projectChangeRequestConfiguration, + ).toBeTruthy(); + }); }); describe('payload generation', () => { diff --git a/frontend/src/component/project/Project/hooks/useProjectForm.ts b/frontend/src/component/project/Project/hooks/useProjectForm.ts index 03943bf601..4cafc7879d 100644 --- a/frontend/src/component/project/Project/hooks/useProjectForm.ts +++ b/frontend/src/component/project/Project/hooks/useProjectForm.ts @@ -38,13 +38,16 @@ const useProjectForm = ( ] = useState(initialProjectChangeRequestConfiguration); const updateProjectEnvironments = (newState: Set) => { - const filteredChangeRequestEnvs = Object.fromEntries( - Object.entries(projectChangeRequestConfiguration).filter(([env]) => - newState.has(env), - ), - ); + if (newState.size !== 0) { + const filteredChangeRequestEnvs = Object.fromEntries( + Object.entries(projectChangeRequestConfiguration).filter( + ([env]) => newState.has(env), + ), + ); + + setProjectChangeRequestConfiguration(filteredChangeRequestEnvs); + } - setProjectChangeRequestConfiguration(filteredChangeRequestEnvs); setProjectEnvironments(newState); }; @@ -57,7 +60,10 @@ const useProjectForm = ( }, enableChangeRequests: (env: string, approvals: number) => { - if (projectEnvironments.has(env)) { + if ( + projectEnvironments.has(env) || + projectEnvironments.size === 0 + ) { setProjectChangeRequestConfiguration((previousState) => ({ ...previousState, [env]: { requiredApprovals: approvals },