feat: update language support and enhance API functionality
- 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.
This commit is contained in:
100
webapp/js/auth.test.js
Normal file
100
webapp/js/auth.test.js
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Unit tests for auth: getTgWebAppDataFromHash, getInitData, isLocalhost.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import {
|
||||
getTgWebAppDataFromHash,
|
||||
getInitData,
|
||||
isLocalhost,
|
||||
} from "./auth.js";
|
||||
|
||||
describe("getTgWebAppDataFromHash", () => {
|
||||
it("returns empty string when tgWebAppData= not present", () => {
|
||||
expect(getTgWebAppDataFromHash("foo=bar")).toBe("");
|
||||
});
|
||||
|
||||
it("returns value from tgWebAppData= to next &tgWebApp or end", () => {
|
||||
expect(getTgWebAppDataFromHash("tgWebAppData=encoded%3Ddata")).toBe(
|
||||
"encoded=data"
|
||||
);
|
||||
});
|
||||
|
||||
it("stops at &tgWebApp", () => {
|
||||
const hash = "tgWebAppData=value&tgWebAppVersion=6";
|
||||
expect(getTgWebAppDataFromHash(hash)).toBe("value");
|
||||
});
|
||||
|
||||
it("decodes URI component", () => {
|
||||
expect(getTgWebAppDataFromHash("tgWebAppData=hello%20world")).toBe(
|
||||
"hello world"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getInitData", () => {
|
||||
const origLocation = window.location;
|
||||
const origTelegram = window.Telegram;
|
||||
|
||||
afterEach(() => {
|
||||
window.location = origLocation;
|
||||
window.Telegram = origTelegram;
|
||||
});
|
||||
|
||||
it("returns initData from Telegram.WebApp when set", () => {
|
||||
window.Telegram = { WebApp: { initData: "sdk-init-data" } };
|
||||
delete window.location;
|
||||
window.location = { ...origLocation, hash: "", search: "" };
|
||||
expect(getInitData()).toBe("sdk-init-data");
|
||||
});
|
||||
|
||||
it("returns data from hash tgWebAppData when SDK empty", () => {
|
||||
window.Telegram = { WebApp: { initData: "" } };
|
||||
delete window.location;
|
||||
window.location = {
|
||||
...origLocation,
|
||||
hash: "#tgWebAppData=hash%20data",
|
||||
search: "",
|
||||
};
|
||||
expect(getInitData()).toBe("hash data");
|
||||
});
|
||||
|
||||
it("returns empty string when no source", () => {
|
||||
window.Telegram = { WebApp: { initData: "" } };
|
||||
delete window.location;
|
||||
window.location = { ...origLocation, hash: "", search: "" };
|
||||
expect(getInitData()).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isLocalhost", () => {
|
||||
const origLocation = window.location;
|
||||
|
||||
afterEach(() => {
|
||||
window.location = origLocation;
|
||||
});
|
||||
|
||||
it("returns true for localhost", () => {
|
||||
delete window.location;
|
||||
window.location = { ...origLocation, hostname: "localhost" };
|
||||
expect(isLocalhost()).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for 127.0.0.1", () => {
|
||||
delete window.location;
|
||||
window.location = { ...origLocation, hostname: "127.0.0.1" };
|
||||
expect(isLocalhost()).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for empty hostname", () => {
|
||||
delete window.location;
|
||||
window.location = { ...origLocation, hostname: "" };
|
||||
expect(isLocalhost()).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for other hostnames", () => {
|
||||
delete window.location;
|
||||
window.location = { ...origLocation, hostname: "example.com" };
|
||||
expect(isLocalhost()).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user