- Changed the default language in `index.html` from Russian to English, updating the title and button aria-labels for improved accessibility. - Refactored the `buildFetchOptions` function in `api.js` to include an optional external abort signal, enhancing request management. - Updated `fetchDuties` and `fetchCalendarEvents` to support request cancellation using the new abort signal, improving error handling. - Added unit tests for the API functions to ensure proper functionality, including handling of 403 errors and request cancellations. - Enhanced CSS styles for duty markers to improve visual consistency. - Removed unused code and improved the overall structure of the JavaScript files for better maintainability.
96 lines
3.0 KiB
JavaScript
96 lines
3.0 KiB
JavaScript
/**
|
|
* Unit tests for calendar: dutiesByDate (including edge case end_at < start_at),
|
|
* calendarEventsByDate.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll } from "vitest";
|
|
|
|
beforeAll(() => {
|
|
document.body.innerHTML =
|
|
'<div id="calendar"></div><h2 id="monthTitle"></h2>' +
|
|
'<div id="dutyList"></div><div id="loading"></div><div id="error"></div>' +
|
|
'<div id="accessDenied"></div><div class="header"></div><div class="weekdays"></div>' +
|
|
'<button id="prevMonth"></button><button id="nextMonth"></button>';
|
|
});
|
|
|
|
import { dutiesByDate, calendarEventsByDate } from "./calendar.js";
|
|
|
|
describe("dutiesByDate", () => {
|
|
it("groups duty by single local day", () => {
|
|
const duties = [
|
|
{
|
|
full_name: "Alice",
|
|
start_at: "2025-02-25T09:00:00Z",
|
|
end_at: "2025-02-25T18:00:00Z",
|
|
},
|
|
];
|
|
const byDate = dutiesByDate(duties);
|
|
expect(byDate["2025-02-25"]).toHaveLength(1);
|
|
expect(byDate["2025-02-25"][0].full_name).toBe("Alice");
|
|
});
|
|
|
|
it("spans duty across multiple days", () => {
|
|
const duties = [
|
|
{
|
|
full_name: "Bob",
|
|
start_at: "2025-02-25T00:00:00Z",
|
|
end_at: "2025-02-27T23:59:59Z",
|
|
},
|
|
];
|
|
const byDate = dutiesByDate(duties);
|
|
const keys = Object.keys(byDate).sort();
|
|
expect(keys.length).toBeGreaterThanOrEqual(2);
|
|
keys.forEach((k) => expect(byDate[k]).toHaveLength(1));
|
|
expect(byDate[keys[0]][0].full_name).toBe("Bob");
|
|
});
|
|
|
|
it("skips duty when end_at < start_at (no infinite loop)", () => {
|
|
const duties = [
|
|
{
|
|
full_name: "Bad",
|
|
start_at: "2025-02-28T12:00:00Z",
|
|
end_at: "2025-02-25T08:00:00Z",
|
|
},
|
|
];
|
|
const byDate = dutiesByDate(duties);
|
|
expect(Object.keys(byDate)).toHaveLength(0);
|
|
});
|
|
|
|
it("does not iterate more than MAX_DAYS_PER_DUTY", () => {
|
|
const start = "2025-01-01T00:00:00Z";
|
|
const end = "2026-06-01T00:00:00Z";
|
|
const duties = [{ full_name: "Long", start_at: start, end_at: end }];
|
|
const byDate = dutiesByDate(duties);
|
|
const keys = Object.keys(byDate).sort();
|
|
expect(keys.length).toBeLessThanOrEqual(367);
|
|
});
|
|
|
|
it("handles empty duties", () => {
|
|
expect(dutiesByDate([])).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("calendarEventsByDate", () => {
|
|
it("maps events to local date key by UTC date", () => {
|
|
const events = [
|
|
{ date: "2025-02-25", summary: "Holiday" },
|
|
{ date: "2025-02-25", summary: "Meeting" },
|
|
{ date: "2025-02-26", summary: "Other" },
|
|
];
|
|
const byDate = calendarEventsByDate(events);
|
|
expect(byDate["2025-02-25"]).toEqual(["Holiday", "Meeting"]);
|
|
expect(byDate["2025-02-26"]).toEqual(["Other"]);
|
|
});
|
|
|
|
it("skips events without summary", () => {
|
|
const events = [{ date: "2025-02-25", summary: null }];
|
|
const byDate = calendarEventsByDate(events);
|
|
expect(byDate["2025-02-25"] || []).toHaveLength(0);
|
|
});
|
|
|
|
it("handles null or undefined events", () => {
|
|
expect(calendarEventsByDate(null)).toEqual({});
|
|
expect(calendarEventsByDate(undefined)).toEqual({});
|
|
});
|
|
});
|