Merge pull request #2272 from automatisch/AUT-1409

feat: accept falsy default values
This commit is contained in:
Ali BARIN
2025-01-27 16:39:07 +01:00
committed by GitHub
6 changed files with 40 additions and 19 deletions

View File

@@ -12,7 +12,7 @@ test.describe('Webhook flow', () => {
test('Create a new flow with a sync Webhook step then a Webhook step', async ({ test('Create a new flow with a sync Webhook step then a Webhook step', async ({
flowEditorPage, flowEditorPage,
page, page,
request request,
}) => { }) => {
await flowEditorPage.flowName.click(); await flowEditorPage.flowName.click();
await flowEditorPage.flowNameInput.fill('syncWebhook'); await flowEditorPage.flowNameInput.fill('syncWebhook');
@@ -23,10 +23,11 @@ test.describe('Webhook flow', () => {
await expect(flowEditorPage.continueButton).toHaveCount(1); await expect(flowEditorPage.continueButton).toHaveCount(1);
await expect(flowEditorPage.continueButton).not.toBeEnabled(); await expect(flowEditorPage.continueButton).not.toBeEnabled();
await page await expect(
page
.getByTestId('parameters.statusCode-power-input') .getByTestId('parameters.statusCode-power-input')
.locator('[contenteditable]') .locator('[contenteditable]')
.fill('200'); ).toHaveText('200');
await flowEditorPage.clickAway(); await flowEditorPage.clickAway();
await expect(flowEditorPage.continueButton).toHaveCount(1); await expect(flowEditorPage.continueButton).toHaveCount(1);
await expect(flowEditorPage.continueButton).not.toBeEnabled(); await expect(flowEditorPage.continueButton).not.toBeEnabled();
@@ -36,7 +37,9 @@ test.describe('Webhook flow', () => {
.locator('[contenteditable]') .locator('[contenteditable]')
.fill('response from webhook'); .fill('response from webhook');
await flowEditorPage.clickAway(); await flowEditorPage.clickAway();
await expect(page.getByTestId("parameters.headers.0.key-power-input")).toBeVisible(); await expect(
page.getByTestId('parameters.headers.0.key-power-input')
).toBeVisible();
await expect(flowEditorPage.continueButton).toBeEnabled(); await expect(flowEditorPage.continueButton).toBeEnabled();
await flowEditorPage.continueButton.click(); await flowEditorPage.continueButton.click();
@@ -52,7 +55,7 @@ test.describe('Webhook flow', () => {
test('Create a new flow with an async Webhook step then a Webhook step', async ({ test('Create a new flow with an async Webhook step then a Webhook step', async ({
flowEditorPage, flowEditorPage,
page, page,
request request,
}) => { }) => {
await flowEditorPage.flowName.click(); await flowEditorPage.flowName.click();
await flowEditorPage.flowNameInput.fill('asyncWebhook'); await flowEditorPage.flowNameInput.fill('asyncWebhook');
@@ -75,7 +78,9 @@ test.describe('Webhook flow', () => {
.locator('[contenteditable]') .locator('[contenteditable]')
.fill('response from webhook'); .fill('response from webhook');
await flowEditorPage.clickAway(); await flowEditorPage.clickAway();
await expect(page.getByTestId("parameters.headers.0.key-power-input")).toBeVisible(); await expect(
page.getByTestId('parameters.headers.0.key-power-input')
).toBeVisible();
await expect(flowEditorPage.continueButton).toBeEnabled(); await expect(flowEditorPage.continueButton).toBeEnabled();
await flowEditorPage.continueButton.click(); await flowEditorPage.continueButton.click();

View File

@@ -14,7 +14,7 @@ function CodeEditor(props) {
required, required,
name, name,
label, label,
defaultValue, defaultValue = '',
shouldUnregister = false, shouldUnregister = false,
disabled, disabled,
'data-test': dataTest, 'data-test': dataTest,
@@ -39,7 +39,7 @@ function CodeEditor(props) {
<Controller <Controller
rules={{ required }} rules={{ required }}
name={name} name={name}
defaultValue={defaultValue || ''} defaultValue={defaultValue}
control={control} control={control}
shouldUnregister={shouldUnregister} shouldUnregister={shouldUnregister}
render={({ field }) => ( render={({ field }) => (

View File

@@ -53,7 +53,7 @@ function ControlledAutocomplete(props) {
<Controller <Controller
rules={{ required }} rules={{ required }}
name={name} name={name}
defaultValue={defaultValue || ''} defaultValue={defaultValue}
control={control} control={control}
shouldUnregister={shouldUnregister} shouldUnregister={shouldUnregister}
render={({ render={({

View File

@@ -23,12 +23,29 @@ function DynamicField(props) {
return fields.reduce((previousValue, field) => { return fields.reduce((previousValue, field) => {
return { return {
...previousValue, ...previousValue,
[field.key]: '', [field.key]: field.value ?? '',
__id: uuidv4(), __id: uuidv4(),
}; };
}, {}); }, {});
}, [fields]); }, [fields]);
const generateDefaultValue = React.useCallback(() => {
if (defaultValue?.length > 0) {
return defaultValue.map((item) => {
return fields.reduce((previousValue, field) => {
return {
...previousValue,
// if field value is different than null or undefined - use it,
// otherwise use the parent default value
[field.key]: field.value ?? item[field.key] ?? '',
};
}, {});
});
} else {
return [createEmptyItem()];
}
}, [defaultValue, fields, createEmptyItem]);
const addItem = React.useCallback(() => { const addItem = React.useCallback(() => {
const values = getValues(name); const values = getValues(name);
if (!values) { if (!values) {
@@ -52,13 +69,11 @@ function DynamicField(props) {
React.useEffect( React.useEffect(
function addInitialGroupWhenEmpty() { function addInitialGroupWhenEmpty() {
const fieldValues = getValues(name); const fieldValues = getValues(name);
if (!fieldValues && defaultValue) { if (!fieldValues) {
setValue(name, defaultValue); setValue(name, generateDefaultValue());
} else if (!fieldValues) {
setValue(name, [createEmptyItem()]);
} }
}, },
[createEmptyItem, defaultValue], [generateDefaultValue],
); );
return ( return (

View File

@@ -162,6 +162,7 @@ function InputCreator(props) {
required={required} required={required}
disabled={disabled} disabled={disabled}
shouldUnregister={shouldUnregister} shouldUnregister={shouldUnregister}
defaultValue={value}
/> />
{isDynamicFieldsLoading && !additionalFields?.length && ( {isDynamicFieldsLoading && !additionalFields?.length && (

View File

@@ -23,7 +23,7 @@ function TextField(props) {
const { const {
required, required,
name, name,
defaultValue, defaultValue = '',
shouldUnregister = false, shouldUnregister = false,
clickToCopy = false, clickToCopy = false,
readOnly = false, readOnly = false,
@@ -38,7 +38,7 @@ function TextField(props) {
<Controller <Controller
rules={{ required }} rules={{ required }}
name={name} name={name}
defaultValue={defaultValue || ''} defaultValue={defaultValue}
control={control} control={control}
shouldUnregister={shouldUnregister} shouldUnregister={shouldUnregister}
render={({ render={({