Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f98fd4

Browse files
SasSwartjaaydenhssncferreira
authored
fix: fix validation error during workspace creation without preset (#18494)
closes #18430. Selecting a preset, and then selecting the "None" preset used to result in a validation error because an invalid preset id ("") was sent to the backend. --------- Co-authored-by: Jaayden Halko <[email protected]> Co-authored-by: Susana Ferreira <[email protected]>
1 parent 796dc7a commit 4f98fd4

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.stories.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,46 @@ export const PresetReselected: Story = {
201201
},
202202
};
203203

204+
export const PresetNoneSelected: Story = {
205+
args: {
206+
...PresetsButNoneSelected.args,
207+
onSubmit: (request, owner) => {
208+
// Assert that template_version_preset_id is not present in the request
209+
console.assert(
210+
!("template_version_preset_id" in request),
211+
'template_version_preset_id should not be present when "None" is selected',
212+
);
213+
action("onSubmit")(request, owner);
214+
},
215+
},
216+
play: async ({ canvasElement }) => {
217+
const canvas = within(canvasElement);
218+
219+
// First select a preset to set the field value
220+
await userEvent.click(canvas.getByLabelText("Preset"));
221+
await userEvent.click(canvas.getByText("Preset 1"));
222+
223+
// Then select "None" to unset the field value
224+
await userEvent.click(canvas.getByLabelText("Preset"));
225+
await userEvent.click(canvas.getByText("None"));
226+
227+
// Fill in required fields and submit to test the API call
228+
await userEvent.type(
229+
canvas.getByLabelText("Workspace Name"),
230+
"test-workspace",
231+
);
232+
await userEvent.click(canvas.getByText("Create workspace"));
233+
},
234+
parameters: {
235+
docs: {
236+
description: {
237+
story:
238+
"This story tests that when 'None' preset is selected, the template_version_preset_id field is not included in the form submission. The story first selects a preset to set the field value, then selects 'None' to unset it, and finally submits the form to verify the API call behavior.",
239+
},
240+
},
241+
},
242+
};
243+
204244
export const ExternalAuth: Story = {
205245
args: {
206246
externalAuth: [

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
369369
setSelectedPresetIndex(index);
370370
form.setFieldValue(
371371
"template_version_preset_id",
372-
option?.value,
372+
// Empty string is equivalent to using None
373+
option?.value === "" ? undefined : option?.value,
373374
);
374375
}}
375376
placeholder="Select a preset"

site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ export const CreateWorkspacePageViewExperimental: FC<
563563
return;
564564
}
565565
setSelectedPresetIndex(index);
566+
form.setFieldValue(
567+
"template_version_preset_id",
568+
index === 0 ? undefined : option,
569+
);
566570
}}
567571
>
568572
<SelectTrigger>

0 commit comments

Comments
 (0)