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

Unified 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, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/webui/md_bookmarks/reducers_test.js
diff --git a/chrome/test/data/webui/md_bookmarks/reducers_test.js b/chrome/test/data/webui/md_bookmarks/reducers_test.js
new file mode 100644
index 0000000000000000000000000000000000000000..74e828be14de0cc864ad7783845a93df9da672aa
--- /dev/null
+++ b/chrome/test/data/webui/md_bookmarks/reducers_test.js
@@ -0,0 +1,257 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+suite('selection state', function() {
+ var initialState;
+
+ function select(items, anchor, add) {
+ return {
+ name: 'select-items',
+ add: add,
+ anchor: anchor,
+ items: items,
+ };
+ }
+
+ setup(function() {
+ initialState = {
+ anchor: null,
+ count: 0,
+ items: 1,
+ };
+ });
+
+ test('can select an item', function() {
+ var nextState;
+ var action;
+
+ action = select(['1'], '1', false);
+ nextState = bookmarks.SelectionState.updateSelection(initialState, action);
+
+ assertDeepEquals({'1': true}, nextState.items);
+ assertEquals('1', nextState.anchor);
+
+ // Replace current selection.
+ action = select(['2'], '2', false);
+ nextState = bookmarks.SelectionState.updateSelection(nextState, action);
+ assertDeepEquals({'2': true}, nextState.items);
+ assertEquals('2', nextState.anchor);
+
+ // Add to current selection.
+ action = select(['3'], '3', true);
+ nextState = bookmarks.SelectionState.updateSelection(nextState, action);
+ assertDeepEquals({'2': true, '3': true}, nextState.items);
+ assertEquals('3', nextState.anchor);
+ });
+
+ test('can select multiple items', function() {
+ var nextState;
+ var action;
+
+ action = select(['1', '2', '3'], '3', false);
+ nextState = bookmarks.SelectionState.updateSelection(initialState, action);
+ assertDeepEquals({'1': true, '2': true, '3': true}, nextState.items);
+
+ action = select(['3', '4'], '4', true);
+ nextState = bookmarks.SelectionState.updateSelection(nextState, action);
+ assertDeepEquals(
+ {'1': true, '2': true, '3': true, '4': true}, nextState.items);
+ });
+
+ test('is cleared when selected folder changes', function() {
+ var nextState;
+ var action;
+
+ action = select(['1', '2', '3'], '3', false);
+ nextState = bookmarks.SelectionState.updateSelection(initialState, action);
+
+ action = bookmarks.actions.selectFolder('2');
+ nextState = bookmarks.SelectionState.updateSelection(nextState, action);
+ assertDeepEquals({}, nextState.items);
+ });
+
+ test('is cleared when search finished', function() {
+ var nextState;
+ var action;
+
+ action = select(['1', '2', '3'], '3', false);
+ nextState = bookmarks.SelectionState.updateSelection(initialState, action);
+
+ action = bookmarks.actions.setSearchResults([createItem('2')]);
+ nextState = bookmarks.SelectionState.updateSelection(nextState, action);
+ assertDeepEquals({}, nextState.items);
+ });
+});
+
+suite('closed folder state', function() {
+ var nodes;
+ var initialState;
+
+ setup(function() {
+ nodes = testTree(createFolder('0', [
+ createFolder('1', []),
+ ]));
+ initialState = {};
+ });
+
+ test('toggle folder open state', function() {
+ var action = bookmarks.actions.changeFolderOpen('1', false);
+ var nextState = bookmarks.ClosedFolderState.updateClosedFolders(
+ initialState, action, nodes);
+ assertTrue(nextState['1']);
+ assertFalse(!!nextState['0']);
+ });
+
+ test('select folder with closed parent', function() {
+ var action;
+ var nextState;
+ // Close '0'
+ action = bookmarks.actions.changeFolderOpen('0', false);
+ nextState = bookmarks.ClosedFolderState.updateClosedFolders(
+ initialState, action, nodes);
+ assertTrue(nextState['0']);
+
+ // Should re-open when '1' is selected.
+ action = bookmarks.actions.selectFolder('1');
+ nextState = bookmarks.ClosedFolderState.updateClosedFolders(
+ nextState, action, nodes);
+ assertFalse(nextState['0']);
+ });
+});
+
+suite('selected folder', function() {
+ var nodes;
+ var initialState;
+
+ setup(function() {
+ nodes = testTree(createFolder('0', [
+ createFolder('1', []),
+ ]));
+
+ initialState = '0';
+ });
+
+ test('updates from selectFolder action', function() {
+ var action = bookmarks.actions.selectFolder('1');
+ var newState = bookmarks.SelectedFolderState.updateSelectedFolder(
+ initialState, action, nodes);
+ assertEquals('1', newState);
+ });
+
+ test('updates when parent of selected folder is closed', function() {
+ var action;
+ var newState;
+
+ action = bookmarks.actions.selectFolder('1');
+ newState = bookmarks.SelectedFolderState.updateSelectedFolder(
+ initialState, action, nodes);
+
+ action = bookmarks.actions.changeFolderOpen('0', false);
+ newState = bookmarks.SelectedFolderState.updateSelectedFolder(
+ newState, action, nodes);
+ assertEquals('0', newState);
+ });
+});
+
+suite('node state', function() {
+ var initialState;
+
+ setup(function() {
+ initialState = testTree(createFolder('0', [
+ createFolder(
+ '1',
+ [
+ createItem('2', {title: 'a', url: 'a.com'}),
+ createItem('3'),
+ createFolder('4', []),
+ ]),
+ createFolder('5', []),
+ ]));
+ });
+
+ test('updates when a node is edited', function() {
+ var action = bookmarks.actions.editBookmark('2', {title: 'b'});
+ var nextState = bookmarks.NodeState.updateNodes(initialState, action);
+
+ assertEquals('b', nextState['2'].title);
+ assertEquals('a.com', nextState['2'].url);
+
+ action = bookmarks.actions.editBookmark('2', {title: 'c', url: 'c.com'});
+ nextState = bookmarks.NodeState.updateNodes(nextState, action);
+
+ assertEquals('c', nextState['2'].title);
+ assertEquals('c.com', nextState['2'].url);
+ });
+
+ test('updates when a node is deleted', function() {
+ var action = bookmarks.actions.removeBookmark('3', '1', 1);
+ var nextState = bookmarks.NodeState.updateNodes(initialState, action);
+
+ assertDeepEquals(['2', '4'], nextState['1'].children);
+
+ // TODO(tsergeant): Deleted nodes should be removed from the nodes map
+ // entirely.
+ });
+});
+
+suite('search', function() {
+ var initialState;
+
+ setup(function() {
+ // Search touches a few different things, so we test using the entire state:
+ initialState = bookmarks.util.createEmptyState();
+ initialState.nodes = testTree(createFolder('0', [
+ createFolder(
+ '1',
+ [
+ createItem('2'),
+ ]),
+ ]));
+ });
+
+ test('updates when search is started and finished', function() {
+ var action;
+ var nextState;
+
+ action = bookmarks.actions.selectFolder('0');
+ nextState = bookmarks.reduceAction(initialState, action);
+
+ action = bookmarks.actions.setSearchTerm('test');
+ nextState = bookmarks.reduceAction(nextState, action);
+
+ assertEquals('test', nextState.search.term);
+ assertTrue(nextState.search.inProgress);
+ // UI should not have changed yet:
+ assertEquals('0', nextState.selectedFolder);
+ assertDeepEquals(['1'], bookmarks.util.getDisplayedList(nextState));
+
+ action =
+ bookmarks.actions.setSearchResults([createItem('1'), createItem('2')]);
+ var searchedState = bookmarks.reduceAction(nextState, action);
+
+ assertFalse(searchedState.search.inProgress);
+ // UI changes once search results arrive:
+ assertEquals(null, searchedState.selectedFolder);
+ assertDeepEquals(
+ ['1', '2'], bookmarks.util.getDisplayedList(searchedState));
+
+ // Case 1: Clear search by setting an empty search term.
+ action = bookmarks.actions.setSearchTerm('');
+ var clearedState = bookmarks.reduceAction(searchedState, action);
+
+ assertEquals('1', clearedState.selectedFolder);
+ assertDeepEquals(['2'], bookmarks.util.getDisplayedList(clearedState));
+ assertEquals('', clearedState.search.term);
+ assertDeepEquals([], clearedState.search.results);
+
+ // Case 2: Clear search by selecting a new folder.
+ action = bookmarks.actions.selectFolder('0');
+ var selectedState = bookmarks.reduceAction(searchedState, action);
+
+ assertEquals('0', selectedState.selectedFolder);
+ assertDeepEquals(['1'], bookmarks.util.getDisplayedList(selectedState));
+ assertEquals('', selectedState.search.term);
+ assertDeepEquals([], selectedState.search.results);
+ });
+});
« 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