feat: add withPersistence state, change theme state
This commit is contained in:
@@ -1,105 +1,95 @@
|
||||
import { STATE_DATA } from "$states/persisted.svelte";
|
||||
import { createState } from "$states/texts.svelte";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("texts.svelte", () => {
|
||||
describe("addText", () => {
|
||||
it("should add new text with unique ID and increment counter", () => {
|
||||
it("should add new text with unique ID", () => {
|
||||
const state = createState();
|
||||
const initialLength = state.texts.length;
|
||||
const initialIds = state.texts.map((item) => item.id);
|
||||
const maxId = Math.max(...initialIds);
|
||||
const newText = "Test text";
|
||||
|
||||
state.addText(newText);
|
||||
state.addText("Test text");
|
||||
|
||||
expect(state.texts).toHaveLength(initialLength + 1);
|
||||
expect(state.texts[initialLength].text).toBe(newText);
|
||||
expect(state.texts[initialLength].id).toBe(maxId + 1);
|
||||
expect(initialIds).not.toContain(state.texts[initialLength].id);
|
||||
expect(state.texts[initialLength].text).toBe("Test text");
|
||||
expect(state.texts[initialLength].id).toBeDefined();
|
||||
});
|
||||
|
||||
it("should not add text with empty string", () => {
|
||||
it("should not add empty text", () => {
|
||||
const state = createState();
|
||||
const initialLength = state.texts.length;
|
||||
|
||||
state.addText("");
|
||||
state.addText(" ");
|
||||
|
||||
expect(state.texts).toHaveLength(initialLength);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeText", () => {
|
||||
it("should remove text by ID and preserve other texts", () => {
|
||||
it("should remove text by ID", () => {
|
||||
const state = createState();
|
||||
const idToRemove = state.texts[0].id;
|
||||
const initialLength = state.texts.length;
|
||||
const idToRemove = state.texts[1].id;
|
||||
const otherTexts = state.texts.filter((item) => item.id !== idToRemove);
|
||||
|
||||
state.removeText(idToRemove);
|
||||
|
||||
expect(state.texts).toHaveLength(initialLength - 1);
|
||||
expect(state.texts.find((item) => item.id === idToRemove)).toBeUndefined();
|
||||
expect(state.texts).toStrictEqual(otherTexts);
|
||||
});
|
||||
|
||||
it("should not remove text with non-existent ID", () => {
|
||||
it("should not affect other texts when removing", () => {
|
||||
const state = createState();
|
||||
const initialLength = state.texts.length;
|
||||
const initialTexts = [...state.texts];
|
||||
const nonExistentId = 999999;
|
||||
const remainingTexts = state.texts.filter((item) => item.id !== state.texts[0].id);
|
||||
const idToRemove = state.texts[0].id;
|
||||
|
||||
state.removeText(nonExistentId);
|
||||
state.removeText(idToRemove);
|
||||
|
||||
expect(state.texts).toHaveLength(initialLength);
|
||||
expect(state.texts).toStrictEqual(initialTexts);
|
||||
expect(state.texts).toStrictEqual(remainingTexts);
|
||||
});
|
||||
});
|
||||
|
||||
describe("clear", () => {
|
||||
it("should clear all texts", () => {
|
||||
it("should remove all texts", () => {
|
||||
const state = createState();
|
||||
state.addText("Test 1");
|
||||
state.addText("Test 2");
|
||||
expect(state.texts.length).toBeGreaterThanOrEqual(2);
|
||||
state.addText("Test 3");
|
||||
|
||||
state.clear();
|
||||
|
||||
expect(state.texts).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("integration", () => {
|
||||
it("should maintain ID uniqueness after multiple operations", () => {
|
||||
describe("STATE_DATA", () => {
|
||||
it("should serialize and deserialize texts", () => {
|
||||
const state = createState();
|
||||
const initialIds = new Set(state.texts.map((item) => item.id));
|
||||
|
||||
state.clear();
|
||||
state.addText("First");
|
||||
state.addText("Second");
|
||||
state.removeText(state.texts[0].id);
|
||||
state.addText("Third");
|
||||
|
||||
const finalIds = state.texts.map((item) => item.id);
|
||||
const uniqueIds = new Set(finalIds);
|
||||
const data = state[STATE_DATA];
|
||||
expect(data).toEqual(["First", "Second", "Third"]);
|
||||
|
||||
expect(finalIds.length).toBe(uniqueIds.size);
|
||||
// Restore from serialized data
|
||||
state[STATE_DATA] = ["New 1", "New 2"];
|
||||
|
||||
expect(state.texts).toHaveLength(2);
|
||||
expect(state.texts[0].text).toBe("New 1");
|
||||
expect(state.texts[1].text).toBe("New 2");
|
||||
expect(state.texts[0].id).toBe(0);
|
||||
expect(state.texts[1].id).toBe(1);
|
||||
});
|
||||
|
||||
it("should handle adding and removing multiple texts", () => {
|
||||
it("should handle empty array", () => {
|
||||
const state = createState();
|
||||
const initialLength = state.texts.length;
|
||||
const addedIds: number[] = [];
|
||||
state.addText("Test");
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
state.addText(`Text ${i}`);
|
||||
addedIds.push(state.texts[state.texts.length - 1].id);
|
||||
}
|
||||
state[STATE_DATA] = [];
|
||||
|
||||
expect(state.texts).toHaveLength(initialLength + 5);
|
||||
|
||||
state.removeText(addedIds[0]);
|
||||
state.removeText(addedIds[2]);
|
||||
state.removeText(addedIds[4]);
|
||||
|
||||
expect(state.texts).toHaveLength(initialLength + 2);
|
||||
expect(state.texts).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user