- Added Skeleton components to CurrentDutyView for better loading state representation. - Updated localization messages to include new labels for remaining time display. - Refactored remaining time display logic for clarity and improved user experience.
88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
/**
|
|
* Unit tests for current-duty: getRemainingTime, findCurrentDuty.
|
|
* Ported from webapp/js/currentDuty.test.js.
|
|
*/
|
|
|
|
import { describe, it, expect, vi } from "vitest";
|
|
import { getRemainingTime, findCurrentDuty } from "./current-duty";
|
|
import type { DutyWithUser } from "@/types";
|
|
|
|
describe("getRemainingTime", () => {
|
|
it("returns hours and minutes until end from now", () => {
|
|
const endAt = "2025-03-02T17:30:00.000Z";
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date("2025-03-02T12:00:00.000Z"));
|
|
const { hours, minutes } = getRemainingTime(endAt);
|
|
vi.useRealTimers();
|
|
expect(hours).toBe(5);
|
|
expect(minutes).toBe(30);
|
|
});
|
|
|
|
it("returns 0 when end is in the past", () => {
|
|
const endAt = "2025-03-02T09:00:00.000Z";
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date("2025-03-02T12:00:00.000Z"));
|
|
const { hours, minutes } = getRemainingTime(endAt);
|
|
vi.useRealTimers();
|
|
expect(hours).toBe(0);
|
|
expect(minutes).toBe(0);
|
|
});
|
|
|
|
it("returns 0 hours and 0 minutes when endAt is invalid", () => {
|
|
const { hours, minutes } = getRemainingTime("not-a-date");
|
|
expect(hours).toBe(0);
|
|
expect(minutes).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe("findCurrentDuty", () => {
|
|
it("returns duty when now is between start_at and end_at", () => {
|
|
const now = new Date();
|
|
const start = new Date(now);
|
|
start.setHours(start.getHours() - 1, 0, 0, 0);
|
|
const end = new Date(now);
|
|
end.setHours(end.getHours() + 1, 0, 0, 0);
|
|
const duties: DutyWithUser[] = [
|
|
{
|
|
id: 1,
|
|
user_id: 1,
|
|
event_type: "duty",
|
|
full_name: "Иванов",
|
|
start_at: start.toISOString(),
|
|
end_at: end.toISOString(),
|
|
phone: null,
|
|
username: null,
|
|
},
|
|
];
|
|
const duty = findCurrentDuty(duties);
|
|
expect(duty).not.toBeNull();
|
|
expect(duty?.full_name).toBe("Иванов");
|
|
});
|
|
|
|
it("returns null when no duty overlaps current time", () => {
|
|
const duties: DutyWithUser[] = [
|
|
{
|
|
id: 1,
|
|
user_id: 1,
|
|
event_type: "duty",
|
|
full_name: "Past",
|
|
start_at: "2020-01-01T09:00:00Z",
|
|
end_at: "2020-01-01T17:00:00Z",
|
|
phone: null,
|
|
username: null,
|
|
},
|
|
{
|
|
id: 2,
|
|
user_id: 2,
|
|
event_type: "duty",
|
|
full_name: "Future",
|
|
start_at: "2030-01-01T09:00:00Z",
|
|
end_at: "2030-01-01T17:00:00Z",
|
|
phone: null,
|
|
username: null,
|
|
},
|
|
];
|
|
expect(findCurrentDuty(duties)).toBeNull();
|
|
});
|
|
});
|