-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtodomvc-test.js
188 lines (172 loc) · 5.79 KB
/
todomvc-test.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { create } from "microstates";
import TodoMVC from "../todomvc";
let all = create(TodoMVC, {
todos: [
{ id: 1, text: "Make initial commit", completed: false },
{ id: 2, text: "Write readme", completed: false },
{ id: 3, text: "Release microstates", completed: false }
]
});
let empty = create(TodoMVC);
describe("queries", () => {
describe("hasTodos", () => {
it("is true when todos are present", () => {
expect(all.hasTodos).toBe(true);
});
it("is false when there are no todos", () => {
expect(empty.hasTodos).toBe(false);
});
});
describe("isAllComplete", () => {
it("is false when there are no items", () => {
expect(empty.isAllComplete).toBe(false);
});
it("is false when items are present but items are not complete", () => {
expect(all.isAllComplete).toBe(false);
});
it("is true when all items are complete", () => {
expect(all.toggleAll().isAllComplete).toBe(true);
});
});
describe("hasCompleted", () => {
it("is false when there are no items", () => {
expect(empty.hasCompleted).toBe(false);
});
it("is true when one item is completed", () => {
let [, second] = all.todos;
expect(second.completed.set(true).hasCompleted).toBe(true);
});
});
describe("filtered", () => {
describe("set to ''", () => {
it("does not filter results", () => {
expect(all.filtered).toHaveLength(3);
});
});
describe("set to 'show_completed'", () => {
let todomvc = all.filter.set("show_completed");
it("has no items to show", () => {
expect(todomvc.filtered).toHaveLength(0);
});
it("has 2nd item after status of second item is changed", () => {
let [, second] = todomvc.todos;
let { filtered } = second.completed.set(true);
let [result] = filtered;
expect(filtered).toHaveLength(1);
expect(result.id.state).toBe(2);
expect(result.text.state).toBe("Write readme");
expect(result.completed.state).toBe(true);
});
});
describe("set to 'show_active'", () => {
let todomvc = all.filter.set("show_active");
it("has 3 items", () => {
expect(todomvc.filtered).toHaveLength(3);
});
it("has 2 items after status of first item is changed", () => {
let [todo] = todomvc.todos;
let {
filtered: [first, second]
} = todo.completed.set(true);
expect(first.id.state).toBe(2);
expect(first.text.state).toBe("Write readme");
expect(first.completed.state).toBe(false);
expect(second.id.state).toBe(3);
expect(second.text.state).toBe("Release microstates");
expect(second.completed.state).toBe(false);
});
});
});
describe("filters", () => {
let [, second] = all.todos;
let todomvc = second.completed.set(true);
it("has 3 filters", () => {
expect(todomvc.filters).toMatchObject([
{
key: "",
label: "All",
selected: true,
select: expect.any(Function)
},
{
key: "show_active",
label: "Active",
selected: false,
select: expect.any(Function)
},
{
key: "show_completed",
label: "Completed",
selected: false,
select: expect.any(Function)
}
]);
});
it("sets filter to show_active when active filter is selected", () => {
expect(todomvc.filters[1].select().filter.state).toBe("show_active");
});
it("sets filter to show_completed when completed filter is selected", () => {
expect(todomvc.filters[2].select().filter.state).toBe("show_completed");
});
});
});
describe("transitions", () => {
describe("insertNewTodo", () => {
it("doesn't add a new item when newTodo is empty", () => {
expect(all.insertNewTodo().todos).toHaveLength(3);
});
let changedNewTodo = all.newTodo.set("write more tests");
let added = changedNewTodo.insertNewTodo();
it("adds an item when newTodo is set", () => {
expect(added.todos).toHaveLength(4);
});
it("adds item to the end of the list", () => {
let [,,,last] = added.todos;
expect(last.text.state).toBe("write more tests");
expect(last.id.state).toBe(4);
});
it("clears newTodo after item is added", () => {
expect(added.newTodo.state).toEqual("");
});
});
describe("clearCompleted", () => {
let [,second] = all.todos;
let oneIsCompleted = second.completed.set(true);
it("removes completed item from todos", () => {
let [first, third] = oneIsCompleted.clearCompleted().todos;
expect(first.id.state).toBe(1);
expect(third.id.state).toBe(3);
});
});
describe("toggleAll", () => {
it("makes all items completed", () => {
let none = all.toggleAll();
let [first, second, third] = none.todos;
expect(first.completed.state).toBe(true);
expect(second.completed.state).toBe(true);
expect(third.completed.state).toBe(true);
});
});
describe("Todo.edit", () => {
let [first] = all.todos;
let editing = first.edit();
it("puts the item into editing mode", () => {
let [first, second, third] = editing.todos;
expect(first.editing.state).toBe(true);
expect(second.editing.state).toBe(false);
expect(third.editing.state).toBe(false);
});
});
describe("Todo.save", () => {
let [first] = all.todos;
let [,second] = first.edit().todos;
let editing = second.edit();
it("stops editing", () => {
let [first] = editing.todos;
let [saved, stillEditing, unchanged] = first.save().todos
expect(saved.editing.state).toBe(false);
expect(stillEditing.editing.state).toBe(true);
expect(unchanged.editing.state).toBe(false);
});
});
});