Impremented test case create button and process
This commit is contained in:
@@ -3,16 +3,53 @@ const router = express.Router();
|
|||||||
const defineCase = require("../../models/cases");
|
const defineCase = require("../../models/cases");
|
||||||
const { DataTypes } = require("sequelize");
|
const { DataTypes } = require("sequelize");
|
||||||
|
|
||||||
|
const requiredFields = [
|
||||||
|
"title",
|
||||||
|
"state",
|
||||||
|
"priority",
|
||||||
|
"type",
|
||||||
|
"automationStatus",
|
||||||
|
"template",
|
||||||
|
"folderId",
|
||||||
|
];
|
||||||
|
|
||||||
|
function isEmpty(value) {
|
||||||
|
if (value === null || value === undefined) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function (sequelize) {
|
module.exports = function (sequelize) {
|
||||||
const Case = defineCase(sequelize, DataTypes);
|
const Case = defineCase(sequelize, DataTypes);
|
||||||
|
|
||||||
router.post("/", async (req, res) => {
|
router.post("/", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { title, state, priority, type, automationStatus, description, template, preConditions, expectedResults, folderId } = req.body;
|
if (
|
||||||
if (!title || !state || !priority || !type || !automationStatus || !template || !folderId) {
|
requiredFields.some((field) => {
|
||||||
return res.status(400).json({ error: "Title, state, priority, type, automationStatus, template, and folderId are required" });
|
return isEmpty(req.body[field]);
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error:
|
||||||
|
"Title, state, priority, type, automationStatus, template, and folderId are required",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
title,
|
||||||
|
state,
|
||||||
|
priority,
|
||||||
|
type,
|
||||||
|
automationStatus,
|
||||||
|
description,
|
||||||
|
template,
|
||||||
|
preConditions,
|
||||||
|
expectedResults,
|
||||||
|
folderId,
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
const newCase = await Case.create({
|
const newCase = await Case.create({
|
||||||
title,
|
title,
|
||||||
state,
|
state,
|
||||||
|
|||||||
@@ -31,6 +31,43 @@ async function fetchCases(url) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchCreateCase(folderId: string) {
|
||||||
|
const newCase = {
|
||||||
|
title: "untitled case",
|
||||||
|
state: 0,
|
||||||
|
priority: 2,
|
||||||
|
type: 0,
|
||||||
|
automationStatus: 0,
|
||||||
|
description: "",
|
||||||
|
template: 0,
|
||||||
|
preConditions: "",
|
||||||
|
expectedResults: "",
|
||||||
|
folderId: folderId,
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchOptions = {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(newCase),
|
||||||
|
};
|
||||||
|
|
||||||
|
const url = `${apiServer}/cases`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, fetchOptions);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
}
|
||||||
|
const data = await response.json();
|
||||||
|
return data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error creating case:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchDeleteCase(caseId: number) {
|
async function fetchDeleteCase(caseId: number) {
|
||||||
const fetchOptions = {
|
const fetchOptions = {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
@@ -47,7 +84,7 @@ async function fetchDeleteCase(caseId: number) {
|
|||||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error deleting project:", error);
|
console.error("Error deleting case:", error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,6 +110,13 @@ export default function Page({
|
|||||||
fetchDataEffect();
|
fetchDataEffect();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const handleCreateCase = async (folderId: string) => {
|
||||||
|
const newCase = await fetchCreateCase(folderId);
|
||||||
|
const updateCases = [...cases];
|
||||||
|
updateCases.push(newCase);
|
||||||
|
setCases(updateCases);
|
||||||
|
};
|
||||||
|
|
||||||
const handleDeleteCase = async (caseId: number) => {
|
const handleDeleteCase = async (caseId: number) => {
|
||||||
await fetchDeleteCase(caseId);
|
await fetchDeleteCase(caseId);
|
||||||
const data = await fetchCases(url);
|
const data = await fetchCases(url);
|
||||||
@@ -84,6 +128,7 @@ export default function Page({
|
|||||||
<TestCaseTable
|
<TestCaseTable
|
||||||
projectId={params.folderId}
|
projectId={params.folderId}
|
||||||
cases={cases}
|
cases={cases}
|
||||||
|
onCreateCase={() => handleCreateCase(params.folderId)}
|
||||||
onDeleteCase={handleDeleteCase}
|
onDeleteCase={handleDeleteCase}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -46,10 +46,11 @@ type Case = {
|
|||||||
type Props = {
|
type Props = {
|
||||||
projectId: boolean;
|
projectId: boolean;
|
||||||
cases: Case[];
|
cases: Case[];
|
||||||
|
onCreateCase: () => void;
|
||||||
onDeleteCase: (caseId: number) => void;
|
onDeleteCase: (caseId: number) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function TestCaseTable({ projectId, cases, onDeleteCase }: Props) {
|
export default function TestCaseTable({ projectId, cases, onCreateCase, onDeleteCase }: Props) {
|
||||||
const [selectedKeys, setSelectedKeys] = useState<Selection>(new Set([]));
|
const [selectedKeys, setSelectedKeys] = useState<Selection>(new Set([]));
|
||||||
const [sortDescriptor, setSortDescriptor] = useState<SortDescriptor>({
|
const [sortDescriptor, setSortDescriptor] = useState<SortDescriptor>({
|
||||||
column: "id",
|
column: "id",
|
||||||
@@ -141,7 +142,7 @@ export default function TestCaseTable({ projectId, cases, onDeleteCase }: Props)
|
|||||||
startContent={<Plus size={16} />}
|
startContent={<Plus size={16} />}
|
||||||
size="sm"
|
size="sm"
|
||||||
color="primary"
|
color="primary"
|
||||||
onClick={() => console.log("create")}
|
onClick={onCreateCase}
|
||||||
>
|
>
|
||||||
New Test Case
|
New Test Case
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user