Skip to content

Commit

Permalink
feat: Change request preview integration (#7743)
Browse files Browse the repository at this point in the history
Use API to check change request evaluation
  • Loading branch information
Tymek authored Aug 2, 2024
1 parent 57a8b9d commit 43c8152
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
32 changes: 22 additions & 10 deletions frontend/src/component/playground/Playground/AdvancedPlayground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ export const AdvancedPlayground: FC<{
const { setToastData } = useToast();
const [searchParams, setSearchParams] = useSearchParams();
const [changeRequest, setChangeRequest] = useState<string>();
const { evaluateAdvancedPlayground, loading, errors } = usePlaygroundApi();
const {
evaluateAdvancedPlayground,
evaluateChangeRequestPlayground,
loading,
errors,
} = usePlaygroundApi();
const [hasFormBeenSubmitted, setHasFormBeenSubmitted] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ test('should show change request and disable other fields until removed', async
const [environments, setEnvironments] = useState<string[]>([]);
const [projects, setProjects] = useState<string[]>(['test-project']);
const [token, setToken] = useState<string>();
const [changeRequest, setChangeRequest] = useState('CR #1');
const [changeRequest, setChangeRequest] = useState('1');

const availableEnvironments = ['development', 'production'];

Expand All @@ -233,14 +233,15 @@ test('should show change request and disable other fields until removed', async
};
render(<Component />);

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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,17 @@ export const PlaygroundConnectionFieldset: FC<
<Box sx={{ flex: 1 }}>
<StyledChangeRequestInput
label='Change request'
value={changeRequest || ''}
value={
changeRequest
? `Change request #${changeRequest}`
: ''
}
onChange={() => {}}
type={'text'}
// error={Boolean(changeRequestError)}
// errorText={changeRequestError)}}
placeholder={'Enter your API token'}
data-testid={'PLAYGROUND_TOKEN_INPUT'}
disabled
InputProps={{
endAdornment: (
<InputAdornment
position='end'
data-testid='CR_INPUT_CLEAR_BTN'
>
<InputAdornment position='end'>
<IconButton
aria-label='clear Change request results'
onClick={
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/hooks/api/actions/usePlayground/usePlayground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,23 @@ export const usePlaygroundApi = () => {
return res.json();
};

const evaluateChangeRequestPlayground = async (
changeRequestId: string,
payload: AdvancedPlaygroundRequestSchema, // FIXME: type
): Promise<AdvancedPlaygroundResponseSchema> => {
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,
};
Expand Down

0 comments on commit 43c8152

Please sign in to comment.