-
Notifications
You must be signed in to change notification settings - Fork 2
/
todoitem_test.js
124 lines (99 loc) · 3.56 KB
/
todoitem_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
'use strict';
var todoitem = require('./todoitem');
var tododata = require('./tododata');
var assert = require('assert');
var observable = require('poochie/observable');
var pub = observable.publisher;
var eq = assert.deepEqual;
// Test double-clicking todo items.
(function(){
var todo = todoitem.todoItemImpl({
text: pub('foo'),
completed: pub(false)
});
var readModeTodo = todo.contents[0];
var writeModeTodo = todo.contents[1];
// Ensure label contents is a list, not a string.
eq(readModeTodo.contents[1].contents.get(), ['foo']);
// Test initial state.
eq(todo.attributes.className.get(), '');
// Test state after double-clicking.
readModeTodo.handlers.dblclick();
eq(todo.attributes.className.get(), 'editing');
// Test state after changing the text.
writeModeTodo.handlers.change({target: {value: 'bar'}});
eq(todo.attributes.className.get(), '');
// Test that hitting escape reverts the value of the input element.
writeModeTodo.handlers.keyup({keyCode: 27, target: {value: 'foo'}});
eq(writeModeTodo.attributes.value.get(), 'bar');
// Test that a non-escape key has no effect.
writeModeTodo.handlers.keyup({keyCode: 42, target: {value: 'foo'}});
// Test that remove() handler is not required.
readModeTodo.contents[2].handlers.click();
})();
// Test marking item as complete.
(function(){
var todo = todoitem.todoItemImpl({
text: pub('foo'),
completed: pub(false)
});
// Test initial state.
var checkbox = todo.contents[0].contents[0];
eq(checkbox.attributes.checked.get(), false);
// Test state after clicking.
checkbox.handlers.click({target: {checked: true}});
eq(checkbox.attributes.checked.get(), true);
// Test state after clicking again.
checkbox.handlers.click({target: {checked: false}});
eq(checkbox.attributes.checked.get(), false);
})();
// Test todoItem className attribute.
(function(){
var todo = todoitem.todoItemImpl({text: pub('a'), completed: pub(false)});
eq(todo.attributes.className.get(), '');
var todo2 = todoitem.todoItemImpl({text: pub('a'), completed: pub(true)});
eq(todo2.attributes.className.get(), 'completed');
})();
// Test removeItem.
(function(){
var oTodoData = pub([{text: pub('a'), completed: pub(false)}]);
var itemData = oTodoData.get()[0];
var todo = todoitem.todoItemImpl({
text: itemData.text,
completed: itemData.completed,
handlers: {remove: function() { tododata.removeItem(0, oTodoData); }}
});
var readModeTodo = todo.contents[0];
var destroyButton = readModeTodo.contents[2];
eq(oTodoData.get().length, 1);
destroyButton.handlers.click();
eq(oTodoData.get().length, 0);
})();
// Test removeItem by editing.
(function(){
var oTodoData = pub([{text: pub('a'), completed: pub(false)}]);
var itemData = oTodoData.get()[0];
var todo = todoitem.todoItemImpl({
text: itemData.text,
completed: itemData.completed,
handlers: {remove: function() { tododata.removeItem(0, oTodoData); }}
});
var readModeTodo = todo.contents[0];
var writeModeTodo = todo.contents[1];
// Ensure nothing happens if in read-mode.
writeModeTodo.handlers.change({target: {value: ''}});
eq(writeModeTodo.focus.get(), false);
eq(oTodoData.get().length, 1);
// Enter write mode.
readModeTodo.handlers.dblclick();
eq(writeModeTodo.focus.get(), true);
// Ensure empty trimmed string causes item to be removed.
writeModeTodo.handlers.change({target: {value: ' '}});
eq(oTodoData.get().length, 0);
})();
// Test that render returns somethine renderable.
(function(){
var todo = todoitem.todoItem({text: pub('a'), completed: pub(false)});
eq(typeof todo.render().render, 'function');
})();
module.exports = 'passed!';