b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
|
|
import { calculateReadingTime, countWords } from "../../src/components/PortableTextEditor";
|
|
|
|
const words = (n: number) => "word ".repeat(n).trim();
|
|
|
|
describe("Editor Metrics", () => {
|
|
describe("calculateReadingTime", () => {
|
|
it("returns 0 minutes for empty document", () => {
|
|
expect(calculateReadingTime("")).toBe(0);
|
|
});
|
|
|
|
it("returns 1 minute for less than 200 words", () => {
|
|
expect(calculateReadingTime(words(1))).toBe(1);
|
|
expect(calculateReadingTime(words(100))).toBe(1);
|
|
expect(calculateReadingTime(words(199))).toBe(1);
|
|
});
|
|
|
|
it("returns 1 minute for exactly 200 words", () => {
|
|
expect(calculateReadingTime(words(200))).toBe(1);
|
|
});
|
|
|
|
it("returns 2 minutes for 201-400 words", () => {
|
|
expect(calculateReadingTime(words(201))).toBe(2);
|
|
expect(calculateReadingTime(words(300))).toBe(2);
|
|
expect(calculateReadingTime(words(400))).toBe(2);
|
|
});
|
|
|
|
it("returns correct reading time for larger documents", () => {
|
|
expect(calculateReadingTime(words(1000))).toBe(5);
|
|
expect(calculateReadingTime(words(1001))).toBe(6);
|
|
expect(calculateReadingTime(words(2000))).toBe(10);
|
|
});
|
|
|
|
it("always rounds up (ceil)", () => {
|
|
// 201 / 200 = 1.005, ceil = 2
|
|
expect(calculateReadingTime(words(201))).toBe(2);
|
|
// 401 / 200 = 2.005, ceil = 3
|
|
expect(calculateReadingTime(words(401))).toBe(3);
|
|
});
|
|
|
|
it("counts CJK content by character, matching the published reading time", () => {
|
|
// Without the CJK rate, a spaceless paragraph counts as one word and
|
|
// collapses to "1 min read". 2000 / 500 = 4 minutes, the same result
|
|
// as the published reading-time util's CJK test.
|
|
expect(calculateReadingTime("日".repeat(2000))).toBe(4);
|
|
expect(calculateReadingTime("中".repeat(1000))).toBe(2);
|
|
expect(calculateReadingTime("한".repeat(2000))).toBe(4);
|
|
});
|
|
|
|
it("adds word-based and CJK reading time for mixed content", () => {
|
|
// 200 words (1 min) + 500 CJK characters (1 min) = 2 minutes
|
|
expect(calculateReadingTime(`${words(200)} ${"日".repeat(500)}`)).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe("countWords", () => {
|
|
it("counts space-separated words for word-based scripts", () => {
|
|
expect(countWords("")).toBe(0);
|
|
expect(countWords("hello world")).toBe(2);
|
|
});
|
|
|
|
it("counts CJK characters individually", () => {
|
|
expect(countWords("日本語")).toBe(3);
|
|
expect(countWords("中文测试")).toBe(4);
|
|
});
|
|
|
|
it("combines word-based and CJK counts", () => {
|
|
expect(countWords("hello 日本")).toBe(3);
|
|
});
|
|
});
|
|
});
|