Impremented test case create button and process

This commit is contained in:
Takeshi Kimata
2024-03-23 23:17:40 +09:00
parent ad9dd5fce1
commit 4122e475a1
3 changed files with 89 additions and 6 deletions

View File

@@ -3,16 +3,53 @@ const router = express.Router();
const defineCase = require("../../models/cases");
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) {
const Case = defineCase(sequelize, DataTypes);
router.post("/", async (req, res) => {
try {
const { title, state, priority, type, automationStatus, description, template, preConditions, expectedResults, folderId } = req.body;
if (!title || !state || !priority || !type || !automationStatus || !template || !folderId) {
return res.status(400).json({ error: "Title, state, priority, type, automationStatus, template, and folderId are required" });
if (
requiredFields.some((field) => {
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({
title,
state,

View File

@@ -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) {
const fetchOptions = {
method: "DELETE",
@@ -47,7 +84,7 @@ async function fetchDeleteCase(caseId: number) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error) {
console.error("Error deleting project:", error);
console.error("Error deleting case:", error);
throw error;
}
}
@@ -73,6 +110,13 @@ export default function Page({
fetchDataEffect();
}, []);
const handleCreateCase = async (folderId: string) => {
const newCase = await fetchCreateCase(folderId);
const updateCases = [...cases];
updateCases.push(newCase);
setCases(updateCases);
};
const handleDeleteCase = async (caseId: number) => {
await fetchDeleteCase(caseId);
const data = await fetchCases(url);
@@ -84,6 +128,7 @@ export default function Page({
<TestCaseTable
projectId={params.folderId}
cases={cases}
onCreateCase={() => handleCreateCase(params.folderId)}
onDeleteCase={handleDeleteCase}
/>
</>

View File

@@ -46,10 +46,11 @@ type Case = {
type Props = {
projectId: boolean;
cases: Case[];
onCreateCase: () => 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 [sortDescriptor, setSortDescriptor] = useState<SortDescriptor>({
column: "id",
@@ -141,7 +142,7 @@ export default function TestCaseTable({ projectId, cases, onDeleteCase }: Props)
startContent={<Plus size={16} />}
size="sm"
color="primary"
onClick={() => console.log("create")}
onClick={onCreateCase}
>
New Test Case
</Button>