test_StartupCacheInit.js (7539B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 ChromeUtils.defineESModuleGetters(this, { 7 actionCreators: "resource://newtab/common/Actions.mjs", 8 actionTypes: "resource://newtab/common/Actions.mjs", 9 StartupCacheInit: "resource://newtab/lib/StartupCacheInit.sys.mjs", 10 sinon: "resource://testing-common/Sinon.sys.mjs", 11 }); 12 13 add_task(async function test_construction() { 14 let sandbox = sinon.createSandbox(); 15 16 let feed = new StartupCacheInit(); 17 18 info("StartupCacheInit constructor should create initial values"); 19 20 Assert.ok(feed, "Could construct a StartupCacheInit"); 21 Assert.ok(!feed.loaded, "StartupCacheInit is not loaded"); 22 Assert.ok(!feed.TopsitesUpdatedReply, "topsites have not replied"); 23 Assert.ok(!feed.DiscoveryStreamSpocsUpdateReply, "spocs have not replied"); 24 Assert.ok(!feed.WeatherUpdateReply, "weather has not replied"); 25 Assert.ok( 26 !feed.CustomWallpaperUpdateReply, 27 "custom wallpapers have not replied" 28 ); 29 sandbox.restore(); 30 }); 31 32 add_task(async function test_onAction_INIT() { 33 let sandbox = sinon.createSandbox(); 34 35 let feed = new StartupCacheInit(); 36 37 info("StartupCacheInit.onAction INIT should set loaded"); 38 39 await feed.onAction({ 40 type: actionTypes.INIT, 41 }); 42 43 Assert.ok(feed.loaded); 44 sandbox.restore(); 45 }); 46 47 add_task( 48 async function test_onAction_NEW_TAB_STATE_REQUEST_WITHOUT_STARTUPCACHE() { 49 let sandbox = sinon.createSandbox(); 50 51 let feed = new StartupCacheInit(); 52 53 feed.store = { 54 dispatch: sinon.spy(), 55 uninitFeed: sinon.spy(), 56 }; 57 58 info( 59 "StartupCacheInit.onAction NEW_TAB_STATE_REQUEST_WITHOUT_STARTUPCACHE should uninit" 60 ); 61 62 await feed.onAction({ 63 type: actionTypes.INIT, 64 }); 65 await feed.onAction({ 66 type: actionTypes.NEW_TAB_STATE_REQUEST_WITHOUT_STARTUPCACHE, 67 }); 68 69 Assert.ok(feed.store.uninitFeed.calledOnce); 70 Assert.ok( 71 feed.store.uninitFeed.calledWith("feeds.startupcacheinit", { 72 type: actionTypes.UNINIT, 73 }) 74 ); 75 sandbox.restore(); 76 } 77 ); 78 79 add_task(async function test_onAction_NEW_TAB_STATE_REQUEST_STARTUPCACHE() { 80 let sandbox = sinon.createSandbox(); 81 82 let feed = new StartupCacheInit(); 83 84 feed.store = { 85 dispatch: sinon.spy(), 86 uninitFeed: sinon.spy(), 87 }; 88 89 info( 90 "StartupCacheInit.onAction NEW_TAB_STATE_REQUEST_STARTUPCACHE should uninit" 91 ); 92 93 await feed.onAction({ 94 type: actionTypes.INIT, 95 }); 96 await feed.onAction({ 97 type: actionTypes.NEW_TAB_STATE_REQUEST_STARTUPCACHE, 98 meta: { fromTarget: "fromTarget" }, 99 }); 100 101 Assert.ok(feed.store.uninitFeed.calledOnce); 102 Assert.ok( 103 feed.store.uninitFeed.calledWith("feeds.startupcacheinit", { 104 type: actionTypes.UNINIT, 105 }) 106 ); 107 sandbox.restore(); 108 }); 109 110 add_task(async function test_onAction_TOP_SITES_UPDATED() { 111 let sandbox = sinon.createSandbox(); 112 113 let feed = new StartupCacheInit(); 114 115 feed.store = { 116 dispatch: sinon.spy(), 117 uninitFeed: sinon.spy(), 118 getState() { 119 return this.state; 120 }, 121 state: { 122 TopSites: { 123 rows: ["topsite1", "topsite2"], 124 }, 125 }, 126 }; 127 128 info( 129 "StartupCacheInit.onAction TOP_SITES_UPDATED should forward topsites info" 130 ); 131 132 await feed.onAction({ 133 type: actionTypes.INIT, 134 }); 135 await feed.onAction({ 136 type: actionTypes.TOP_SITES_UPDATED, 137 }); 138 await feed.onAction({ 139 type: actionTypes.NEW_TAB_STATE_REQUEST_STARTUPCACHE, 140 meta: { fromTarget: "fromTarget" }, 141 }); 142 143 Assert.ok(feed.store.dispatch.calledOnce); 144 Assert.ok( 145 feed.store.dispatch.calledWith( 146 actionCreators.OnlyToOneContent( 147 { 148 type: actionTypes.TOP_SITES_UPDATED, 149 data: { 150 links: ["topsite1", "topsite2"], 151 }, 152 }, 153 "fromTarget" 154 ) 155 ) 156 ); 157 sandbox.restore(); 158 }); 159 160 add_task(async function test_onAction_DISCOVERY_STREAM_SPOCS_UPDATE() { 161 let sandbox = sinon.createSandbox(); 162 163 let feed = new StartupCacheInit(); 164 165 feed.store = { 166 dispatch: sinon.spy(), 167 uninitFeed: sinon.spy(), 168 getState() { 169 return this.state; 170 }, 171 state: { 172 DiscoveryStream: { 173 spocs: { 174 data: ["spoc1", "spoc2"], 175 lastUpdated: "lastUpdated", 176 onDemand: { enabled: false }, 177 cacheUpdateTime: 30 * 60 * 1000, 178 }, 179 }, 180 Prefs: { 181 values: { 182 "discoverystream.spocs.startupCache.enabled": false, 183 }, 184 }, 185 }, 186 }; 187 188 info( 189 "StartupCacheInit.onAction DISCOVERY_STREAM_SPOCS_UPDATE should forward spocs info" 190 ); 191 192 await feed.onAction({ 193 type: actionTypes.INIT, 194 }); 195 await feed.onAction({ 196 type: actionTypes.DISCOVERY_STREAM_SPOCS_UPDATE, 197 }); 198 await feed.onAction({ 199 type: actionTypes.NEW_TAB_STATE_REQUEST_STARTUPCACHE, 200 meta: { fromTarget: "fromTarget" }, 201 }); 202 203 Assert.ok(feed.store.dispatch.calledOnce); 204 Assert.ok( 205 feed.store.dispatch.calledWith( 206 actionCreators.OnlyToOneContent( 207 { 208 type: actionTypes.DISCOVERY_STREAM_SPOCS_UPDATE, 209 data: { 210 spocs: ["spoc1", "spoc2"], 211 lastUpdated: "lastUpdated", 212 spocsOnDemand: false, 213 spocsCacheUpdateTime: 30 * 60 * 1000, 214 }, 215 }, 216 "fromTarget" 217 ) 218 ) 219 ); 220 sandbox.restore(); 221 }); 222 223 add_task(async function test_onAction_WEATHER_UPDATE() { 224 let sandbox = sinon.createSandbox(); 225 226 let feed = new StartupCacheInit(); 227 228 feed.store = { 229 dispatch: sinon.spy(), 230 uninitFeed: sinon.spy(), 231 getState() { 232 return this.state; 233 }, 234 state: { 235 Weather: { 236 suggestions: "suggestions", 237 lastUpdated: "lastUpdated", 238 locationData: "locationData", 239 }, 240 }, 241 }; 242 243 info("StartupCacheInit.onAction WEATHER_UPDATE should forward weather info"); 244 245 await feed.onAction({ 246 type: actionTypes.INIT, 247 }); 248 await feed.onAction({ 249 type: actionTypes.WEATHER_UPDATE, 250 }); 251 await feed.onAction({ 252 type: actionTypes.NEW_TAB_STATE_REQUEST_STARTUPCACHE, 253 meta: { fromTarget: "fromTarget" }, 254 }); 255 256 Assert.ok(feed.store.dispatch.calledOnce); 257 Assert.ok( 258 feed.store.dispatch.calledWith( 259 actionCreators.OnlyToOneContent( 260 { 261 type: actionTypes.WEATHER_UPDATE, 262 data: { 263 suggestions: "suggestions", 264 lastUpdated: "lastUpdated", 265 locationData: "locationData", 266 }, 267 }, 268 "fromTarget" 269 ) 270 ) 271 ); 272 sandbox.restore(); 273 }); 274 275 add_task(async function test_onAction_WALLPAPERS_CUSTOM_SET() { 276 let sandbox = sinon.createSandbox(); 277 278 let feed = new StartupCacheInit(); 279 280 feed.store = { 281 dispatch: sinon.spy(), 282 uninitFeed: sinon.spy(), 283 getState() { 284 return this.state; 285 }, 286 state: { 287 Wallpapers: { 288 uploadedWallpaper: "uploadedWallpaper", 289 }, 290 }, 291 }; 292 293 info( 294 "StartupCacheInit.onAction WALLPAPERS_CUSTOM_SET should forward wallpaper info" 295 ); 296 297 await feed.onAction({ 298 type: actionTypes.INIT, 299 }); 300 await feed.onAction({ 301 type: actionTypes.WALLPAPERS_CUSTOM_SET, 302 }); 303 await feed.onAction({ 304 type: actionTypes.NEW_TAB_STATE_REQUEST_STARTUPCACHE, 305 meta: { fromTarget: "fromTarget" }, 306 }); 307 308 Assert.ok(feed.store.dispatch.calledOnce); 309 Assert.ok( 310 feed.store.dispatch.calledWith( 311 actionCreators.OnlyToOneContent( 312 { 313 type: actionTypes.WALLPAPERS_CUSTOM_SET, 314 data: "uploadedWallpaper", 315 }, 316 "fromTarget" 317 ) 318 ) 319 ); 320 sandbox.restore(); 321 });