From b00c93bcd22a6c7dfe644063d57204a3b0f9a52c Mon Sep 17 00:00:00 2001 From: Takeshi Kimata <117462761+kimatata@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:34:21 +0900 Subject: [PATCH] update runCase at once --- backend/routes/cases/indexByProjectId.js | 2 +- .../[projectId]/runs/[runId]/RunEditor.tsx | 26 ++---- .../runs/[runId]/TestCaseSelector.tsx | 12 +-- .../[projectId]/runs/runsControl.test.ts | 82 +++++++++++++------ .../projects/[projectId]/runs/runsControl.ts | 58 +------------ frontend/types/case.ts | 2 + 6 files changed, 78 insertions(+), 104 deletions(-) diff --git a/backend/routes/cases/indexByProjectId.js b/backend/routes/cases/indexByProjectId.js index 1bfe0b6..35c56ec 100644 --- a/backend/routes/cases/indexByProjectId.js +++ b/backend/routes/cases/indexByProjectId.js @@ -39,7 +39,7 @@ module.exports = function (sequelize) { }, { model: RunCase, - attributes: ['runId', 'status'], + attributes: ['id', 'runId', 'status'], }, ], }); diff --git a/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx b/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx index 5730e46..552c393 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx +++ b/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/RunEditor.tsx @@ -21,18 +21,11 @@ import { import { Save, ArrowLeft, Folder, ChevronDown, CopyPlus, CopyMinus, RotateCw } from 'lucide-react'; import RunProgressChart from './RunPregressDonutChart'; import TestCaseSelector from './TestCaseSelector'; -import { testRunStatus } from '@/config/selection'; +import { testRunCaseStatus, testRunStatus } from '@/config/selection'; import { RunType, RunStatusCountType, RunMessages } from '@/types/run'; import { CaseType } from '@/types/case'; import { FolderType } from '@/types/folder'; -import { - fetchRun, - updateRun, - updateRunCases, - processRunCases, - fetchProjectCases, - processTestCases, -} from '../runsControl'; +import { fetchRun, updateRun, updateRunCases, fetchProjectCases, processTestCases } from '../runsControl'; import { fetchFolders } from '../../folders/foldersControl'; import { TokenContext } from '@/utils/TokenProvider'; import { useTheme } from 'next-themes'; @@ -122,14 +115,13 @@ export default function RunEditor({ projectId, runId, messages, locale }: Props) const handleChangeStatus = async (changeCaseId: number, status: number) => { setIsDirty(true); - setTestCases((prevTestCases) => { - return prevTestCases.map((testCase) => { - if (testCase.id === changeCaseId) { - return { ...testCase, runStatus: status, editState: 'changed' }; - } - return testCase; - }); - }); + const newTestCases = [...testCases]; + const found = newTestCases.find((testCase) => testCase.id === changeCaseId); + if (found && found.RunCases && testRunCaseStatus.length > 0) { + found.RunCases[0].status = status; + found.RunCases[0].editState = 'changed'; + } + setTestCases(newTestCases); }; const handleIncludeExcludeCase = async (isInclude: boolean, clickedTestCaseId: number) => { diff --git a/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/TestCaseSelector.tsx b/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/TestCaseSelector.tsx index 34362c0..3d4eaa2 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/TestCaseSelector.tsx +++ b/frontend/src/app/[locale]/projects/[projectId]/runs/[runId]/TestCaseSelector.tsx @@ -156,7 +156,7 @@ export default function TestCaseSelector({ startContent={renderStatusIcon(runCaseStatus.uid)} onPress={() => onStatusChange(testCase.id, index)} > - {messages[runStatus.uid]} + {messages[runCaseStatus.uid]} ))} @@ -250,12 +250,14 @@ export default function TestCaseSelector({ )} - - {(item) => ( + + {sortedItems.map((item) => ( - {(columnKey) => {renderCell(item, columnKey)}} + {headerColumns.map((column) => ( + {renderCell(item, column.uid)} + ))} - )} + ))} diff --git a/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.test.ts b/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.test.ts index 0074e0d..6728d7b 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.test.ts +++ b/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.test.ts @@ -1,41 +1,75 @@ import { describe, expect, test } from 'vitest'; -import { processRunCases } from './runsControl'; -import { RunCaseType } from '@/types/run'; +import { processTestCases } from './runsControl'; +import { CaseType } from '@/types/case'; -const initialRuncases: RunCaseType[] = [ - { id: 1, runId: 1, caseId: 1, editState: 'notChanged', status: 0 }, - { id: 2, runId: 1, caseId: 2, editState: 'notChanged', status: 0 }, - { id: 3, runId: 1, caseId: 3, editState: 'notChanged', status: 0 }, +const sampleTestCase: CaseType = { + id: 1, + title: '', + state: 0, + priority: 0, + type: 0, + automationStatus: 0, + description: '', + template: 0, + preConditions: '', + expectedResults: '', + folderId: 1, +}; + +const initialTestCases: CaseType[] = [ + { + ...sampleTestCase, + id: 1, + RunCases: [ + { + id: 1, + runId: 1, + caseId: 1, + status: 0, + editState: 'notChanged', + }, + ], + }, + { + ...sampleTestCase, + id: 2, + RunCases: [ + { + id: 2, + runId: 1, + caseId: 2, + status: 0, + editState: 'notChanged', + }, + ], + }, + { + ...sampleTestCase, + id: 3, + RunCases: [], + }, ]; describe('runsControl', () => { - test('Add test cases not yet included in the test run', () => { + test('include test case', () => { const isInclude = true; - const keys: number[] = [4, 5]; + const keys: number[] = [3]; const runId = 1; - const currentRunCases = [...initialRuncases]; - const newRunCases = processRunCases(isInclude, keys, runId, currentRunCases); + const currentRunCases = [...initialTestCases]; + const newTestCases = processTestCases(isInclude, keys, runId, currentRunCases); - expect(newRunCases).toStrictEqual([ - { id: 1, runId: 1, caseId: 1, editState: 'notChanged', status: 0 }, - { id: 2, runId: 1, caseId: 2, editState: 'notChanged', status: 0 }, - { id: 3, runId: 1, caseId: 3, editState: 'notChanged', status: 0 }, - { id: -1, runId: runId, caseId: 4, status: -1, editState: 'new' }, - { id: -1, runId: runId, caseId: 5, status: -1, editState: 'new' }, - ]); + expect(newTestCases[2].RunCases?.length).toBe(1); + expect(newTestCases[2].RunCases[0].editState).toBe('new'); }); test('Exclude test cases already included in the test run', () => { const isInclude = false; const keys: number[] = [1, 3]; const runId = 1; - const currentRunCases = [...initialRuncases]; - const newRunCases = processRunCases(isInclude, keys, runId, currentRunCases); + const currentRunCases = [...initialTestCases]; + const newTestCases = processTestCases(isInclude, keys, runId, currentRunCases); - expect(newRunCases).toStrictEqual([ - { id: 1, runId: 1, caseId: 1, editState: 'deleted', status: 0 }, - { id: 2, runId: 1, caseId: 2, editState: 'notChanged', status: 0 }, - { id: 3, runId: 1, caseId: 3, editState: 'deleted', status: 0 }, - ]); + expect(newTestCases[0].RunCases[0].editState).toBe('deleted'); + expect(newTestCases[2].RunCases[0].editState).toBe('deleted'); }); }); diff --git a/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts b/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts index 692b1ca..6f0a52c 100644 --- a/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts +++ b/frontend/src/app/[locale]/projects/[projectId]/runs/runsControl.ts @@ -209,66 +209,12 @@ function processTestCases(isInclude: boolean, keys: number[], runId: number, cur return updatedTestCases; } -// function processRunCases( -// isInclude: boolean, -// keys: number[], -// runId: number, -// currentRunCases: RunCaseType[] -// ): RunCaseType[] { -// const updatedRunCases = [...currentRunCases]; - -// if (isInclude) { -// keys.forEach((caseId) => { -// const existingRunCase = currentRunCases.find((runCase) => runCase.caseId === caseId); -// if (existingRunCase) { -// // already included -// if (existingRunCase.editState === 'notChanged') { -// // do nothing -// } else if (existingRunCase.editState === 'changed') { -// // do nothing -// } else if (existingRunCase.editState === 'new') { -// // do nothing -// } else if (existingRunCase.editState === 'deleted') { -// existingRunCase.editState = 'changed'; -// } -// } else { -// updatedRunCases.push({ -// id: -1, -// runId: runId, -// caseId: caseId, -// status: 0, -// editState: 'new', -// }); -// } -// }); -// } else { -// keys.forEach((caseId) => { -// const existingRunCase = currentRunCases.find((runCase) => runCase.caseId === caseId); -// if (!existingRunCase) { -// // already excluded -// } else { -// if (existingRunCase.editState === 'notChanged') { -// existingRunCase.editState = 'deleted'; -// } else if (existingRunCase.editState === 'changed') { -// existingRunCase.editState = 'deleted'; -// } else if (existingRunCase.editState === 'new') { -// existingRunCase.editState = 'deleted'; -// } else if (existingRunCase.editState === 'deleted') { -// // do nothing -// } -// } -// }); -// } - -// return updatedRunCases; -// } - async function updateRunCases(jwt: string, runId: number, testCases: CaseType[]) { const runCases: RunCaseType[] = []; testCases.forEach((itr) => { if (itr.RunCases && itr.RunCases.length > 0) { runCases.push({ - id: -1, + id: itr.RunCases[0].id, caseId: itr.id, runId: runId, status: itr.RunCases[0].status, @@ -277,8 +223,6 @@ async function updateRunCases(jwt: string, runId: number, testCases: CaseType[]) } }); - console.log(testCases, runCases); - const fetchOptions = { method: 'POST', headers: { diff --git a/frontend/types/case.ts b/frontend/types/case.ts index 2558bd8..cdba0a8 100644 --- a/frontend/types/case.ts +++ b/frontend/types/case.ts @@ -35,7 +35,9 @@ type StepType = { }; type RunCaseType = { + id: number; runId: number; + caseId: number; status: number; editState: 'notChanged' | 'changed' | 'new' | 'deleted'; };