From 6477ccf34b0c01680f14cf3355195c415b74135a Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Wed, 8 May 2024 07:26:49 +0200 Subject: [PATCH] test: test how the project form deals with project envs and cr env interaction (#6997) This PR adds some tests around how project envs and change request envs interact in the new project form. It tests that: 1. If you remove an env from the project setup, that env is also removed from the change request list. 2. If you try to enable CRs for an env that isn't enabled, nothing happens. --- .../Project/hooks/useProjectForm.test.ts | 37 +++++++++++++++++++ .../project/Project/hooks/useProjectForm.ts | 12 +++--- 2 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 frontend/src/component/project/Project/hooks/useProjectForm.test.ts diff --git a/frontend/src/component/project/Project/hooks/useProjectForm.test.ts b/frontend/src/component/project/Project/hooks/useProjectForm.test.ts new file mode 100644 index 0000000000..88ec54b706 --- /dev/null +++ b/frontend/src/component/project/Project/hooks/useProjectForm.test.ts @@ -0,0 +1,37 @@ +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()); + + result.current.setProjectEnvironments(new Set(['dev', 'prod'])); + result.current.updateProjectChangeRequestConfig.enableChangeRequests( + 'prod', + 5, + ); + + expect(result.current.projectChangeRequestConfiguration).toMatchObject({ + prod: { requiredApprovals: 5 }, + }); + + result.current.setProjectEnvironments(new Set(['dev'])); + + expect( + 'prod' in result.current.projectChangeRequestConfiguration, + ).toBeFalsy(); +}); + +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(['prod'])); + + result.current.updateProjectChangeRequestConfig.enableChangeRequests( + 'dev', + 5, + ); + + expect( + 'dev' in result.current.projectChangeRequestConfiguration, + ).toBeFalsy(); +}); diff --git a/frontend/src/component/project/Project/hooks/useProjectForm.ts b/frontend/src/component/project/Project/hooks/useProjectForm.ts index 0c890c6c60..57495412ba 100644 --- a/frontend/src/component/project/Project/hooks/useProjectForm.ts +++ b/frontend/src/component/project/Project/hooks/useProjectForm.ts @@ -37,8 +37,6 @@ const useProjectForm = ( setProjectChangeRequestConfiguration, ] = useState(initialProjectChangeRequestConfiguration); - // todo: write tests for this - // also: disallow adding a project to cr config that isn't in envs const updateProjectEnvironments = (newState: Set) => { const filteredChangeRequestEnvs = Object.fromEntries( Object.entries(projectChangeRequestConfiguration).filter(([env]) => @@ -59,10 +57,12 @@ const useProjectForm = ( }, enableChangeRequests: (env: string, approvals: number) => { - setProjectChangeRequestConfiguration((previousState) => ({ - ...previousState, - [env]: { requiredApprovals: approvals }, - })); + if (projectEnvironments.has(env)) { + setProjectChangeRequestConfiguration((previousState) => ({ + ...previousState, + [env]: { requiredApprovals: approvals }, + })); + } }, };