-
Notifications
You must be signed in to change notification settings - Fork 58
/
todo_list_item_test.dart
292 lines (246 loc) · 12.7 KB
/
todo_list_item_test.dart
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
@TestOn('browser')
import 'dart:async';
import 'dart:html';
import 'package:over_react/over_react.dart';
import 'package:over_react/over_react_redux.dart';
import 'package:over_react_test/over_react_test.dart';
import 'package:test/test.dart';
import 'package:time/time.dart';
import 'package:todo_client/src/actions.dart';
import 'package:todo_client/src/components/app.dart';
import 'package:todo_client/src/components/shared/todo_item_text_field.dart';
import 'package:todo_client/src/components/todo_list_item.dart';
import 'package:todo_client/src/components/user_selector.dart';
import 'package:todo_client/src/models/todo.dart';
import 'package:todo_client/src/models/user.dart';
import 'fixtures/utils.dart';
main() {
initializeComponentTests();
group('TodoListItem', () {
Ref<TodoAppComponent> appRef;
TodoListItemComponent component;
Todo model;
const localModelSynchronizationTimingReason =
'state.localModel should only be synchronized with persisted state '
'as a result of the component entering an editable state';
const modelGetterReturnsPersistedStateReason =
'When props.isEditable is false, the component\'s model getter should '
'return the value of props.model instead of state.localModel.';
setUp(() {
initializeTestStore();
appRef = createRef();
mount((ReduxProvider()..store = testStore)(
(TodoApp()..ref = appRef)(),
));
final appComponent = appRef.current;
model = testStore.state.todos.first;
component = getComponentByTestId(appComponent, 'todo_client.TodoListItem.${model.id}');
expect(component, isA<TodoListItemComponent>(), reason: 'test setup sanity check');
});
tearDown(() {
appRef = null;
component = null;
model = null;
});
Future<Null> enterEditable() async {
expect(component.props.isEditable, isFalse, reason: 'test setup sanity check');
component.redrawCount = 0;
// ignore: invalid_use_of_protected_member
component.enterEditable();
await component.didRedraw().future.timeout(10.milliseconds);
expect(component.props.isEditable, isTrue, reason: 'test setup sanity check');
}
Future<Null> exitEditable({bool saveChanges = true}) async {
expect(component.props.isEditable, isTrue, reason: 'test setup sanity check');
component.redrawCount = 0;
// ignore: invalid_use_of_protected_member
component.exitEditable(saveChanges: saveChanges);
await component.didRedraw().future.timeout(10.milliseconds);
expect(component.props.isEditable, isFalse, reason: 'test setup sanity check');
}
group('renders', () {
group('a description text field', () {
TodoItemTextFieldComponent descriptionTextFieldComponent() {
final textFieldComponent = getComponentByTestId(component, 'todo_client.TodoListItem.descriptionTextField');
expect(textFieldComponent, isA<TodoItemTextFieldComponent>(), reason: 'test setup sanity check');
return textFieldComponent;
}
InputElement descriptionTextFieldNode() => descriptionTextFieldComponent().textFieldRef.current;
group('with its props set as expected', () {
test('', () {
expect(descriptionTextFieldComponent().props.label, 'Description');
expect(descriptionTextFieldComponent().props.placeholder, 'Describe the task...');
expect(descriptionTextFieldComponent().props.value, model.description);
});
group('based on props.isEditable', () {
setUp(() {
expect(component.props.isEditable, isFalse, reason: 'test setup sanity check');
});
test('initially', () {
expect(descriptionTextFieldComponent().props.readOnly, isTrue);
expect(descriptionTextFieldComponent().props.autoFocus, isFalse);
expect(descriptionTextFieldComponent().props['inputProps']['style'], isNotNull);
});
test('when props.isEditable is updated', () async {
await enterEditable();
expect(descriptionTextFieldComponent().props.readOnly, isFalse);
expect(descriptionTextFieldComponent().props.autoFocus, isTrue);
expect(descriptionTextFieldComponent().props['inputProps']['style'], isNull);
});
});
});
group('with a props.onChange callback that updates', () {
const String newDescription = 'Something different entirely that was not the description before';
setUp(() {
expect(component.model.description, isNot(newDescription), reason: 'test setup sanity check');
});
group('state.localModel when props.isEditable is true', () {
Future<Null> storeDescriptionUpdateLocally() async {
await enterEditable();
descriptionTextFieldNode().value = newDescription;
change(descriptionTextFieldNode());
}
test('', () async {
await storeDescriptionUpdateLocally();
expect(descriptionTextFieldComponent().props.value, newDescription);
expect(component.state.localModel.description, newDescription);
expect(component.props.model.description, isNot(newDescription),
reason: 'The change should not have been persisted yet');
});
test('- and then synchronizes props.model with state.localModel'
'when exitEditable() is called', () async {
await storeDescriptionUpdateLocally();
await exitEditable();
expect(descriptionTextFieldComponent().props.value, newDescription);
expect(component.props.model.description, newDescription);
});
});
});
});
group('a notes text field', () {
TodoItemTextFieldComponent notesTextFieldComponent() {
final textFieldComponent = getComponentByTestId(component, 'todo_client.TodoListItem.notesTextField');
expect(textFieldComponent, isA<TodoItemTextFieldComponent>(), reason: 'test setup sanity check');
return textFieldComponent;
}
TextAreaElement notesTextFieldNode() => notesTextFieldComponent().textFieldRef.current;
group('with its props set as expected', () {
test('', () {
expect(notesTextFieldComponent().props.label, 'Notes');
expect(notesTextFieldComponent().props.placeholder, 'Add some notes about the task');
expect(notesTextFieldComponent().props.value, model.notes);
expect(notesTextFieldComponent().props.multiline, isTrue);
expect(notesTextFieldComponent().props['rows'], 3);
});
group('based on props.isEditable', () {
setUp(() {
expect(component.props.isEditable, isFalse, reason: 'test setup sanity check');
});
test('initially', () {
expect(notesTextFieldComponent().props.readOnly, isTrue);
});
test('when props.isEditable is updated', () async {
await enterEditable();
expect(notesTextFieldComponent().props.readOnly, isFalse);
});
});
});
group('with a props.onChange callback that updates', () {
const String newNotes = 'Something different entirely that was not the notes before';
setUp(() {
expect(component.model.notes, isNot(newNotes), reason: 'test setup sanity check');
});
group('state.localModel when props.isEditable is true', () {
Future<Null> storeDescriptionUpdateLocally() async {
await enterEditable();
notesTextFieldNode().value = newNotes;
change(notesTextFieldNode());
}
test('', () async {
await storeDescriptionUpdateLocally();
expect(notesTextFieldComponent().props.value, newNotes);
expect(component.state.localModel.notes, newNotes);
expect(component.props.model.notes, isNot(newNotes),
reason: 'The change should not have been persisted yet');
});
test('- and then synchronizes props.model with state.localModel'
'when exitEditable() is called', () async {
await storeDescriptionUpdateLocally();
await exitEditable();
expect(notesTextFieldComponent().props.value, newNotes);
expect(component.props.model.notes, newNotes);
});
});
});
});
group('a ConnectedUserSelector', () {
UserSelectorProps connectedUserSelectorProps() {
return UserSelector(getProps(getByTestId(component, 'todo_client.TodoListItem.ConnectedUserSelector')));
}
group('with props.model.assignedUserId forwarded to props.selectedUserId', () {
test('initially', () {
expect(connectedUserSelectorProps().selectedUserId, component.props.model.assignedUserId);
});
test('when props.assignedUserId is updated', () async {
final newAssignedUser = testStore.state.users[1];
expect(component.props.model.assignedUserId, isNot(newAssignedUser.id), reason: 'test setup sanity check');
testStore.dispatch(UpdateTodoAction(Todo.from(component.props.model)..assignedUserId = newAssignedUser.id));
await component.didRedraw().future.timeout(10.milliseconds);
expect(component.props.model.assignedUserId, newAssignedUser.id);
expect(connectedUserSelectorProps().selectedUserId, component.props.model.assignedUserId);
});
});
group('with a props.onUserSelect callback that updates', () {
User newAssignedUser;
group('the persisted model data immediately when props.isEditable is false', () {
Future<Null> persistSelectedUpdate() async {
newAssignedUser = testStore.state.users[1];
expect(component.model.assignedUserId, isNot(newAssignedUser.id), reason: 'test setup sanity check');
expect(component.props.isEditable, isFalse, reason: 'test setup sanity check');
connectedUserSelectorProps().onUserSelect(newAssignedUser.id);
await component.didRedraw().future.timeout(10.milliseconds);
}
test('', () async {
await persistSelectedUpdate();
expect(connectedUserSelectorProps().selectedUserId, newAssignedUser.id);
expect(component.state.localModel.assignedUserId, isNot(component.props.model.assignedUserId),
reason: localModelSynchronizationTimingReason);
expect(component.model.assignedUserId, component.props.model.assignedUserId,
reason: modelGetterReturnsPersistedStateReason);
});
test('- and then synchronizes props.model with state.localModel'
'when enterEditable() is called', () async {
await persistSelectedUpdate();
await enterEditable();
expect(connectedUserSelectorProps().selectedUserId, newAssignedUser.id);
expect(component.state.localModel.assignedUserId, component.props.model.assignedUserId);
});
});
group('state.localModel when props.isEditable is true', () {
Future<Null> storeSelectedUpdateLocally() async {
newAssignedUser = testStore.state.users[1];
await enterEditable();
expect(component.model.assignedUserId, isNot(newAssignedUser.id), reason: 'test setup sanity check');
connectedUserSelectorProps().onUserSelect(newAssignedUser.id);
}
test('', () async {
await storeSelectedUpdateLocally();
expect(connectedUserSelectorProps().selectedUserId, newAssignedUser.id);
expect(component.state.localModel.assignedUserId, newAssignedUser.id);
expect(component.props.model.assignedUserId, isNot(newAssignedUser.id),
reason: 'The change should not have been persisted yet');
});
test('- and then synchronizes props.model with state.localModel'
'when exitEditable() is called', () async {
await storeSelectedUpdateLocally();
await exitEditable();
expect(connectedUserSelectorProps().selectedUserId, newAssignedUser.id);
expect(component.props.model.assignedUserId, newAssignedUser.id);
});
});
});
});
// TODO: Add tests for cancel / save button, and the footer area itself that contains them that is only visible when the item is editable
});
});
}