summaryrefslogtreecommitdiffhomepage
path: root/src/features/surface-host/logic/todo.test.ts
blob: 66ff03670995c032f7ab3fc1b69b46b0da027866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, expect, it } from "vitest";
import { parseTodoPayload, type TodoItem } from "./todo";

const item = (content: string, status: TodoItem["status"] = "pending"): TodoItem => ({
  content,
  status,
});

describe("parseTodoPayload", () => {
  it("parses a well-formed payload with items", () => {
    const data = parseTodoPayload({
      todos: [
        item("Write tests", "in_progress"),
        item("Ship it", "pending"),
        item("Read docs", "completed"),
      ],
    });
    expect(data).toEqual({
      todos: [
        item("Write tests", "in_progress"),
        item("Ship it", "pending"),
        item("Read docs", "completed"),
      ],
    });
  });

  it("parses an empty-todos payload", () => {
    expect(parseTodoPayload({ todos: [] })).toEqual({ todos: [] });
  });

  it("preserves item order", () => {
    const data = parseTodoPayload({ todos: [item("a"), item("b"), item("c")] });
    expect(data?.todos.map((t) => t.content)).toEqual(["a", "b", "c"]);
  });

  it("accepts all four status values", () => {
    const data = parseTodoPayload({
      todos: [
        item("p", "pending"),
        item("i", "in_progress"),
        item("c", "completed"),
        item("x", "cancelled"),
      ],
    });
    expect(data?.todos.map((t) => t.status)).toEqual([
      "pending",
      "in_progress",
      "completed",
      "cancelled",
    ]);
  });

  it.each([
    ["null", null],
    ["a number", 7],
    ["a string", "nope"],
    ["missing todos key", { foo: [] }],
    ["todos not an array", { todos: "x" }],
    ["entry not an object", { todos: ["x"] }],
    ["entry missing content", { todos: [{ status: "pending" }] }],
    ["entry with non-string content", { todos: [{ content: 1, status: "pending" }] }],
    ["entry missing status", { todos: [{ content: "x" }] }],
    ["entry with invalid status", { todos: [item("x", "done" as never)] }],
  ])("returns null for invalid payload: %s", (_label, payload) => {
    expect(parseTodoPayload(payload)).toBeNull();
  });
});