Files
duty-teller/webapp/js/main.test.js
Nikolay Tatarinov 67ba9826c7 feat: unify language handling across the application
- Updated the language configuration to use a single source of truth from `DEFAULT_LANGUAGE` for the bot, API, and Mini App, eliminating auto-detection from user settings.
- Refactored the `get_lang` function to always return `DEFAULT_LANGUAGE`, ensuring consistent language usage throughout the application.
- Modified the handling of language in various components, including API responses and UI elements, to reflect the new language management approach.
- Enhanced documentation and comments to clarify the changes in language handling.
- Added unit tests to verify the new language handling behavior and ensure coverage for the updated functionality.
2026-03-02 23:05:28 +03:00

70 lines
2.6 KiB
JavaScript

/**
* Unit tests for main.js: applyLangToUi (locale-to-DOM) and lang re-evaluation behaviour.
*/
import { describe, it, expect, beforeAll } from "vitest";
beforeAll(() => {
document.body.innerHTML =
'<div id="calendarSticky">' +
'<button id="prevMonth"></button><h1 id="monthTitle"></h1><button id="nextMonth"></button>' +
'<div class="weekdays">' +
'<span></span><span></span><span></span><span></span><span></span><span></span><span></span>' +
"</div></div>" +
'<div id="dutyList"></div>' +
'<div id="loading"><span class="loading__text"></span></div>' +
'<div id="error"></div><div id="accessDenied"></div>';
});
import { applyLangToUi } from "./main.js";
import { state } from "./dom.js";
describe("applyLangToUi", () => {
it("state.lang is set from getLang() at startup (getLang reads window.__DT_LANG; default en)", () => {
expect(state.lang).toBe("en");
});
it("sets document title and loading text for en", () => {
state.lang = "en";
applyLangToUi();
expect(document.title).toBe("Duty Calendar");
const loadingText = document.querySelector(".loading__text");
expect(loadingText && loadingText.textContent).toBe("Loading…");
});
it("sets document title and loading text for ru", () => {
state.lang = "ru";
applyLangToUi();
expect(document.title).toBe("Календарь дежурств");
const loadingText = document.querySelector(".loading__text");
expect(loadingText && loadingText.textContent).toBe("Загрузка…");
});
it("sets documentElement.lang to state.lang", () => {
state.lang = "en";
applyLangToUi();
expect(document.documentElement.lang).toBe("en");
state.lang = "ru";
applyLangToUi();
expect(document.documentElement.lang).toBe("ru");
});
it("sets weekday labels and nav aria-labels for current lang", () => {
state.lang = "en";
applyLangToUi();
const weekdays = document.querySelectorAll(".weekdays span");
expect(weekdays.length).toBe(7);
expect(weekdays[0].textContent).toBe("Mon");
const prevBtn = document.getElementById("prevMonth");
const nextBtn = document.getElementById("nextMonth");
expect(prevBtn && prevBtn.getAttribute("aria-label")).toBe("Previous month");
expect(nextBtn && nextBtn.getAttribute("aria-label")).toBe("Next month");
state.lang = "ru";
applyLangToUi();
expect(weekdays[0].textContent).toBe("Пн");
expect(prevBtn && prevBtn.getAttribute("aria-label")).toContain("Пред");
expect(nextBtn && nextBtn.getAttribute("aria-label")).toContain("След");
});
});