| 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 suite('<bookmarks-router>', function() { | 5 suite('<bookmarks-router>', function() { |
| 6 var store; | 6 var store; |
| 7 var router; | 7 var router; |
| 8 | 8 |
| 9 function navigateTo(route) { | 9 function navigateTo(route) { |
| 10 window.history.replaceState({}, '', route); | 10 window.history.replaceState({}, '', route); |
| 11 window.dispatchEvent(new CustomEvent('location-changed')); | 11 window.dispatchEvent(new CustomEvent('location-changed')); |
| 12 } | 12 } |
| 13 | 13 |
| 14 setup(function() { | 14 setup(function() { |
| 15 store = new bookmarks.TestStore({ | 15 store = new bookmarks.TestStore({ |
| 16 nodes: testTree(createFolder('1', [createFolder('2', [])])), |
| 16 selectedFolder: '1', | 17 selectedFolder: '1', |
| 17 search: { | 18 search: { |
| 18 term: '', | 19 term: '', |
| 19 }, | 20 }, |
| 20 }); | 21 }); |
| 21 bookmarks.Store.instance_ = store; | 22 bookmarks.Store.instance_ = store; |
| 22 | 23 |
| 23 router = document.createElement('bookmarks-router'); | 24 router = document.createElement('bookmarks-router'); |
| 24 replaceBody(router); | 25 replaceBody(router); |
| 25 }); | 26 }); |
| 26 | 27 |
| 27 test('search updates from route', function() { | 28 test('search updates from route', function() { |
| 28 navigateTo('/?q=bleep'); | 29 navigateTo('/?q=bleep'); |
| 29 assertEquals('start-search', store.lastAction.name); | 30 assertEquals('start-search', store.lastAction.name); |
| 30 assertEquals('bleep', store.lastAction.term); | 31 assertEquals('bleep', store.lastAction.term); |
| 31 }); | 32 }); |
| 32 | 33 |
| 33 test('selected folder updates from route', function() { | 34 test('selected folder updates from route', function() { |
| 34 navigateTo('/?id=5'); | 35 navigateTo('/?id=2'); |
| 35 assertEquals('select-folder', store.lastAction.name); | 36 assertEquals('select-folder', store.lastAction.name); |
| 36 assertEquals('5', store.lastAction.id); | 37 assertEquals('2', store.lastAction.id); |
| 37 }); | 38 }); |
| 38 | 39 |
| 39 test('route updates from ID', function() { | 40 test('route updates from ID', function() { |
| 40 store.data.selectedFolder = '6'; | 41 store.data.selectedFolder = '2'; |
| 41 store.notifyObservers(); | 42 store.notifyObservers(); |
| 42 | 43 |
| 43 return Promise.resolve().then(function() { | 44 return Promise.resolve().then(function() { |
| 44 assertEquals('chrome://bookmarks/?id=6', window.location.href); | 45 assertEquals('chrome://bookmarks/?id=2', window.location.href); |
| 45 }); | 46 }); |
| 46 }); | 47 }); |
| 47 | 48 |
| 48 test('route updates from search', function() { | 49 test('route updates from search', function() { |
| 49 store.data.search = {term: 'bloop'}; | 50 store.data.search = {term: 'bloop'}; |
| 50 store.notifyObservers(); | 51 store.notifyObservers(); |
| 51 | 52 |
| 52 return Promise.resolve() | 53 return Promise.resolve() |
| 53 .then(function() { | 54 .then(function() { |
| 54 assertEquals('chrome://bookmarks/?q=bloop', window.location.href); | 55 assertEquals('chrome://bookmarks/?q=bloop', window.location.href); |
| 55 | 56 |
| 56 // Ensure that the route doesn't change when the search finishes. | 57 // Ensure that the route doesn't change when the search finishes. |
| 57 store.data.selectedFolder = null; | 58 store.data.selectedFolder = null; |
| 58 store.notifyObservers(); | 59 store.notifyObservers(); |
| 59 }) | 60 }) |
| 60 .then(function() { | 61 .then(function() { |
| 61 assertEquals('chrome://bookmarks/?q=bloop', window.location.href); | 62 assertEquals('chrome://bookmarks/?q=bloop', window.location.href); |
| 62 }); | 63 }); |
| 63 }); | 64 }); |
| 64 }); | 65 }); |
| 65 | 66 |
| 66 suite('URL preload', function() { | 67 suite('URL preload', function() { |
| 67 test('loading a search URL performs a search', function(done) { | 68 /** |
| 68 function verifySearch(query) { | 69 * Reset the page state with a <bookmarks-app> and a clean Store, with the |
| 69 assertEquals('testQuery', query); | 70 * given |url| to trigger routing initialization code. |
| 70 done(); | 71 */ |
| 71 } | 72 function setupWithUrl(url) { |
| 73 PolymerTest.clearBody(); |
| 74 bookmarks.Store.instance_ = undefined; |
| 75 window.history.replaceState({}, '', url); |
| 72 | 76 |
| 73 if (window.searchedQuery) { | 77 chrome.bookmarks.getTree = function(callback) { |
| 74 verifySearch(window.searchedQuery); | 78 console.log('getTree'); |
| 75 return; | 79 console.log(window.location.href); |
| 76 } | 80 callback([ |
| 81 createFolder( |
| 82 '0', |
| 83 [ |
| 84 createFolder( |
| 85 '1', |
| 86 [ |
| 87 createFolder('11', []), |
| 88 ]), |
| 89 createFolder( |
| 90 '2', |
| 91 [ |
| 92 createItem('21'), |
| 93 ]), |
| 94 ]), |
| 95 ]); |
| 96 }; |
| 77 | 97 |
| 78 chrome.bookmarks.search = verifySearch; | 98 app = document.createElement('bookmarks-app'); |
| 99 document.body.appendChild(app); |
| 100 } |
| 101 |
| 102 test('loading a search URL performs a search', function() { |
| 103 var lastQuery; |
| 104 chrome.bookmarks.search = function(query) { |
| 105 lastQuery = query; |
| 106 return ['11']; |
| 107 }; |
| 108 |
| 109 setupWithUrl('/?q=testQuery'); |
| 110 assertEquals('testQuery', lastQuery); |
| 111 }); |
| 112 |
| 113 test('loading a folder URL selects that folder', function() { |
| 114 setupWithUrl('/?id=2'); |
| 115 var state = bookmarks.Store.getInstance().data; |
| 116 assertEquals('2', state.selectedFolder); |
| 117 assertDeepEquals(['21'], bookmarks.util.getDisplayedList(state)); |
| 79 }); | 118 }); |
| 80 }); | 119 }); |
| OLD | NEW |