Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: chrome/test/data/webui/md_bookmarks/reducers_test.js

Issue 2704983002: MD Bookmarks: Proof-of-concept reimplementation of data storage/binding layer (Closed)
Patch Set: Add doc comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 suite('selection state', function() {
6 var initialState;
7
8 function select(items, anchor, add) {
9 return {
10 name: 'select-items',
11 add: add,
12 anchor: anchor,
13 items: items,
14 };
15 }
16
17 setup(function() {
18 initialState = {
19 anchor: null,
20 count: 0,
21 items: 1,
22 };
23 });
24
25 test('can select an item', function() {
26 var nextState;
27 var action;
28
29 action = select(['1'], '1', false);
30 nextState = bookmarks.SelectionState.updateSelection(initialState, action);
31
32 assertDeepEquals({'1': true}, nextState.items);
33 assertEquals('1', nextState.anchor);
34
35 // Replace current selection.
36 action = select(['2'], '2', false);
37 nextState = bookmarks.SelectionState.updateSelection(nextState, action);
38 assertDeepEquals({'2': true}, nextState.items);
39 assertEquals('2', nextState.anchor);
40
41 // Add to current selection.
42 action = select(['3'], '3', true);
43 nextState = bookmarks.SelectionState.updateSelection(nextState, action);
44 assertDeepEquals({'2': true, '3': true}, nextState.items);
45 assertEquals('3', nextState.anchor);
46 });
47
48 test('can select multiple items', function() {
49 var nextState;
50 var action;
51
52 action = select(['1', '2', '3'], '3', false);
53 nextState = bookmarks.SelectionState.updateSelection(initialState, action);
54 assertDeepEquals({'1': true, '2': true, '3': true}, nextState.items);
55
56 action = select(['3', '4'], '4', true);
57 nextState = bookmarks.SelectionState.updateSelection(nextState, action);
58 assertDeepEquals(
59 {'1': true, '2': true, '3': true, '4': true}, nextState.items);
60 });
61
62 test('is cleared when selected folder changes', function() {
63 var nextState;
64 var action;
65
66 action = select(['1', '2', '3'], '3', false);
67 nextState = bookmarks.SelectionState.updateSelection(initialState, action);
68
69 action = bookmarks.actions.selectFolder('2');
70 nextState = bookmarks.SelectionState.updateSelection(nextState, action);
71 assertDeepEquals({}, nextState.items);
72 });
73
74 test('is cleared when search finished', function() {
75 var nextState;
76 var action;
77
78 action = select(['1', '2', '3'], '3', false);
79 nextState = bookmarks.SelectionState.updateSelection(initialState, action);
80
81 action = bookmarks.actions.setSearchResults([createItem('2')]);
82 nextState = bookmarks.SelectionState.updateSelection(nextState, action);
83 assertDeepEquals({}, nextState.items);
84 });
85 });
86
87 suite('closed folder state', function() {
88 var nodes;
89 var initialState;
90
91 setup(function() {
92 nodes = testTree(createFolder('0', [
93 createFolder('1', []),
94 ]));
95 initialState = {};
96 });
97
98 test('toggle folder open state', function() {
99 var action = bookmarks.actions.changeFolderOpen('1', false);
100 var nextState = bookmarks.ClosedFolderState.updateClosedFolders(
101 initialState, action, nodes);
102 assertTrue(nextState['1']);
103 assertFalse(!!nextState['0']);
104 });
105
106 test('select folder with closed parent', function() {
107 var action;
108 var nextState;
109 // Close '0'
110 action = bookmarks.actions.changeFolderOpen('0', false);
111 nextState = bookmarks.ClosedFolderState.updateClosedFolders(
112 initialState, action, nodes);
113 assertTrue(nextState['0']);
114
115 // Should re-open when '1' is selected.
116 action = bookmarks.actions.selectFolder('1');
117 nextState = bookmarks.ClosedFolderState.updateClosedFolders(
118 nextState, action, nodes);
119 assertFalse(nextState['0']);
120 });
121 });
122
123 suite('selected folder', function() {
124 var nodes;
125 var initialState;
126
127 setup(function() {
128 nodes = testTree(createFolder('0', [
129 createFolder('1', []),
130 ]));
131
132 initialState = '0';
133 });
134
135 test('updates from selectFolder action', function() {
136 var action = bookmarks.actions.selectFolder('1');
137 var newState = bookmarks.SelectedFolderState.updateSelectedFolder(
138 initialState, action, nodes);
139 assertEquals('1', newState);
140 });
141
142 test('updates when parent of selected folder is closed', function() {
143 var action;
144 var newState;
145
146 action = bookmarks.actions.selectFolder('1');
147 newState = bookmarks.SelectedFolderState.updateSelectedFolder(
148 initialState, action, nodes);
149
150 action = bookmarks.actions.changeFolderOpen('0', false);
151 newState = bookmarks.SelectedFolderState.updateSelectedFolder(
152 newState, action, nodes);
153 assertEquals('0', newState);
154 });
155 });
156
157 suite('node state', function() {
158 var initialState;
159
160 setup(function() {
161 initialState = testTree(createFolder('0', [
162 createFolder(
163 '1',
164 [
165 createItem('2', {title: 'a', url: 'a.com'}),
166 createItem('3'),
167 createFolder('4', []),
168 ]),
169 createFolder('5', []),
170 ]));
171 });
172
173 test('updates when a node is edited', function() {
174 var action = bookmarks.actions.editBookmark('2', {title: 'b'});
175 var nextState = bookmarks.NodeState.updateNodes(initialState, action);
176
177 assertEquals('b', nextState['2'].title);
178 assertEquals('a.com', nextState['2'].url);
179
180 action = bookmarks.actions.editBookmark('2', {title: 'c', url: 'c.com'});
181 nextState = bookmarks.NodeState.updateNodes(nextState, action);
182
183 assertEquals('c', nextState['2'].title);
184 assertEquals('c.com', nextState['2'].url);
185 });
186
187 test('updates when a node is deleted', function() {
188 var action = bookmarks.actions.removeBookmark('3', '1', 1);
189 var nextState = bookmarks.NodeState.updateNodes(initialState, action);
190
191 assertDeepEquals(['2', '4'], nextState['1'].children);
192
193 // TODO(tsergeant): Deleted nodes should be removed from the nodes map
194 // entirely.
195 });
196 });
197
198 suite('search', function() {
199 var initialState;
200
201 setup(function() {
202 // Search touches a few different things, so we test using the entire state:
203 initialState = bookmarks.util.createEmptyState();
204 initialState.nodes = testTree(createFolder('0', [
205 createFolder(
206 '1',
207 [
208 createItem('2'),
209 ]),
210 ]));
211 });
212
213 test('updates when search is started and finished', function() {
214 var action;
215 var nextState;
216
217 action = bookmarks.actions.selectFolder('0');
218 nextState = bookmarks.reduceAction(initialState, action);
219
220 action = bookmarks.actions.setSearchTerm('test');
221 nextState = bookmarks.reduceAction(nextState, action);
222
223 assertEquals('test', nextState.search.term);
224 assertTrue(nextState.search.inProgress);
225 // UI should not have changed yet:
226 assertEquals('0', nextState.selectedFolder);
227 assertDeepEquals(['1'], bookmarks.util.getDisplayedList(nextState));
228
229 action =
230 bookmarks.actions.setSearchResults([createItem('1'), createItem('2')]);
231 var searchedState = bookmarks.reduceAction(nextState, action);
232
233 assertFalse(searchedState.search.inProgress);
234 // UI changes once search results arrive:
235 assertEquals(null, searchedState.selectedFolder);
236 assertDeepEquals(
237 ['1', '2'], bookmarks.util.getDisplayedList(searchedState));
238
239 // Case 1: Clear search by setting an empty search term.
240 action = bookmarks.actions.setSearchTerm('');
241 var clearedState = bookmarks.reduceAction(searchedState, action);
242
243 assertEquals('1', clearedState.selectedFolder);
244 assertDeepEquals(['2'], bookmarks.util.getDisplayedList(clearedState));
245 assertEquals('', clearedState.search.term);
246 assertDeepEquals([], clearedState.search.results);
247
248 // Case 2: Clear search by selecting a new folder.
249 action = bookmarks.actions.selectFolder('0');
250 var selectedState = bookmarks.reduceAction(searchedState, action);
251
252 assertEquals('0', selectedState.selectedFolder);
253 assertDeepEquals(['1'], bookmarks.util.getDisplayedList(selectedState));
254 assertEquals('', selectedState.search.term);
255 assertDeepEquals([], selectedState.search.results);
256 });
257 });
OLDNEW
« no previous file with comments | « chrome/test/data/webui/md_bookmarks/md_bookmarks_browsertest.js ('k') | chrome/test/data/webui/md_bookmarks/sidebar_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698