/
vladlevin790
/
cpp-lab-2
Обзор
Документация
Войти
/
vladlevin790
/
cpp-lab-2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
web/js/utils.test.js
49 строк
2 KB
vladlevin790
init
05 май 2026, 21:32
05 май 2026, 21:32
3310cbd
Код
Авторство
О чём код?
import { jest } from "@jest/globals"; import { escapeHtml, wait, getIntegerFromInput, getTextFromInput, normalizeAction, parseJson, shouldUseFlyAnimation } from "./utils.js"; describe("frontend utilities", () => { test("parses json and returns null for broken input", () => { const consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {}); expect(parseJson("{\"ok\":true}")).toEqual({ ok: true }); expect(parseJson("{")).toBeNull(); consoleSpy.mockRestore(); }); test("normalizes input values", () => { expect(getIntegerFromInput({ value: "12.8" }, 0)).toBe(12); expect(getIntegerFromInput({ value: "abc" }, 5)).toBe(5); expect(getTextFromInput({ value: "hello" }, "")).toBe("hello"); expect(getTextFromInput({ value: "" }, "fallback")).toBe("fallback"); }); test("escapes html before rendering logs", () => { expect(escapeHtml("<b>&\"'</b>")).toBe("<b>&"'</b>"); }); test("chooses fly animation only for light operations", () => { expect(normalizeAction(" Insert ")).toBe("insert"); expect(shouldUseFlyAnimation({ action: "insert" }, 10)).toBe(true); expect(shouldUseFlyAnimation({ action: "calculate-mean" }, 10)).toBe(false); expect(shouldUseFlyAnimation({ action: "insert" }, 80)).toBe(false); expect(shouldUseFlyAnimation(null, 1)).toBe(true); }); test("wait resolves after a timeout", async () => { jest.useFakeTimers(); const promise = wait(50); jest.advanceTimersByTime(50); await expect(promise).resolves.toBeUndefined(); jest.useRealTimers(); }); });