feat: use REST API endpoint to test step

This commit is contained in:
Ali BARIN
2024-09-19 09:08:44 +00:00
parent c8dae9ea9a
commit 84d5b3b158
2 changed files with 38 additions and 43 deletions

View File

@@ -1,6 +1,5 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import * as React from 'react'; import * as React from 'react';
import { useMutation } from '@apollo/client';
import Box from '@mui/material/Box'; import Box from '@mui/material/Box';
import Collapse from '@mui/material/Collapse'; import Collapse from '@mui/material/Collapse';
import ListItem from '@mui/material/ListItem'; import ListItem from '@mui/material/ListItem';
@@ -10,37 +9,19 @@ import LoadingButton from '@mui/lab/LoadingButton';
import { EditorContext } from 'contexts/Editor'; import { EditorContext } from 'contexts/Editor';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { EXECUTE_FLOW } from 'graphql/mutations/execute-flow'; import useTestStep from 'hooks/useTestStep';
import JSONViewer from 'components/JSONViewer'; import JSONViewer from 'components/JSONViewer';
import WebhookUrlInfo from 'components/WebhookUrlInfo'; import WebhookUrlInfo from 'components/WebhookUrlInfo';
import FlowSubstepTitle from 'components/FlowSubstepTitle'; import FlowSubstepTitle from 'components/FlowSubstepTitle';
import { useQueryClient } from '@tanstack/react-query'; import { useQueryClient } from '@tanstack/react-query';
import { StepPropType, SubstepPropType } from 'propTypes/propTypes'; import { StepPropType, SubstepPropType } from 'propTypes/propTypes';
function serializeErrors(graphQLErrors) {
return graphQLErrors?.map((error) => {
try {
return {
...error,
message: (
<pre style={{ margin: 0, whiteSpace: 'pre-wrap' }}>
{JSON.stringify(JSON.parse(error.message), null, 2)}
</pre>
),
};
} catch {
return error;
}
});
}
function TestSubstep(props) { function TestSubstep(props) {
const { const {
substep, substep,
expanded = false, expanded = false,
onExpand, onExpand,
onCollapse, onCollapse,
onSubmit,
onContinue, onContinue,
step, step,
showWebhookUrl = false, showWebhookUrl = false,
@@ -48,15 +29,21 @@ function TestSubstep(props) {
} = props; } = props;
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const editorContext = React.useContext(EditorContext); const editorContext = React.useContext(EditorContext);
const [executeFlow, { data, error, loading, called, reset }] = useMutation( const {
EXECUTE_FLOW, mutateAsync: testStep,
{ isPending: isTestStepPending,
context: { autoSnackbar: false }, data,
}, isSuccess: isCompleted,
); reset,
const response = data?.executeFlow?.data; } = useTestStep(step.id);
const isCompleted = !error && called && !loading; const loading = isTestStepPending;
const hasNoOutput = !response && isCompleted; const lastExecutionStep = data?.data.lastExecutionStep;
const dataOut = lastExecutionStep?.dataOut;
const errorDetails = lastExecutionStep?.errorDetails;
const hasError = errorDetails && Object.values(errorDetails).length > 0;
const hasNoOutput = !hasError && isCompleted && !dataOut;
const hasOutput =
!hasError && isCompleted && dataOut && Object.values(dataOut).length > 0;
const { name } = substep; const { name } = substep;
const queryClient = useQueryClient(); const queryClient = useQueryClient();
@@ -75,18 +62,12 @@ function TestSubstep(props) {
return; return;
} }
await executeFlow({ await testStep();
variables: {
input: {
stepId: step.id,
},
},
});
await queryClient.invalidateQueries({ await queryClient.invalidateQueries({
queryKey: ['flows', flowId], queryKey: ['flows', flowId],
}); });
}, [onSubmit, onContinue, isCompleted, queryClient, flowId]); }, [testStep, onContinue, isCompleted, queryClient, flowId]);
const onToggle = expanded ? onCollapse : onExpand; const onToggle = expanded ? onCollapse : onExpand;
@@ -102,14 +83,14 @@ function TestSubstep(props) {
alignItems: 'flex-start', alignItems: 'flex-start',
}} }}
> >
{!!error?.graphQLErrors?.length && ( {hasError && (
<Alert <Alert
severity="error" severity="error"
sx={{ mb: 2, fontWeight: 500, width: '100%' }} sx={{ mb: 2, fontWeight: 500, width: '100%' }}
> >
{serializeErrors(error.graphQLErrors).map((error, i) => ( <pre style={{ margin: 0, whiteSpace: 'pre-wrap' }}>
<div key={i}>{error.message}</div> {JSON.stringify(errorDetails, null, 2)}
))} </pre>
</Alert> </Alert>
)} )}
@@ -133,12 +114,12 @@ function TestSubstep(props) {
</Alert> </Alert>
)} )}
{response && ( {hasOutput && (
<Box <Box
sx={{ maxHeight: 400, overflowY: 'auto', width: '100%' }} sx={{ maxHeight: 400, overflowY: 'auto', width: '100%' }}
data-test="flow-test-substep-output" data-test="flow-test-substep-output"
> >
<JSONViewer data={response} /> <JSONViewer data={dataOut} />
</Box> </Box>
)} )}

View File

@@ -0,0 +1,14 @@
import { useMutation } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useTestStep(stepId) {
const query = useMutation({
mutationFn: async () => {
const { data } = await api.post(`/v1/steps/${stepId}/test`);
return data;
},
});
return query;
}