1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

feat: Change request preview integration (#7743)

Use API to check change request evaluation
This commit is contained in:
Tymoteusz Czech 2024-08-02 14:35:48 +02:00 committed by GitHub
parent 57a8b9da79
commit 43c8152515
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 24 deletions

View File

@ -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(() => {
@ -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();

View File

@ -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'];
@ -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);

View File

@ -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={

View File

@ -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,
};