| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** |
| 6 * Helper method for creating a 'remove-bookmark' action. |
| 7 * @param {string} id |
| 8 * @param {string} parentId |
| 9 * @param {number} index |
| 10 * @param {!Array<string>} descendants |
| 11 * @return {!Action} |
| 12 */ |
| 13 function remove(id, parentId, index, descendants) { |
| 14 descendants.push(id); |
| 15 return bookmarks.actions.removeBookmarkSubtree( |
| 16 id, parentId, index, new Set(descendants)); |
| 17 } |
| 18 |
| 5 suite('selection state', function() { | 19 suite('selection state', function() { |
| 6 var state; | 20 var state; |
| 7 var action; | 21 var action; |
| 8 | 22 |
| 9 function select(items, anchor, add) { | |
| 10 return { | |
| 11 name: 'select-items', | |
| 12 add: add, | |
| 13 anchor: anchor, | |
| 14 items: items, | |
| 15 }; | |
| 16 } | |
| 17 | |
| 18 setup(function() { | 23 setup(function() { |
| 19 state = { | 24 state = { |
| 20 anchor: null, | 25 anchor: null, |
| 21 items: {}, | 26 items: {}, |
| 22 }; | 27 }; |
| 23 }); | 28 }); |
| 24 | 29 |
| 25 test('can select an item', function() { | 30 test('can select an item', function() { |
| 26 action = select(['1'], '1', false); | 31 action = bookmarks.actions.selectItemSet(['1'], '1', false); |
| 27 state = bookmarks.SelectionState.updateSelection(state, action); | 32 state = bookmarks.SelectionState.updateSelection(state, action); |
| 28 | 33 |
| 29 assertDeepEquals(['1'], normalizeSet(state.items)); | 34 assertDeepEquals(['1'], normalizeSet(state.items)); |
| 30 assertEquals('1', state.anchor); | 35 assertEquals('1', state.anchor); |
| 31 | 36 |
| 32 // Replace current selection. | 37 // Replace current selection. |
| 33 action = select(['2'], '2', false); | 38 action = bookmarks.actions.selectItemSet(['2'], '2', false); |
| 34 state = bookmarks.SelectionState.updateSelection(state, action); | 39 state = bookmarks.SelectionState.updateSelection(state, action); |
| 35 assertDeepEquals(['2'], normalizeSet(state.items)); | 40 assertDeepEquals(['2'], normalizeSet(state.items)); |
| 36 assertEquals('2', state.anchor); | 41 assertEquals('2', state.anchor); |
| 37 | 42 |
| 38 // Add to current selection. | 43 // Add to current selection. |
| 39 action = select(['3'], '3', true); | 44 action = bookmarks.actions.selectItemSet(['3'], '3', true); |
| 40 state = bookmarks.SelectionState.updateSelection(state, action); | 45 state = bookmarks.SelectionState.updateSelection(state, action); |
| 41 assertDeepEquals(['2', '3'], normalizeSet(state.items)); | 46 assertDeepEquals(['2', '3'], normalizeSet(state.items)); |
| 42 assertEquals('3', state.anchor); | 47 assertEquals('3', state.anchor); |
| 43 }); | 48 }); |
| 44 | 49 |
| 45 test('can select multiple items', function() { | 50 test('can select multiple items', function() { |
| 46 action = select(['1', '2', '3'], '3', false); | 51 action = bookmarks.actions.selectItemSet(['1', '2', '3'], '3', false); |
| 47 state = bookmarks.SelectionState.updateSelection(state, action); | 52 state = bookmarks.SelectionState.updateSelection(state, action); |
| 48 assertDeepEquals(['1', '2', '3'], normalizeSet(state.items)); | 53 assertDeepEquals(['1', '2', '3'], normalizeSet(state.items)); |
| 49 | 54 |
| 50 action = select(['3', '4'], '4', true); | 55 action = bookmarks.actions.selectItemSet(['3', '4'], '4', true); |
| 51 state = bookmarks.SelectionState.updateSelection(state, action); | 56 state = bookmarks.SelectionState.updateSelection(state, action); |
| 52 assertDeepEquals(['1', '2', '3', '4'], normalizeSet(state.items)); | 57 assertDeepEquals(['1', '2', '3', '4'], normalizeSet(state.items)); |
| 53 }); | 58 }); |
| 54 | 59 |
| 55 test('is cleared when selected folder changes', function() { | 60 test('is cleared when selected folder changes', function() { |
| 56 action = select(['1', '2', '3'], '3', false); | 61 action = bookmarks.actions.selectItemSet(['1', '2', '3'], '3', false); |
| 57 state = bookmarks.SelectionState.updateSelection(state, action); | 62 state = bookmarks.SelectionState.updateSelection(state, action); |
| 58 | 63 |
| 59 action = bookmarks.actions.selectFolder('2'); | 64 action = bookmarks.actions.selectFolder('2'); |
| 60 state = bookmarks.SelectionState.updateSelection(state, action); | 65 state = bookmarks.SelectionState.updateSelection(state, action); |
| 61 assertDeepEquals({}, state.items); | 66 assertDeepEquals({}, state.items); |
| 62 }); | 67 }); |
| 63 | 68 |
| 64 test('is cleared when search finished', function() { | 69 test('is cleared when search finished', function() { |
| 65 action = select(['1', '2', '3'], '3', false); | 70 action = bookmarks.actions.selectItemSet(['1', '2', '3'], '3', false); |
| 66 state = bookmarks.SelectionState.updateSelection(state, action); | 71 state = bookmarks.SelectionState.updateSelection(state, action); |
| 67 | 72 |
| 68 action = bookmarks.actions.setSearchResults(['2']); | 73 action = bookmarks.actions.setSearchResults(['2']); |
| 69 state = bookmarks.SelectionState.updateSelection(state, action); | 74 state = bookmarks.SelectionState.updateSelection(state, action); |
| 70 assertDeepEquals({}, state.items); | 75 assertDeepEquals({}, state.items); |
| 71 }); | 76 }); |
| 72 | 77 |
| 73 test('is cleared when search cleared', function() { | 78 test('is cleared when search cleared', function() { |
| 74 action = select(['1', '2', '3'], '3', false); | 79 action = bookmarks.actions.selectItemSet(['1', '2', '3'], '3', false); |
| 75 state = bookmarks.SelectionState.updateSelection(state, action); | 80 state = bookmarks.SelectionState.updateSelection(state, action); |
| 76 | 81 |
| 77 action = bookmarks.actions.clearSearch(); | 82 action = bookmarks.actions.clearSearch(); |
| 78 state = bookmarks.SelectionState.updateSelection(state, action); | 83 state = bookmarks.SelectionState.updateSelection(state, action); |
| 79 assertDeepEquals({}, state.items); | 84 assertDeepEquals({}, state.items); |
| 80 }); | 85 }); |
| 81 | 86 |
| 82 test('deselect items', function() { | 87 test('deselect items', function() { |
| 83 action = select(['1', '2', '3'], '3', false); | 88 action = bookmarks.actions.selectItemSet(['1', '2', '3'], '3', false); |
| 84 state = bookmarks.SelectionState.updateSelection(state, action); | 89 state = bookmarks.SelectionState.updateSelection(state, action); |
| 85 | 90 |
| 86 action = bookmarks.actions.deselectItems(); | 91 action = bookmarks.actions.deselectItems(); |
| 87 state = bookmarks.SelectionState.updateSelection(state, action); | 92 state = bookmarks.SelectionState.updateSelection(state, action); |
| 88 assertDeepEquals({}, state.items); | 93 assertDeepEquals({}, state.items); |
| 89 }); | 94 }); |
| 90 | 95 |
| 91 test('deselects items when they are deleted', function() { | 96 test('deselects items when they are deleted', function() { |
| 92 var nodeList = testTree(createFolder('0', [ | 97 var nodeList = testTree(createFolder('0', [ |
| 93 createFolder( | 98 createFolder( |
| 94 '1', | 99 '1', |
| 95 [ | 100 [ |
| 96 createItem('2'), | 101 createItem('2'), |
| 97 createItem('3'), | 102 createItem('3'), |
| 98 createItem('4'), | 103 createItem('4'), |
| 99 ]), | 104 ]), |
| 100 createItem('5'), | 105 createItem('5'), |
| 101 ])); | 106 ])); |
| 102 | 107 |
| 103 action = select(['2', '4', '5'], '4', false); | 108 action = bookmarks.actions.selectItemSet(['2', '4', '5'], '4', false); |
| 104 state = bookmarks.SelectionState.updateSelection(state, action); | 109 state = bookmarks.SelectionState.updateSelection(state, action); |
| 105 | 110 |
| 106 action = bookmarks.actions.removeBookmark('1', '0', 0, nodeList); | 111 action = remove('1', '0', 0, ['2', '3', '4']); |
| 107 state = bookmarks.SelectionState.updateSelection(state, action); | 112 state = bookmarks.SelectionState.updateSelection(state, action); |
| 108 | 113 |
| 109 assertDeepEquals(['5'], normalizeSet(state.items)); | 114 assertDeepEquals(['5'], normalizeSet(state.items)); |
| 110 assertEquals(null, state.anchor); | 115 assertEquals(null, state.anchor); |
| 111 }); | 116 }); |
| 112 }); | 117 }); |
| 113 | 118 |
| 114 suite('closed folder state', function() { | 119 suite('closed folder state', function() { |
| 115 var nodes; | 120 var nodes; |
| 116 var state; | 121 var state; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 state, action, nodes); | 210 state, action, nodes); |
| 206 assertEquals('1', state); | 211 assertEquals('1', state); |
| 207 }); | 212 }); |
| 208 | 213 |
| 209 test('selects ancestor when selected folder is deleted', function() { | 214 test('selects ancestor when selected folder is deleted', function() { |
| 210 action = bookmarks.actions.selectFolder('3'); | 215 action = bookmarks.actions.selectFolder('3'); |
| 211 state = bookmarks.SelectedFolderState.updateSelectedFolder( | 216 state = bookmarks.SelectedFolderState.updateSelectedFolder( |
| 212 state, action, nodes); | 217 state, action, nodes); |
| 213 | 218 |
| 214 // Delete the selected folder: | 219 // Delete the selected folder: |
| 215 action = bookmarks.actions.removeBookmark('3', '2', 0, nodes); | 220 action = remove('3', '2', 0, []); |
| 216 state = bookmarks.SelectedFolderState.updateSelectedFolder( | 221 state = bookmarks.SelectedFolderState.updateSelectedFolder( |
| 217 state, action, nodes); | 222 state, action, nodes); |
| 218 | 223 |
| 219 assertEquals('2', state); | 224 assertEquals('2', state); |
| 220 | 225 |
| 221 action = bookmarks.actions.selectFolder('4'); | 226 action = bookmarks.actions.selectFolder('4'); |
| 222 state = bookmarks.SelectedFolderState.updateSelectedFolder( | 227 state = bookmarks.SelectedFolderState.updateSelectedFolder( |
| 223 state, action, nodes); | 228 state, action, nodes); |
| 224 | 229 |
| 225 // Delete an ancestor of the selected folder: | 230 // Delete an ancestor of the selected folder: |
| 226 action = bookmarks.actions.removeBookmark('2', '1', 0, nodes); | 231 action = remove('2', '1', 0, ['3']); |
| 227 state = bookmarks.SelectedFolderState.updateSelectedFolder( | 232 state = bookmarks.SelectedFolderState.updateSelectedFolder( |
| 228 state, action, nodes); | 233 state, action, nodes); |
| 229 | 234 |
| 230 assertEquals('1', state); | 235 assertEquals('1', state); |
| 231 }); | 236 }); |
| 232 }); | 237 }); |
| 233 | 238 |
| 234 suite('node state', function() { | 239 suite('node state', function() { |
| 235 var state; | 240 var state; |
| 236 var action; | 241 var action; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 | 303 |
| 299 action = bookmarks.actions.createBookmark(item.id, item); | 304 action = bookmarks.actions.createBookmark(item.id, item); |
| 300 state = bookmarks.NodeState.updateNodes(state, action); | 305 state = bookmarks.NodeState.updateNodes(state, action); |
| 301 | 306 |
| 302 assertEquals('6', state['7'].parentId); | 307 assertEquals('6', state['7'].parentId); |
| 303 assertEquals(undefined, state['7'].children); | 308 assertEquals(undefined, state['7'].children); |
| 304 assertDeepEquals(['7'], state['6'].children); | 309 assertDeepEquals(['7'], state['6'].children); |
| 305 }); | 310 }); |
| 306 | 311 |
| 307 test('updates when a node is deleted', function() { | 312 test('updates when a node is deleted', function() { |
| 308 action = bookmarks.actions.removeBookmark('3', '1', 1, state); | 313 action = remove('3', '1', 1, []); |
| 309 state = bookmarks.NodeState.updateNodes(state, action); | 314 state = bookmarks.NodeState.updateNodes(state, action); |
| 310 | 315 |
| 311 assertDeepEquals(['2', '4'], state['1'].children); | 316 assertDeepEquals(['2', '4'], state['1'].children); |
| 312 | 317 |
| 313 assertDeepEquals(['2', '4'], state['1'].children); | 318 assertDeepEquals(['2', '4'], state['1'].children); |
| 314 assertEquals(undefined, state['3']); | 319 assertEquals(undefined, state['3']); |
| 315 }); | 320 }); |
| 316 | 321 |
| 317 test('removes all children of deleted nodes', function() { | 322 test('removes all children of deleted nodes', function() { |
| 318 action = bookmarks.actions.removeBookmark('1', '0', 0, state); | 323 action = remove('1', '0', 0, ['2', '3', '4']); |
| 319 state = bookmarks.NodeState.updateNodes(state, action); | 324 state = bookmarks.NodeState.updateNodes(state, action); |
| 320 | 325 |
| 321 assertDeepEquals(['0', '5'], Object.keys(state).sort()); | 326 assertDeepEquals(['0', '5'], Object.keys(state).sort()); |
| 322 }); | 327 }); |
| 323 | 328 |
| 324 test('updates when a node is moved', function() { | 329 test('updates when a node is moved', function() { |
| 325 // Move within the same folder backwards. | 330 // Move within the same folder backwards. |
| 326 action = bookmarks.actions.moveBookmark('3', '1', 0, '1', 1); | 331 action = bookmarks.actions.moveBookmark('3', '1', 0, '1', 1); |
| 327 state = bookmarks.NodeState.updateNodes(state, action); | 332 state = bookmarks.NodeState.updateNodes(state, action); |
| 328 | 333 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 | 410 |
| 406 test('removes deleted nodes', function() { | 411 test('removes deleted nodes', function() { |
| 407 var action; | 412 var action; |
| 408 | 413 |
| 409 action = bookmarks.actions.setSearchTerm('test'); | 414 action = bookmarks.actions.setSearchTerm('test'); |
| 410 state = bookmarks.reduceAction(state, action); | 415 state = bookmarks.reduceAction(state, action); |
| 411 | 416 |
| 412 action = bookmarks.actions.setSearchResults(['1', '3', '2']); | 417 action = bookmarks.actions.setSearchResults(['1', '3', '2']); |
| 413 state = bookmarks.reduceAction(state, action); | 418 state = bookmarks.reduceAction(state, action); |
| 414 | 419 |
| 415 action = bookmarks.actions.removeBookmark('2', '1', 0, state.nodes); | 420 action = remove('2', '1', 0, ['3']); |
| 416 state = bookmarks.reduceAction(state, action); | 421 state = bookmarks.reduceAction(state, action); |
| 417 | 422 |
| 418 // 2 and 3 should be removed, since 2 was deleted and 3 was a descendant of | 423 // 2 and 3 should be removed, since 2 was deleted and 3 was a descendant of |
| 419 // 2. | 424 // 2. |
| 420 assertDeepEquals(['1'], state.search.results); | 425 assertDeepEquals(['1'], state.search.results); |
| 421 }); | 426 }); |
| 422 }); | 427 }); |
| OLD | NEW |