Files
pleno-vue/tests/unit/goal-progress-resolver.spec.js
T
Jeppe Bundgaard b39b458f4f Add .prettierrc.json and refactor test files for improved formatting consistency:
- Introduced `.prettierrc.json` to enforce consistent code formatting across the project.
- Updated unit and e2e test files to address formatting issues, improve readability, and ensure alignment with the new Prettier configuration.
2026-04-13 09:13:27 +02:00

138 lines
4.8 KiB
JavaScript

import { describe, expect, it } from "vitest";
import {
computeCadenceTargetForTimeframe,
isGoalProgressKeyForTimeframe,
normalizeTimeframeKey,
resolveGoalProgressKey,
resolveGoalProgressPeriod,
resolveGoalProgressSlice,
toProgressNumber,
} from "@/views/dashboards/departmentDashboard/modules/goals/functions/goalProgressResolver.js";
describe("goal progress resolver", () => {
it("normalizes until_now to to_date", () => {
expect(normalizeTimeframeKey("until_now")).toBe("to_date");
expect(normalizeTimeframeKey("month")).toBe("month");
});
it("resolves legacy this_week alias for week timeframe", () => {
const scope = {
this_week: { count: 3, target: 7 },
};
expect(resolveGoalProgressKey(scope, "week", {})).toBe("this_week");
expect(resolveGoalProgressSlice(scope, "week", {})).toEqual({ count: 3, target: 7 });
});
it("resolves cadence-aware keys for bi-weekly targets", () => {
const scope = {
every_2_weeks: { count: 5, target: 14 },
week: { count: 1, target: 7 },
};
const criteria = {
target_duration: "WEEKS",
target_duration_every: 2,
};
expect(resolveGoalProgressKey(scope, "week", criteria)).toBe("every_2_weeks");
expect(resolveGoalProgressSlice(scope, "week", criteria)).toEqual({ count: 5, target: 14 });
});
it("does not force cadence bucket for unrelated timeframe tabs", () => {
const scope = {
to_date: { count: 1, target: 7 },
every_2_weeks: { count: 5, target: 14 },
};
const criteria = {
target_duration: "WEEKS",
target_duration_every: 2,
};
expect(resolveGoalProgressKey(scope, "month", criteria)).toBe("to_date");
expect(resolveGoalProgressSlice(scope, "month", criteria)).toEqual({ count: 1, target: 7 });
});
it("keeps timeframe-specific slices when available", () => {
const scope = {
today: { count: 2, target: 2 },
week: { count: 7, target: 14 },
every_2_weeks: { count: 7, target: 14 },
};
const criteria = {
target_duration: "WEEKS",
target_duration_every: 2,
};
expect(resolveGoalProgressKey(scope, "today", criteria)).toBe("today");
expect(resolveGoalProgressSlice(scope, "today", criteria)).toEqual({ count: 2, target: 2 });
});
it("marks only matching keys as timeframe-compatible", () => {
const criteria = {
target_duration: "WEEKS",
target_duration_every: 2,
};
expect(isGoalProgressKeyForTimeframe("every_2_weeks", "week", criteria)).toBe(true);
expect(isGoalProgressKeyForTimeframe("to_date", "week", criteria)).toBe(false);
expect(isGoalProgressKeyForTimeframe("month", "month", criteria)).toBe(true);
expect(isGoalProgressKeyForTimeframe("year", "month", criteria)).toBe(false);
});
it("falls back to preferred OpenAPI keys when selected key is missing", () => {
const scope = {
to_date: { count: 21, target: 30 },
all: { count: 42, target: 100 },
};
expect(resolveGoalProgressKey(scope, "month", {})).toBe("to_date");
});
it("extracts period from snake_case or camelCase date fields", () => {
expect(
resolveGoalProgressPeriod(
{ date_from: "2026-03-24T00:00:00+01:00", date_end: "2026-04-06T23:59:59+01:00" },
{ start: "2026-03-01T00:00:00+01:00", end: "2026-05-01T23:59:59+01:00" }
)
).toEqual({
from: "2026-03-24T00:00:00+01:00",
end: "2026-04-06T23:59:59+01:00",
});
expect(
resolveGoalProgressPeriod(
{ dateFrom: "2026-03-24T00:00:00+01:00", dateEnd: "2026-04-06T23:59:59+01:00" },
{ start: "2026-03-01T00:00:00+01:00", end: "2026-05-01T23:59:59+01:00" }
)
).toEqual({
from: "2026-03-24T00:00:00+01:00",
end: "2026-04-06T23:59:59+01:00",
});
});
it("parses numeric strings safely for progress values", () => {
expect(toProgressNumber("12.5")).toBe(12.5);
expect(toProgressNumber(8)).toBe(8);
expect(toProgressNumber("")).toBe(null);
expect(toProgressNumber("abc")).toBe(null);
});
it("computes cadence target per timeframe for bi-weekly goals", () => {
const criteria = {
target: 7,
target_duration: "WEEKS",
target_duration_every: 2,
start: "2026-03-24T00:00:00+01:00",
end: "2026-07-24T23:59:59+01:00",
};
const now = new Date("2026-04-10T12:00:00+01:00");
expect(computeCadenceTargetForTimeframe(criteria, "today", now)).toBe(7);
expect(computeCadenceTargetForTimeframe(criteria, "week", now)).toBe(14);
expect(computeCadenceTargetForTimeframe(criteria, "month", now)).toBe(21);
expect(computeCadenceTargetForTimeframe(criteria, "year", now)).toBe(63);
expect(computeCadenceTargetForTimeframe(criteria, "all", now)).toBe(63);
expect(computeCadenceTargetForTimeframe(criteria, "to_date", new Date("2026-03-24T12:00:00+01:00"))).toBe(7);
});
});