From 43c8152515c9a84b70b861dd8268028d52922caf Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Fri, 2 Aug 2024 14:35:48 +0200 Subject: [PATCH] feat: Change request preview integration (#7743) Use API to check change request evaluation --- .../Playground/AdvancedPlayground.tsx | 32 +++++++++++++------ .../PlaygroundConnectionFieldset.test.tsx | 11 ++++--- .../PlaygroundConnectionFieldset.tsx | 15 ++++----- .../actions/usePlayground/usePlayground.ts | 15 +++++++++ 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/frontend/src/component/playground/Playground/AdvancedPlayground.tsx b/frontend/src/component/playground/Playground/AdvancedPlayground.tsx index b46231015e..573a02aa49 100644 --- a/frontend/src/component/playground/Playground/AdvancedPlayground.tsx +++ b/frontend/src/component/playground/Playground/AdvancedPlayground.tsx @@ -113,7 +113,12 @@ export const AdvancedPlayground: FC<{ const { setToastData } = useToast(); const [searchParams, setSearchParams] = useSearchParams(); const [changeRequest, setChangeRequest] = useState(); - const { evaluateAdvancedPlayground, loading, errors } = usePlaygroundApi(); + const { + evaluateAdvancedPlayground, + evaluateChangeRequestPlayground, + loading, + errors, + } = usePlaygroundApi(); const [hasFormBeenSubmitted, setHasFormBeenSubmitted] = useState(false); useEffect(() => { @@ -203,15 +208,22 @@ export const AdvancedPlayground: FC<{ ) => { try { setConfigurationError(undefined); - const parsedContext = JSON.parse(context || '{}'); - const response = await evaluateAdvancedPlayground({ - environments: resolveEnvironments(environments), - projects: resolveProjects(projects), - context: { - appName: 'playground', - ...parsedContext, - }, - }); + const parsedContext = { + appName: 'playground', + ...JSON.parse(context || '{}'), + }; + + const response = changeRequest + ? await evaluateChangeRequestPlayground(changeRequest, { + context: parsedContext, + projects: [], + environments: [], + }) + : await evaluateAdvancedPlayground({ + environments: resolveEnvironments(environments), + projects: resolveProjects(projects), + context: parsedContext, + }); if (action && typeof action === 'function') { action(); diff --git a/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.test.tsx b/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.test.tsx index b673edecca..b7b7c16ec7 100644 --- a/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.test.tsx +++ b/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.test.tsx @@ -213,7 +213,7 @@ test('should show change request and disable other fields until removed', async const [environments, setEnvironments] = useState([]); const [projects, setProjects] = useState(['test-project']); const [token, setToken] = useState(); - const [changeRequest, setChangeRequest] = useState('CR #1'); + const [changeRequest, setChangeRequest] = useState('1'); const availableEnvironments = ['development', 'production']; @@ -233,14 +233,15 @@ test('should show change request and disable other fields until removed', async }; render(); - const changeRequestInput = await screen.findByDisplayValue('CR #1'); - // expect(changeRequestInput).toHaveValue('CR #1'); + const changeRequestInput = + await screen.findByDisplayValue('Change request #1'); const viewButton = await screen.findByText(/View change request/); expect(viewButton).toHaveProperty( 'href', - 'http://localhost:3000/projects/test-project/change-requests/CR%20#1', + 'http://localhost:3000/projects/test-project/change-requests/1', ); - // TODO: check if other fields are disabled + const tokenInput = await screen.findByLabelText('API token'); + expect(tokenInput).toBeDisabled(); const clearButton = await screen.findByLabelText(/clear change request/i); diff --git a/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.tsx b/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.tsx index 6520d587c4..1ae3008cff 100644 --- a/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.tsx +++ b/frontend/src/component/playground/Playground/PlaygroundForm/PlaygroundConnectionFieldset/PlaygroundConnectionFieldset.tsx @@ -277,20 +277,17 @@ export const PlaygroundConnectionFieldset: FC< {}} type={'text'} - // error={Boolean(changeRequestError)} - // errorText={changeRequestError)}} - placeholder={'Enter your API token'} - data-testid={'PLAYGROUND_TOKEN_INPUT'} disabled InputProps={{ endAdornment: ( - + { return res.json(); }; + const evaluateChangeRequestPlayground = async ( + changeRequestId: string, + payload: AdvancedPlaygroundRequestSchema, // FIXME: type + ): Promise => { + const path = `${URI}/change-request/${changeRequestId}`; + const req = createRequest(path, { + method: 'POST', + body: JSON.stringify(payload), + }); + + const res = await makeRequest(req.caller, req.id); + return res.json(); + }; + return { evaluateAdvancedPlayground, + evaluateChangeRequestPlayground, errors, loading, };