diff --git a/reana-ui/package.json b/reana-ui/package.json index 11680d15..a3620884 100644 --- a/reana-ui/package.json +++ b/reana-ui/package.json @@ -66,6 +66,7 @@ "eslint-config-react-app": "^7.0.1", "eslint-plugin-prettier": "^5.0.1", "eslint-plugin-react": "^7.23.2", + "jest-date-mock": "^1.0.8", "npm-run-all": "^4.1.5", "prettier": "^3.0.3", "semantic-ui-less": "^2.4.1", diff --git a/reana-ui/src/util.js b/reana-ui/src/util.js index 2400a44b..619b5ef3 100644 --- a/reana-ui/src/util.js +++ b/reana-ui/src/util.js @@ -156,13 +156,16 @@ export function getDuration(start, end) { /** * Parses workflows date info in a friendly way. */ -function parseWorkflowDates(workflow) { +export function parseWorkflowDates(workflow) { const createdMoment = moment.utc(workflow.created); const startedMoment = moment.utc(workflow.progress.run_started_at); const finishedMoment = moment.utc(workflow.progress.run_finished_at); const stoppedMoment = moment.utc(workflow.progress.run_stopped_at); // Mapping between workflow status and the end moment to use for calculating the duration + // If the workflow has not terminated yet (running, queued, pending), the endMoment should not be + // specified, and the current time will be used instead. const endMomentStatusMapping = { + failed: finishedMoment, finished: finishedMoment, stopped: stoppedMoment, deleted: finishedMoment.isValid() ? finishedMoment : stoppedMoment, diff --git a/reana-ui/src/util.test.js b/reana-ui/src/util.test.js index b899655a..4f00204e 100644 --- a/reana-ui/src/util.test.js +++ b/reana-ui/src/util.test.js @@ -4,7 +4,9 @@ import { formatSearch, getDuration, getMimeType, + parseWorkflowDates, } from "~/util"; +import { advanceTo, clear } from "jest-date-mock"; test.each([ ["path/to/test.txt", "text/plain"], @@ -56,3 +58,31 @@ test.each([ ])("formatFileSize(%p) === %p", (fileSize, formattedFileSize) => { expect(formatFileSize(fileSize)).toEqual(formattedFileSize); }); + +test.each([ + ["finished", "15 min 0 sec", { run_finished_at: "2024-01-18T08:45:00" }], + ["failed", "15 min 0 sec", { run_finished_at: "2024-01-18T08:45:00" }], + ["stopped", "10 min 0 sec", { run_stopped_at: "2024-01-18T08:40:00" }], + ["running", "20 min 0 sec", {}], + ["queued", "20 min 0 sec", {}], + ["pending", "20 min 0 sec", {}], + ["created", "20 min 0 sec", {}], +])( + `parseWorkflowDates [status: %p], duration === %p`, + (status, duration, progress_override) => { + const workflow = { + status: status, + created: "2024-01-18T08:25:00", + progress: { + run_started_at: "2024-01-18T08:30:00", + run_stopped_at: null, + run_finished_at: null, + ...progress_override, + }, + }; + + advanceTo(new Date(2024, 0, 18, 8, 50, 0)); + expect(parseWorkflowDates(workflow).duration).toEqual(duration); + clear(); + }, +); diff --git a/reana-ui/yarn.lock b/reana-ui/yarn.lock index 55437e83..23ce80ac 100644 --- a/reana-ui/yarn.lock +++ b/reana-ui/yarn.lock @@ -6420,6 +6420,11 @@ jest-config@^27.5.1: slash "^3.0.0" strip-json-comments "^3.1.1" +jest-date-mock@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/jest-date-mock/-/jest-date-mock-1.0.8.tgz#13468c0352c5a3614c6b356dbc6b88eb37d9e0b3" + integrity sha512-0Lyp+z9xvuNmLbK+5N6FOhSiBeux05Lp5bbveFBmYo40Aggl2wwxFoIrZ+rOWC8nDNcLeBoDd2miQdEDSf3iQw== + jest-diff@^27.5.1: version "27.5.1" resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-27.5.1.tgz#a07f5011ac9e6643cf8a95a462b7b1ecf6680def"