Skip to content

Commit

Permalink
[stu-346-placeholder-test] chore: Using object as input for better cl…
Browse files Browse the repository at this point in the history
…arity
  • Loading branch information
LatentDream committed Apr 21, 2024
1 parent 747c2a1 commit c776372
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 57 deletions.
6 changes: 1 addition & 5 deletions src/renderer/hooks/useTestImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ function parseDiscoverContainer(
settings: ImportTestSettings,
) {
return map(data.response, (container) => {
const new_elem = createNewTest(
container.testName,
container.path,
settings.importType,
);
const new_elem = createNewTest({ name: container.testName, path: container.path, type: settings.importType });
return new_elem;
});
}
Expand Down
43 changes: 24 additions & 19 deletions src/renderer/hooks/useTestSequencerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
testSequenceStopRequest,
} from "../routes/test_sequencer_panel/models/models";
import { produce } from "immer";
import { z } from "zod";

// sync this with the definition of setElems
export type SetElemsFn = {
Expand Down Expand Up @@ -98,34 +99,38 @@ const validateElements = (
return !validators.some((validator) => !validator(elems), validators);
};

export function createNewTest(
name: string,
path: string,
export const NewTest = z.object({
name: z.string(),
path: z.string(),
type: TestType,
exportToCloud?: boolean,
id?: string,
groupId?: string,
minValue?: number,
maxValue?: number,
unit?: string,
): Test {
exportToCloud: z.boolean().optional(),
id: z.string().optional(),
groupId: z.string().optional(),
minValue: z.number().optional(),
maxValue: z.number().optional(),
unit: z.string().optional(),
});

export type NewTest = z.infer<typeof NewTest>;

export function createNewTest(test: NewTest): Test {
const newTest: Test = {
type: "test",
id: id || uuidv4(),
groupId: groupId || uuidv4(),
path: path,
testName: name,
id: test.id || uuidv4(),
groupId: test.groupId || uuidv4(),
path: test.path,
testName: test.name,
runInParallel: false,
testType: type,
testType: test.type,
status: "pending",
completionTime: undefined,
error: null,
isSavedToCloud: false,
exportToCloud: exportToCloud === undefined ? true : exportToCloud,
exportToCloud: test.exportToCloud || true,
createdAt: new Date().toISOString(),
minValue: minValue,
maxValue: maxValue,
unit: unit,
minValue: test.minValue,
maxValue: test.maxValue,
unit: test.unit,
};
return newTest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ export const CreatePlaceholderTestModal = ({

async function onSubmit(values: z.infer<typeof formSchema>) {
const res = await addNewElems([
createNewTest(
values.name,
"",
"placeholder",
false,
undefined,
undefined,
values.min,
values.max,
values.unit,
),
createNewTest({
name: values.name,
path: "",
type: "placeholder",
exportToCloud: false,
minValue: values.min,
maxValue: values.max,
unit: values.unit,
}),
]);
if (res.isErr()) {
return;
Expand Down
44 changes: 22 additions & 22 deletions src/renderer/routes/test_sequencer_panel/utils/SequenceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ async function createExportableSequenceElementsFromTestSequencerElements(
}
const elements = [...elems].map((elem) => {
return elem.type === "test"
? createNewTest(
removeBaseFolderFromName(elem.testName, baseFolder),
elem.path.replaceAll(baseFolder, ""),
elem.testType,
elem.exportToCloud,
elem.id,
elem.groupId,
elem.minValue,
elem.maxValue,
elem.unit,
)
? createNewTest({
name: removeBaseFolderFromName(elem.testName, baseFolder),
path: elem.path.replaceAll(baseFolder, ""),
type: elem.testType,
exportToCloud: elem.exportToCloud,
id: elem.id,
groupId: elem.groupId,
minValue: elem.minValue,
maxValue: elem.maxValue,
unit: elem.unit,
})
: {
...elem,
condition: elem.condition.replaceAll(baseFolder, ""),
Expand All @@ -298,17 +298,17 @@ async function createTestSequencerElementsFromSequenceElements(
): Promise<Result<TestSequenceElement[], Error>> {
const elements: TestSequenceElement[] = [...sequence.elems].map((elem) => {
return elem.type === "test"
? createNewTest(
removeBaseFolderFromName(elem.testName, baseFolder),
baseFolder + elem.path,
elem.testType,
elem.exportToCloud,
elem.id,
elem.groupId,
elem.minValue,
elem.maxValue,
elem.unit,
)
? createNewTest({
name: removeBaseFolderFromName(elem.testName, baseFolder),
path: baseFolder + elem.path,
type: elem.testType,
exportToCloud: elem.exportToCloud,
id: elem.id,
groupId: elem.groupId,
minValue: elem.minValue,
maxValue: elem.maxValue,
unit: elem.unit,
})
: {
...elem,
};
Expand Down

0 comments on commit c776372

Please sign in to comment.