Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions frontend/__tests__/test/events/stats.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { describe, it, expect, beforeEach, vi, type Mock } from "vitest";

vi.mock("../../../src/ts/test/test-stats", () => ({
start: 1000,
Expand Down Expand Up @@ -26,16 +26,8 @@ vi.mock("../../../src/ts/test/test-words", () => {
get(): Word[] {
return [...list];
},
push(word: string) {
let commit: CommitChar = "";
if (word.endsWith(" ")) {
commit = " ";
word = word.slice(0, -1);
} else if (word.endsWith("\n")) {
commit = "\n";
word = word.slice(0, -1);
}
list.push({ text: word, textWithCommit: word + commit, commit });
push(word: { text: string; commit: CommitChar }) {
list.push({ ...word, textWithCommit: word.text + word.commit });
},
reset() {
list.length = 0;
Expand Down Expand Up @@ -91,19 +83,26 @@ import type {
import { Config } from "../../../src/ts/config/store";
import { Keycode } from "../../../src/ts/constants/keys";
import * as TestState from "../../../src/ts/test/test-state";
import { words as TestWords } from "../../../src/ts/test/test-words";
import {
words as TestWords,
type CommitChar,
} from "../../../src/ts/test/test-words";
import { isFunboxActiveWithProperty } from "../../../src/ts/test/funbox/list";

// tell TypeScript this is a mock
const mockedTestWordsPush = TestWords.push as Mock;

// mirror the generator: each word carries a trailing space separator unless it
// already ends with a newline, the nospace funbox is active, or it's the last
// word (the final separator is stripped once all words are generated)
function pushWords(...words: string[]): void {
const nospace = isFunboxActiveWithProperty("nospace");
words.forEach((word, i) => {
const isLast = i === words.length - 1;
const withSeparator =
isLast || nospace || word.endsWith("\n") ? word : `${word} `;
TestWords.push(withSeparator, i);
const match = /(.*?)( |\n|)$/.exec(word) as RegExpExecArray;
let commit = match[2] as CommitChar;
if (commit === "" && !isLast && !nospace) commit = " ";
mockedTestWordsPush({ text: match[1] as string, commit });
});
}

Expand Down
59 changes: 30 additions & 29 deletions frontend/__tests__/test/test-words.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,48 @@ describe("test-words", () => {
words.reset();
});

describe("push", () => {
// separators are added by the generator as a trailing space/newline; push
// splits that into the commit char while keeping the bare word as text
it("splits the trailing separator into the commit char", () => {
words.push("the ", 0);
words.push("cat ", 0);
words.push("sat", 0);
expect(words.get().map((w) => w.text)).toEqual(["the", "cat", "sat"]);
expect(words.get().map((w) => w.commit)).toEqual([" ", " ", ""]);
expect(words.get().map((w) => w.textWithCommit)).toEqual([
"the ",
"cat ",
"sat",
]);
});

it("tracks length and section indexes", () => {
words.push("a ", 3);
words.push("b", 5);
expect(words.length).toBe(2);
expect(words.get().map((w) => w.sectionIndex)).toEqual([3, 5]);
});
});

describe("removeCommitCharacterFromLastWord", () => {
it("strips a trailing space from the last word", () => {
words.push("the ", 0);
words.push("end ", 0);
words.push({
text: "the",
commit: " ",
direction: "ltr",
sectionIndex: 0,
});
words.push({
text: "end",
commit: " ",
direction: "ltr",
sectionIndex: 0,
});
words.removeCommitCharacterFromLastWord();
expect(words.get().map((w) => w.textWithCommit)).toEqual(["the ", "end"]);
});

it("strips a trailing newline from the last word", () => {
words.push("line\n", 0);
words.push({
text: "line",
commit: "\n",
direction: "ltr",
sectionIndex: 0,
});
words.removeCommitCharacterFromLastWord();
expect(words.get().map((w) => w.textWithCommit)).toEqual(["line"]);
});

it("leaves a bare last word unchanged", () => {
words.push("the ", 0);
words.push("end", 0);
words.push({
text: "the",
commit: " ",
direction: "ltr",
sectionIndex: 0,
});
words.push({
text: "end",
commit: "",
direction: "ltr",
sectionIndex: 0,
});
words.removeCommitCharacterFromLastWord();
expect(words.get().map((w) => w.textWithCommit)).toEqual(["the ", "end"]);
});
Expand Down
58 changes: 58 additions & 0 deletions frontend/__tests__/test/words-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, it, expect, vi, type Mock } from "vitest";

vi.mock("../../src/ts/test/funbox/active", () => ({
isFunboxActiveWithProperty: vi.fn(),
}));

import { isFunboxActiveWithProperty } from "../../src/ts/test/funbox/active";
import * as WordsGenerator from "../../src/ts/test/words-generator";

// tell TypeScript this is a mock
const mockedIsFunboxActiveWithProperty = isFunboxActiveWithProperty as Mock;

describe("words-generator", () => {
describe("getTextWithCommitChar", () => {
it("splits the trailing separator into the commit char", () => {
expect(WordsGenerator.getTextWithCommitChar("the ")).toEqual({
text: "the",
commit: " ",
});
expect(WordsGenerator.getTextWithCommitChar("cat\n")).toEqual({
text: "cat",
commit: "\n",
});
});

it("adds a space if there is no trailing spearator and 'nospace' funbox is off", () => {
mockedIsFunboxActiveWithProperty.mockReturnValue(false);
expect(WordsGenerator.getTextWithCommitChar("the ")).toEqual({
text: "the",
commit: " ",
});
expect(WordsGenerator.getTextWithCommitChar("cat\n")).toEqual({
text: "cat",
commit: "\n",
});
expect(WordsGenerator.getTextWithCommitChar("sat")).toEqual({
text: "sat",
commit: " ",
});
});

it("doesn't add a space if 'nospace' funbox is on", () => {
mockedIsFunboxActiveWithProperty.mockReturnValue(true);
expect(WordsGenerator.getTextWithCommitChar("the ")).toEqual({
text: "the",
commit: " ",
});
expect(WordsGenerator.getTextWithCommitChar("cat\n")).toEqual({
text: "cat",
commit: "\n",
});
expect(WordsGenerator.getTextWithCommitChar("sat")).toEqual({
text: "sat",
commit: "",
});
});
});
});
Loading
Loading