How to use loaded method in storybook-root

Best JavaScript code snippet using storybook-root

modulemanager_test.js

Source:modulemanager_test.js Github

copy

Full Screen

1// Copyright 2008 The Closure Library Authors. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS-IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14goog.provide('goog.module.ModuleManagerTest');15goog.setTestOnly('goog.module.ModuleManagerTest');16goog.require('goog.array');17goog.require('goog.functions');18goog.require('goog.module.BaseModule');19goog.require('goog.module.ModuleManager');20goog.require('goog.testing');21goog.require('goog.testing.MockClock');22goog.require('goog.testing.jsunit');23goog.require('goog.testing.recordFunction');24var clock;25var requestCount = 0;26function tearDown() {27 clock.dispose();28}29function setUp() {30 clock = new goog.testing.MockClock(true);31 requestCount = 0;32}33function getModuleManager(infoMap) {34 var mm = new goog.module.ModuleManager();35 mm.setAllModuleInfo(infoMap);36 mm.isModuleLoaded = function(id) {37 return this.getModuleInfo(id).isLoaded();38 };39 return mm;40}41function createSuccessfulBatchLoader(moduleMgr) {42 return {43 loadModules: function(44 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {45 requestCount++;46 setTimeout(goog.bind(this.onLoad, this, ids.concat(), 0), 5);47 },48 onLoad: function(ids, idxLoaded) {49 moduleMgr.beforeLoadModuleCode(ids[idxLoaded]);50 moduleMgr.setLoaded(ids[idxLoaded]);51 moduleMgr.afterLoadModuleCode(ids[idxLoaded]);52 var idx = idxLoaded + 1;53 if (idx < ids.length) {54 setTimeout(goog.bind(this.onLoad, this, ids, idx), 2);55 }56 }57 };58}59function createSuccessfulNonBatchLoader(moduleMgr) {60 return {61 loadModules: function(62 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {63 requestCount++;64 setTimeout(function() {65 moduleMgr.beforeLoadModuleCode(ids[0]);66 moduleMgr.setLoaded(ids[0]);67 moduleMgr.afterLoadModuleCode(ids[0]);68 if (opt_successFn) {69 opt_successFn();70 }71 }, 5);72 }73 };74}75function createUnsuccessfulLoader(moduleMgr, status) {76 return {77 loadModules: function(78 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {79 moduleMgr.beforeLoadModuleCode(ids[0]);80 setTimeout(function() { opt_errFn(status); }, 5);81 }82 };83}84function createUnsuccessfulBatchLoader(moduleMgr, status) {85 return {86 loadModules: function(87 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {88 setTimeout(function() { opt_errFn(status); }, 5);89 }90 };91}92function createTimeoutLoader(moduleMgr, status) {93 return {94 loadModules: function(95 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {96 setTimeout(function() { opt_timeoutFn(status); }, 5);97 }98 };99}100/**101 * Tests loading a module under different conditions i.e. unloaded102 * module, already loaded module, module loaded through user initiated103 * actions, synchronous callback for a module that has been already104 * loaded. Test both batch and non-batch loaders.105 */106function testExecOnLoad() {107 var mm = getModuleManager({'a': [], 'b': [], 'c': []});108 mm.setLoader(createSuccessfulNonBatchLoader(mm));109 execOnLoad_(mm);110 mm = getModuleManager({'a': [], 'b': [], 'c': []});111 mm.setLoader(createSuccessfulBatchLoader(mm));112 mm.setBatchModeEnabled(true);113 execOnLoad_(mm);114}115/**116 * Tests execOnLoad with the specified module manager.117 * @param {goog.module.ModuleManager} mm The module manager.118 */119function execOnLoad_(mm) {120 // When module is unloaded, execOnLoad is async.121 var execCalled1 = false;122 mm.execOnLoad('a', function() { execCalled1 = true; });123 assertFalse('module "a" should not be loaded', mm.isModuleLoaded('a'));124 assertTrue('module "a" should be loading', mm.isModuleLoading('a'));125 assertFalse('execCalled1 should not be set yet', execCalled1);126 assertTrue('ModuleManager should be active', mm.isActive());127 assertFalse('ModuleManager should not be user active', mm.isUserActive());128 clock.tick(5);129 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));130 assertFalse('module "a" should not be loading', mm.isModuleLoading('a'));131 assertTrue('execCalled1 should be set', execCalled1);132 assertFalse('ModuleManager should not be active', mm.isActive());133 assertFalse('ModuleManager should not be user active', mm.isUserActive());134 // When module is already loaded, execOnLoad is still async unless135 // specified otherwise.136 var execCalled2 = false;137 mm.execOnLoad('a', function() { execCalled2 = true; });138 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));139 assertFalse('module "a" should not be loading', mm.isModuleLoading('a'));140 assertFalse('execCalled2 should not be set yet', execCalled2);141 clock.tick(5);142 assertTrue('execCalled2 should be set', execCalled2);143 // When module is unloaded, execOnLoad is async (user active).144 var execCalled5 = false;145 mm.execOnLoad('c', function() { execCalled5 = true; }, null, null, true);146 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));147 assertTrue('module "c" should be loading', mm.isModuleLoading('c'));148 assertFalse('execCalled1 should not be set yet', execCalled5);149 assertTrue('ModuleManager should be active', mm.isActive());150 assertTrue('ModuleManager should be user active', mm.isUserActive());151 clock.tick(5);152 assertTrue('module "c" should be loaded', mm.isModuleLoaded('c'));153 assertFalse('module "c" should not be loading', mm.isModuleLoading('c'));154 assertTrue('execCalled1 should be set', execCalled5);155 assertFalse('ModuleManager should not be active', mm.isActive());156 assertFalse('ModuleManager should not be user active', mm.isUserActive());157 // When module is already loaded, execOnLoad is still synchronous when158 // so specified159 var execCalled6 = false;160 mm.execOnLoad('c', function() {161 execCalled6 = true;162 }, undefined, undefined, undefined, true);163 assertTrue('module "c" should be loaded', mm.isModuleLoaded('c'));164 assertFalse('module "c" should not be loading', mm.isModuleLoading('c'));165 assertTrue('execCalled6 should be set', execCalled6);166 clock.tick(5);167 assertTrue('execCalled6 should still be set', execCalled6);168}169/**170 * Test aborting the callback called on module load.171 */172function testExecOnLoadAbort() {173 var mm = getModuleManager({'a': [], 'b': [], 'c': []});174 mm.setLoader(createSuccessfulNonBatchLoader(mm));175 // When module is unloaded and abort is called, module still gets176 // loaded, but callback is cancelled.177 var execCalled1 = false;178 var callback1 = mm.execOnLoad('b', function() { execCalled1 = true; });179 callback1.abort();180 clock.tick(5);181 assertTrue('module "b" should be loaded', mm.isModuleLoaded('b'));182 assertFalse('execCalled3 should not be set', execCalled1);183 // When module is already loaded, execOnLoad is still async, so calling184 // abort should still cancel the callback.185 var execCalled2 = false;186 var callback2 = mm.execOnLoad('a', function() { execCalled2 = true; });187 callback2.abort();188 clock.tick(5);189 assertFalse('execCalled2 should not be set', execCalled2);190}191/**192 * Test preloading modules and ensure that the before load, after load193 * and set load called are called only once per module.194 */195function testExecOnLoadWhilePreloadingAndViceVersa() {196 var mm = getModuleManager({'c': [], 'd': []});197 mm.setLoader(createSuccessfulNonBatchLoader(mm));198 execOnLoadWhilePreloadingAndViceVersa_(mm);199 mm = getModuleManager({'c': [], 'd': []});200 mm.setLoader(createSuccessfulBatchLoader(mm));201 mm.setBatchModeEnabled(true);202 execOnLoadWhilePreloadingAndViceVersa_(mm);203}204/**205 * Perform tests with the specified module manager.206 * @param {goog.module.ModuleManager} mm The module manager.207 */208function execOnLoadWhilePreloadingAndViceVersa_(mm) {209 var mm = getModuleManager({'c': [], 'd': []});210 mm.setLoader(createSuccessfulNonBatchLoader(mm));211 var origSetLoaded = mm.setLoaded;212 var calls = [0, 0, 0];213 mm.beforeLoadModuleCode = function(id) { calls[0]++; };214 mm.setLoaded = function(id) {215 calls[1]++;216 origSetLoaded.call(mm, id);217 };218 mm.afterLoadModuleCode = function(id) { calls[2]++; };219 mm.preloadModule('c', 2);220 assertFalse('module "c" should not be loading yet', mm.isModuleLoading('c'));221 clock.tick(2);222 assertTrue('module "c" should now be loading', mm.isModuleLoading('c'));223 mm.execOnLoad('c', function() {});224 assertTrue('module "c" should still be loading', mm.isModuleLoading('c'));225 clock.tick(5);226 assertFalse('module "c" should be done loading', mm.isModuleLoading('c'));227 assertEquals('beforeLoad should only be called once for "c"', 1, calls[0]);228 assertEquals('setLoaded should only be called once for "c"', 1, calls[1]);229 assertEquals('afterLoad should only be called once for "c"', 1, calls[2]);230 mm.execOnLoad('d', function() {});231 assertTrue('module "d" should now be loading', mm.isModuleLoading('d'));232 mm.preloadModule('d', 2);233 clock.tick(5);234 assertFalse('module "d" should be done loading', mm.isModuleLoading('d'));235 assertTrue('module "d" should now be loaded', mm.isModuleLoaded('d'));236 assertEquals('beforeLoad should only be called once for "d"', 2, calls[0]);237 assertEquals('setLoaded should only be called once for "d"', 2, calls[1]);238 assertEquals('afterLoad should only be called once for "d"', 2, calls[2]);239}240/**241 * Tests that multiple callbacks on the same module don't cause242 * confusion about the active state after the module is finally loaded.243 */244function testUserInitiatedExecOnLoadEventuallyLeavesManagerIdle() {245 var mm = getModuleManager({'c': [], 'd': []});246 mm.setLoader(createSuccessfulNonBatchLoader(mm));247 var calledBack1 = false;248 var calledBack2 = false;249 mm.execOnLoad(250 'c', function() { calledBack1 = true; }, undefined, undefined, true);251 mm.execOnLoad(252 'c', function() { calledBack2 = true; }, undefined, undefined, true);253 mm.load('c');254 assertTrue(255 'Manager should be active while waiting for load', mm.isUserActive());256 clock.tick(5);257 assertTrue('First callback should be called', calledBack1);258 assertTrue('Second callback should be called', calledBack2);259 assertFalse(260 'Manager should be inactive after loading is complete',261 mm.isUserActive());262}263/**264 * Tests loading a module by requesting a Deferred object.265 */266function testLoad() {267 var mm = getModuleManager({'a': [], 'b': [], 'c': []});268 mm.setLoader(createSuccessfulNonBatchLoader(mm));269 var calledBack = false;270 var error = null;271 var d = mm.load('a');272 d.addCallback(function(ctx) { calledBack = true; });273 d.addErrback(function(err) { error = err; });274 assertFalse(calledBack);275 assertNull(error);276 assertFalse(mm.isUserActive());277 clock.tick(5);278 assertTrue(calledBack);279 assertNull(error);280}281/**282 * Tests loading 2 modules asserting that the loads happen in parallel283 * in one unit of time.284 */285function testLoad_concurrent() {286 var mm = getModuleManager({'a': [], 'b': [], 'c': []});287 mm.setConcurrentLoadingEnabled(true);288 mm.setLoader(createSuccessfulNonBatchLoader(mm));289 var calledBack = false;290 var error = null;291 mm.load('a');292 mm.load('b');293 assertEquals(2, requestCount);294 // Only time for one serialized download.295 clock.tick(5);296 assertTrue(mm.getModuleInfo('a').isLoaded());297 assertTrue(mm.getModuleInfo('b').isLoaded());298}299function testLoad_concurrentSecondIsDepOfFist() {300 var mm = getModuleManager({'a': [], 'b': [], 'c': []});301 mm.setBatchModeEnabled(true);302 mm.setConcurrentLoadingEnabled(true);303 mm.setLoader(createSuccessfulBatchLoader(mm));304 var calledBack = false;305 var error = null;306 mm.loadMultiple(['a', 'b']);307 mm.load('b');308 assertEquals('No 2nd request expected', 1, requestCount);309 // Only time for one serialized download.310 clock.tick(5);311 clock.tick(2); // Makes second module come in from batch requst.312 assertTrue(mm.getModuleInfo('a').isLoaded());313 assertTrue(mm.getModuleInfo('b').isLoaded());314}315function testLoad_nonConcurrent() {316 var mm = getModuleManager({'a': [], 'b': [], 'c': []});317 mm.setLoader(createSuccessfulNonBatchLoader(mm));318 var calledBack = false;319 var error = null;320 mm.load('a');321 mm.load('b');322 assertEquals(1, requestCount);323 // Only time for one serialized download.324 clock.tick(5);325 assertTrue(mm.getModuleInfo('a').isLoaded());326 assertFalse(mm.getModuleInfo('b').isLoaded());327}328function testLoadUnknown() {329 var mm = getModuleManager({'a': [], 'b': [], 'c': []});330 mm.setLoader(createSuccessfulNonBatchLoader(mm));331 var e = assertThrows(function() { mm.load('DoesNotExist'); });332 assertEquals('Unknown module: DoesNotExist', e.message);333}334/**335 * Tests loading multiple modules by requesting a Deferred object.336 */337function testLoadMultiple() {338 var mm = getModuleManager({'a': [], 'b': [], 'c': []});339 mm.setBatchModeEnabled(true);340 mm.setLoader(createSuccessfulBatchLoader(mm));341 var calledBack = false;342 var error = null;343 var calledBack2 = false;344 var error2 = null;345 var dMap = mm.loadMultiple(['a', 'b']);346 dMap['a'].addCallback(function(ctx) { calledBack = true; });347 dMap['a'].addErrback(function(err) { error = err; });348 dMap['b'].addCallback(function(ctx) { calledBack2 = true; });349 dMap['b'].addErrback(function(err) { error2 = err; });350 assertFalse(calledBack);351 assertFalse(calledBack2);352 clock.tick(5);353 assertTrue(calledBack);354 assertFalse(calledBack2);355 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));356 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));357 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));358 clock.tick(2);359 assertTrue(calledBack);360 assertTrue(calledBack2);361 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));362 assertTrue('module "b" should be loaded', mm.isModuleLoaded('b'));363 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));364 assertNull(error);365 assertNull(error2);366}367/**368 * Tests loading multiple modules with deps by requesting a Deferred object.369 */370function testLoadMultipleWithDeps() {371 var mm = getModuleManager({'a': [], 'b': ['c'], 'c': []});372 mm.setBatchModeEnabled(true);373 mm.setLoader(createSuccessfulBatchLoader(mm));374 var calledBack = false;375 var error = null;376 var calledBack2 = false;377 var error2 = null;378 var dMap = mm.loadMultiple(['a', 'b']);379 dMap['a'].addCallback(function(ctx) { calledBack = true; });380 dMap['a'].addErrback(function(err) { error = err; });381 dMap['b'].addCallback(function(ctx) { calledBack2 = true; });382 dMap['b'].addErrback(function(err) { error2 = err; });383 assertFalse(calledBack);384 assertFalse(calledBack2);385 clock.tick(5);386 assertTrue(calledBack);387 assertFalse(calledBack2);388 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));389 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));390 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));391 clock.tick(2);392 assertFalse(calledBack2);393 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));394 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));395 assertTrue('module "c" should be loaded', mm.isModuleLoaded('c'));396 clock.tick(2);397 assertTrue(calledBack2);398 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));399 assertTrue('module "b" should be loaded', mm.isModuleLoaded('b'));400 assertTrue('module "c" should be loaded', mm.isModuleLoaded('c'));401 assertNull(error);402 assertNull(error2);403}404/**405 * Tests loading multiple modules by requesting a Deferred object when406 * a server error occurs.407 */408function testLoadMultipleWithErrors() {409 var mm = getModuleManager({'a': [], 'b': [], 'c': []});410 mm.setBatchModeEnabled(true);411 mm.setLoader(createUnsuccessfulLoader(mm, 500));412 var calledBack = false;413 var error = null;414 var calledBack2 = false;415 var error2 = null;416 var calledBack3 = false;417 var error3 = null;418 var dMap = mm.loadMultiple(['a', 'b', 'c']);419 dMap['a'].addCallback(function(ctx) { calledBack = true; });420 dMap['a'].addErrback(function(err) { error = err; });421 dMap['b'].addCallback(function(ctx) { calledBack2 = true; });422 dMap['b'].addErrback(function(err) { error2 = err; });423 dMap['c'].addCallback(function(ctx) { calledBack3 = true; });424 dMap['c'].addErrback(function(err) { error3 = err; });425 assertFalse(calledBack);426 assertFalse(calledBack2);427 assertFalse(calledBack3);428 clock.tick(4);429 // A module request is now underway using the unsuccessful loader.430 // We substitute a successful loader for future module load requests.431 mm.setLoader(createSuccessfulBatchLoader(mm));432 clock.tick(1);433 assertFalse(calledBack);434 assertFalse(calledBack2);435 assertFalse(calledBack3);436 assertFalse('module "a" should not be loaded', mm.isModuleLoaded('a'));437 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));438 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));439 // Retry should happen after a backoff440 clock.tick(5 + mm.getBackOff_());441 assertTrue(calledBack);442 assertFalse(calledBack2);443 assertFalse(calledBack3);444 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));445 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));446 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));447 clock.tick(2);448 assertTrue(calledBack2);449 assertFalse(calledBack3);450 assertTrue('module "b" should be loaded', mm.isModuleLoaded('b'));451 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));452 clock.tick(2);453 assertTrue(calledBack3);454 assertTrue('module "c" should be loaded', mm.isModuleLoaded('c'));455 assertNull(error);456 assertNull(error2);457 assertNull(error3);458}459/**460 * Tests loading multiple modules by requesting a Deferred object when461 * consecutive server error occur and the loader falls back to serial462 * loads.463 */464function testLoadMultipleWithErrorsFallbackOnSerial() {465 var mm = getModuleManager({'a': [], 'b': [], 'c': []});466 mm.setBatchModeEnabled(true);467 mm.setLoader(createUnsuccessfulLoader(mm, 500));468 var calledBack = false;469 var error = null;470 var calledBack2 = false;471 var error2 = null;472 var calledBack3 = false;473 var error3 = null;474 var dMap = mm.loadMultiple(['a', 'b', 'c']);475 dMap['a'].addCallback(function(ctx) { calledBack = true; });476 dMap['a'].addErrback(function(err) { error = err; });477 dMap['b'].addCallback(function(ctx) { calledBack2 = true; });478 dMap['b'].addErrback(function(err) { error2 = err; });479 dMap['c'].addCallback(function(ctx) { calledBack3 = true; });480 dMap['c'].addErrback(function(err) { error3 = err; });481 assertFalse(calledBack);482 assertFalse(calledBack2);483 assertFalse(calledBack3);484 clock.tick(5);485 assertFalse(calledBack);486 assertFalse(calledBack2);487 assertFalse(calledBack3);488 assertFalse('module "a" should not be loaded', mm.isModuleLoaded('a'));489 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));490 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));491 // Retry should happen and fail after a backoff492 clock.tick(5 + mm.getBackOff_());493 assertFalse(calledBack);494 assertFalse(calledBack2);495 assertFalse(calledBack3);496 assertFalse('module "a" should not be loaded', mm.isModuleLoaded('a'));497 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));498 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));499 // A second retry should happen after a backoff500 clock.tick(4 + mm.getBackOff_());501 // The second retry is now underway using the unsuccessful loader.502 // We substitute a successful loader for future module load requests.503 mm.setLoader(createSuccessfulBatchLoader(mm));504 clock.tick(1);505 // A second retry should fail now506 assertFalse(calledBack);507 assertFalse(calledBack2);508 assertFalse(calledBack3);509 assertFalse('module "a" should not be loaded', mm.isModuleLoaded('a'));510 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));511 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));512 // Each module should be loaded individually now, each taking 5 ticks513 clock.tick(5);514 assertTrue(calledBack);515 assertFalse(calledBack2);516 assertFalse(calledBack3);517 assertTrue('module "a" should be loaded', mm.isModuleLoaded('a'));518 assertFalse('module "b" should not be loaded', mm.isModuleLoaded('b'));519 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));520 clock.tick(5);521 assertTrue(calledBack2);522 assertFalse(calledBack3);523 assertTrue('module "b" should be loaded', mm.isModuleLoaded('b'));524 assertFalse('module "c" should not be loaded', mm.isModuleLoaded('c'));525 clock.tick(5);526 assertTrue(calledBack3);527 assertTrue('module "c" should be loaded', mm.isModuleLoaded('c'));528 assertNull(error);529 assertNull(error2);530 assertNull(error3);531}532/**533 * Tests loading a module by user action by requesting a Deferred object.534 */535function testLoadForUser() {536 var mm = getModuleManager({'a': [], 'b': [], 'c': []});537 mm.setLoader(createSuccessfulNonBatchLoader(mm));538 var calledBack = false;539 var error = null;540 var d = mm.load('a', true);541 d.addCallback(function(ctx) { calledBack = true; });542 d.addErrback(function(err) { error = err; });543 assertFalse(calledBack);544 assertNull(error);545 assertTrue(mm.isUserActive());546 clock.tick(5);547 assertTrue(calledBack);548 assertNull(error);549}550/**551 * Tests that preloading a module calls back the deferred object.552 */553function testPreloadDeferredWhenNotLoaded() {554 var mm = getModuleManager({'a': []});555 mm.setLoader(createSuccessfulNonBatchLoader(mm));556 var calledBack = false;557 var d = mm.preloadModule('a');558 d.addCallback(function(ctx) { calledBack = true; });559 // First load should take five ticks.560 assertFalse('module "a" should not be loaded yet', calledBack);561 clock.tick(5);562 assertTrue('module "a" should be loaded', calledBack);563}564/**565 * Tests preloading an already loaded module.566 */567function testPreloadDeferredWhenLoaded() {568 var mm = getModuleManager({'a': []});569 mm.setLoader(createSuccessfulNonBatchLoader(mm));570 var calledBack = false;571 mm.preloadModule('a');572 clock.tick(5);573 var d = mm.preloadModule('a');574 d.addCallback(function(ctx) { calledBack = true; });575 // Module is already loaded, should be called back after the setTimeout576 // in preloadModule.577 assertFalse('deferred for module "a" should not be called yet', calledBack);578 clock.tick(1);579 assertTrue('module "a" should be loaded', calledBack);580}581/**582 * Tests preloading a module that is currently loading.583 */584function testPreloadDeferredWhenLoading() {585 var mm = getModuleManager({'a': []});586 mm.setLoader(createSuccessfulNonBatchLoader(mm));587 mm.preloadModule('a');588 clock.tick(1);589 // 'b' is in the middle of loading, should get called back when it's done.590 var calledBack = false;591 var d = mm.preloadModule('a');592 d.addCallback(function(ctx) { calledBack = true; });593 assertFalse('module "a" should not be loaded yet', calledBack);594 clock.tick(4);595 assertTrue('module "a" should be loaded', calledBack);596}597/**598 * Tests that load doesn't trigger another load if a module is already599 * preloading.600 */601function testLoadWhenPreloading() {602 var mm = getModuleManager({'a': [], 'b': [], 'c': []});603 mm.setLoader(createSuccessfulNonBatchLoader(mm));604 var origSetLoaded = mm.setLoaded;605 var calls = [0, 0, 0];606 mm.beforeLoadModuleCode = function(id) { calls[0]++; };607 mm.setLoaded = function(id) {608 calls[1]++;609 origSetLoaded.call(mm, id);610 };611 mm.afterLoadModuleCode = function(id) { calls[2]++; };612 var calledBack = false;613 var error = null;614 mm.preloadModule('c', 2);615 assertFalse('module "c" should not be loading yet', mm.isModuleLoading('c'));616 clock.tick(2);617 assertTrue('module "c" should now be loading', mm.isModuleLoading('c'));618 var d = mm.load('c');619 d.addCallback(function(ctx) { calledBack = true; });620 d.addErrback(function(err) { error = err; });621 assertTrue('module "c" should still be loading', mm.isModuleLoading('c'));622 clock.tick(5);623 assertFalse('module "c" should be done loading', mm.isModuleLoading('c'));624 assertEquals('beforeLoad should only be called once for "c"', 1, calls[0]);625 assertEquals('setLoaded should only be called once for "c"', 1, calls[1]);626 assertEquals('afterLoad should only be called once for "c"', 1, calls[2]);627 assertTrue(calledBack);628 assertNull(error);629}630/**631 * Tests that load doesn't trigger another load if a module is already632 * preloading.633 */634function testLoadMultipleWhenPreloading() {635 var mm = getModuleManager({'a': [], 'b': ['d'], 'c': [], 'd': []});636 mm.setLoader(createSuccessfulBatchLoader(mm));637 mm.setBatchModeEnabled(true);638 var origSetLoaded = mm.setLoaded;639 var calls = {'a': [0, 0, 0], 'b': [0, 0, 0], 'c': [0, 0, 0], 'd': [0, 0, 0]};640 mm.beforeLoadModuleCode = function(id) { calls[id][0]++; };641 mm.setLoaded = function(id) {642 calls[id][1]++;643 origSetLoaded.call(mm, id);644 };645 mm.afterLoadModuleCode = function(id) { calls[id][2]++; };646 var calledBack = false;647 var error = null;648 var calledBack2 = false;649 var error2 = null;650 var calledBack3 = false;651 var error3 = null;652 mm.preloadModule('c', 2);653 mm.preloadModule('d', 3);654 assertFalse('module "c" should not be loading yet', mm.isModuleLoading('c'));655 assertFalse('module "d" should not be loading yet', mm.isModuleLoading('d'));656 clock.tick(2);657 assertTrue('module "c" should now be loading', mm.isModuleLoading('c'));658 clock.tick(1);659 assertTrue('module "d" should now be loading', mm.isModuleLoading('d'));660 var dMap = mm.loadMultiple(['a', 'b', 'c']);661 dMap['a'].addCallback(function(ctx) { calledBack = true; });662 dMap['a'].addErrback(function(err) { error = err; });663 dMap['b'].addCallback(function(ctx) { calledBack2 = true; });664 dMap['b'].addErrback(function(err) { error2 = err; });665 dMap['c'].addCallback(function(ctx) { calledBack3 = true; });666 dMap['c'].addErrback(function(err) { error3 = err; });667 assertTrue('module "a" should be loading', mm.isModuleLoading('a'));668 assertTrue('module "b" should be loading', mm.isModuleLoading('b'));669 assertTrue('module "c" should still be loading', mm.isModuleLoading('c'));670 clock.tick(4);671 assertTrue(calledBack3);672 assertFalse('module "c" should be done loading', mm.isModuleLoading('c'));673 assertTrue('module "d" should still be loading', mm.isModuleLoading('d'));674 clock.tick(5);675 assertFalse('module "d" should be done loading', mm.isModuleLoading('d'));676 assertFalse(calledBack);677 assertFalse(calledBack2);678 assertTrue('module "a" should still be loading', mm.isModuleLoading('a'));679 assertTrue('module "b" should still be loading', mm.isModuleLoading('b'));680 clock.tick(7);681 assertTrue(calledBack);682 assertTrue(calledBack2);683 assertFalse('module "a" should be done loading', mm.isModuleLoading('a'));684 assertFalse('module "b" should be done loading', mm.isModuleLoading('b'));685 assertEquals(686 'beforeLoad should only be called once for "a"', 1, calls['a'][0]);687 assertEquals(688 'setLoaded should only be called once for "a"', 1, calls['a'][1]);689 assertEquals(690 'afterLoad should only be called once for "a"', 1, calls['a'][2]);691 assertEquals(692 'beforeLoad should only be called once for "b"', 1, calls['b'][0]);693 assertEquals(694 'setLoaded should only be called once for "b"', 1, calls['b'][1]);695 assertEquals(696 'afterLoad should only be called once for "b"', 1, calls['b'][2]);697 assertEquals(698 'beforeLoad should only be called once for "c"', 1, calls['c'][0]);699 assertEquals(700 'setLoaded should only be called once for "c"', 1, calls['c'][1]);701 assertEquals(702 'afterLoad should only be called once for "c"', 1, calls['c'][2]);703 assertEquals(704 'beforeLoad should only be called once for "d"', 1, calls['d'][0]);705 assertEquals(706 'setLoaded should only be called once for "d"', 1, calls['d'][1]);707 assertEquals(708 'afterLoad should only be called once for "d"', 1, calls['d'][2]);709 assertNull(error);710 assertNull(error2);711 assertNull(error3);712}713/**714 * Tests that the deferred is still called when loadMultiple loads modules715 * that are already preloading.716 */717function testLoadMultipleWhenPreloadingSameModules() {718 var mm = getModuleManager({'a': [], 'b': ['d'], 'c': [], 'd': []});719 mm.setLoader(createSuccessfulBatchLoader(mm));720 mm.setBatchModeEnabled(true);721 var origSetLoaded = mm.setLoaded;722 var calls = {'c': [0, 0, 0], 'd': [0, 0, 0]};723 mm.beforeLoadModuleCode = function(id) { calls[id][0]++; };724 mm.setLoaded = function(id) {725 calls[id][1]++;726 origSetLoaded.call(mm, id);727 };728 mm.afterLoadModuleCode = function(id) { calls[id][2]++; };729 var calledBack = false;730 var error = null;731 var calledBack2 = false;732 var error2 = null;733 mm.preloadModule('c', 2);734 mm.preloadModule('d', 3);735 assertFalse('module "c" should not be loading yet', mm.isModuleLoading('c'));736 assertFalse('module "d" should not be loading yet', mm.isModuleLoading('d'));737 clock.tick(2);738 assertTrue('module "c" should now be loading', mm.isModuleLoading('c'));739 clock.tick(1);740 assertTrue('module "d" should now be loading', mm.isModuleLoading('d'));741 var dMap = mm.loadMultiple(['c', 'd']);742 dMap['c'].addCallback(function(ctx) { calledBack = true; });743 dMap['c'].addErrback(function(err) { error = err; });744 dMap['d'].addCallback(function(ctx) { calledBack2 = true; });745 dMap['d'].addErrback(function(err) { error2 = err; });746 assertTrue('module "c" should still be loading', mm.isModuleLoading('c'));747 clock.tick(4);748 assertFalse('module "c" should be done loading', mm.isModuleLoading('c'));749 assertTrue('module "d" should still be loading', mm.isModuleLoading('d'));750 clock.tick(5);751 assertFalse('module "d" should be done loading', mm.isModuleLoading('d'));752 assertTrue(calledBack);753 assertTrue(calledBack2);754 assertEquals(755 'beforeLoad should only be called once for "c"', 1, calls['c'][0]);756 assertEquals(757 'setLoaded should only be called once for "c"', 1, calls['c'][1]);758 assertEquals(759 'afterLoad should only be called once for "c"', 1, calls['c'][2]);760 assertEquals(761 'beforeLoad should only be called once for "d"', 1, calls['d'][0]);762 assertEquals(763 'setLoaded should only be called once for "d"', 1, calls['d'][1]);764 assertEquals(765 'afterLoad should only be called once for "d"', 1, calls['d'][2]);766 assertNull(error);767 assertNull(error2);768}769/**770 * Tests loading a module via load when the module is already771 * loaded. The deferred's callback should be called immediately.772 */773function testLoadWhenLoaded() {774 var mm = getModuleManager({'a': [], 'b': [], 'c': []});775 mm.setLoader(createSuccessfulNonBatchLoader(mm));776 var calledBack = false;777 var error = null;778 mm.preloadModule('b', 2);779 clock.tick(10);780 assertFalse('module "b" should be done loading', mm.isModuleLoading('b'));781 var d = mm.load('b');782 d.addCallback(function(ctx) { calledBack = true; });783 d.addErrback(function(err) { error = err; });784 assertTrue(calledBack);785 assertNull(error);786}787/**788 * Tests that the deferred's errbacks are called if the module fails to load.789 */790function testLoadWithFailingModule() {791 var mm = getModuleManager({'a': [], 'b': [], 'c': []});792 mm.setLoader(createUnsuccessfulLoader(mm, 401));793 mm.registerCallback(794 goog.module.ModuleManager.CallbackType.ERROR,795 function(callbackType, id, cause) {796 assertEquals(797 'Failure cause was not as expected',798 goog.module.ModuleManager.FailureType.UNAUTHORIZED, cause);799 firedLoadFailed = true;800 });801 var calledBack = false;802 var error = null;803 var d = mm.load('a');804 d.addCallback(function(ctx) { calledBack = true; });805 d.addErrback(function(err) { error = err; });806 assertFalse(calledBack);807 assertNull(error);808 clock.tick(500);809 assertFalse(calledBack);810 // NOTE: Deferred always calls errbacks with an Error object. For now the811 // module manager just passes the FailureType which gets set as the Error812 // object's message.813 assertEquals(814 'Failure cause was not as expected',815 goog.module.ModuleManager.FailureType.UNAUTHORIZED,816 Number(error.message));817}818/**819 * Tests that the deferred's errbacks are called if a module fails to load.820 */821function testLoadMultipleWithFailingModule() {822 var mm = getModuleManager({'a': [], 'b': [], 'c': []});823 mm.setLoader(createUnsuccessfulLoader(mm, 401));824 mm.setBatchModeEnabled(true);825 mm.registerCallback(826 goog.module.ModuleManager.CallbackType.ERROR,827 function(callbackType, id, cause) {828 assertEquals(829 'Failure cause was not as expected',830 goog.module.ModuleManager.FailureType.UNAUTHORIZED, cause);831 });832 var calledBack11 = false;833 var error11 = null;834 var calledBack12 = false;835 var error12 = null;836 var calledBack21 = false;837 var error21 = null;838 var calledBack22 = false;839 var error22 = null;840 var dMap = mm.loadMultiple(['a', 'b']);841 dMap['a'].addCallback(function(ctx) { calledBack11 = true; });842 dMap['a'].addErrback(function(err) { error11 = err; });843 dMap['b'].addCallback(function(ctx) { calledBack12 = true; });844 dMap['b'].addErrback(function(err) { error12 = err; });845 var dMap2 = mm.loadMultiple(['b', 'c']);846 dMap2['b'].addCallback(function(ctx) { calledBack21 = true; });847 dMap2['b'].addErrback(function(err) { error21 = err; });848 dMap2['c'].addCallback(function(ctx) { calledBack22 = true; });849 dMap2['c'].addErrback(function(err) { error22 = err; });850 assertFalse(calledBack11);851 assertFalse(calledBack12);852 assertFalse(calledBack21);853 assertFalse(calledBack22);854 assertNull(error11);855 assertNull(error12);856 assertNull(error21);857 assertNull(error22);858 clock.tick(5);859 assertFalse(calledBack11);860 assertFalse(calledBack12);861 assertFalse(calledBack21);862 assertFalse(calledBack22);863 // NOTE: Deferred always calls errbacks with an Error object. For now the864 // module manager just passes the FailureType which gets set as the Error865 // object's message.866 assertEquals(867 'Failure cause was not as expected',868 goog.module.ModuleManager.FailureType.UNAUTHORIZED,869 Number(error11.message));870 assertEquals(871 'Failure cause was not as expected',872 goog.module.ModuleManager.FailureType.UNAUTHORIZED,873 Number(error12.message));874 // The first deferred of the second load should be called since it asks for875 // one of the failed modules.876 assertEquals(877 'Failure cause was not as expected',878 goog.module.ModuleManager.FailureType.UNAUTHORIZED,879 Number(error21.message));880 // The last deferred should be dropped so it is neither called back nor an881 // error.882 assertFalse(calledBack22);883 assertNull(error22);884}885/**886 * Tests that the right dependencies are cancelled on a loadMultiple failure.887 */888function testLoadMultipleWithFailingModuleDependencies() {889 var mm =890 getModuleManager({'a': [], 'b': [], 'c': ['b'], 'd': ['c'], 'e': []});891 mm.setLoader(createUnsuccessfulLoader(mm, 401));892 mm.setBatchModeEnabled(true);893 var cancelledIds = [];894 mm.registerCallback(895 goog.module.ModuleManager.CallbackType.ERROR,896 function(callbackType, id, cause) {897 assertEquals(898 'Failure cause was not as expected',899 goog.module.ModuleManager.FailureType.UNAUTHORIZED, cause);900 cancelledIds.push(id);901 });902 var calledBack11 = false;903 var error11 = null;904 var calledBack12 = false;905 var error12 = null;906 var calledBack21 = false;907 var error21 = null;908 var calledBack22 = false;909 var error22 = null;910 var calledBack23 = false;911 var error23 = null;912 var dMap = mm.loadMultiple(['a', 'b']);913 dMap['a'].addCallback(function(ctx) { calledBack11 = true; });914 dMap['a'].addErrback(function(err) { error11 = err; });915 dMap['b'].addCallback(function(ctx) { calledBack12 = true; });916 dMap['b'].addErrback(function(err) { error12 = err; });917 var dMap2 = mm.loadMultiple(['c', 'd', 'e']);918 dMap2['c'].addCallback(function(ctx) { calledBack21 = true; });919 dMap2['c'].addErrback(function(err) { error21 = err; });920 dMap2['d'].addCallback(function(ctx) { calledBack22 = true; });921 dMap2['d'].addErrback(function(err) { error22 = err; });922 dMap2['e'].addCallback(function(ctx) { calledBack23 = true; });923 dMap2['e'].addErrback(function(err) { error23 = err; });924 assertFalse(calledBack11);925 assertFalse(calledBack12);926 assertFalse(calledBack21);927 assertFalse(calledBack22);928 assertFalse(calledBack23);929 assertNull(error11);930 assertNull(error12);931 assertNull(error21);932 assertNull(error22);933 assertNull(error23);934 clock.tick(5);935 assertFalse(calledBack11);936 assertFalse(calledBack12);937 assertFalse(calledBack21);938 assertFalse(calledBack22);939 assertFalse(calledBack23);940 // NOTE: Deferred always calls errbacks with an Error object. For now the941 // module manager just passes the FailureType which gets set as the Error942 // object's message.943 assertEquals(944 'Failure cause was not as expected',945 goog.module.ModuleManager.FailureType.UNAUTHORIZED,946 Number(error11.message));947 assertEquals(948 'Failure cause was not as expected',949 goog.module.ModuleManager.FailureType.UNAUTHORIZED,950 Number(error12.message));951 // Check that among the failed modules, 'c' and 'd' are also cancelled952 // due to dependencies.953 assertTrue(goog.array.equals(['a', 'b', 'c', 'd'], cancelledIds.sort()));954}955/**956 * Tests that when loading multiple modules, the input array is not modified957 * when it has duplicates.958 */959function testLoadMultipleWithDuplicates() {960 var mm = getModuleManager({'a': [], 'b': []});961 mm.setBatchModeEnabled(true);962 mm.setLoader(createSuccessfulBatchLoader(mm));963 var listWithDuplicates = ['a', 'a', 'b'];964 mm.loadMultiple(listWithDuplicates);965 assertArrayEquals(966 'loadMultiple should not modify its input', ['a', 'a', 'b'],967 listWithDuplicates);968}969/**970 * Test loading dependencies transitively.971 */972function testLoadingDepsInNonBatchMode1() {973 var mm =974 getModuleManager({'i': [], 'j': [], 'k': ['j'], 'l': ['i', 'j', 'k']});975 mm.setLoader(createSuccessfulNonBatchLoader(mm));976 mm.preloadModule('j');977 clock.tick(5);978 assertTrue('module "j" should be loaded', mm.isModuleLoaded('j'));979 assertFalse('module "i" should not be loaded (1)', mm.isModuleLoaded('i'));980 assertFalse('module "k" should not be loaded (1)', mm.isModuleLoaded('k'));981 assertFalse('module "l" should not be loaded (1)', mm.isModuleLoaded('l'));982 // When loading a module in non-batch mode, its dependencies should be983 // requested independently, and in dependency order.984 mm.preloadModule('l');985 clock.tick(5);986 assertTrue('module "i" should be loaded', mm.isModuleLoaded('i'));987 assertFalse('module "k" should not be loaded (2)', mm.isModuleLoaded('k'));988 assertFalse('module "l" should not be loaded (2)', mm.isModuleLoaded('l'));989 clock.tick(5);990 assertTrue('module "k" should be loaded', mm.isModuleLoaded('k'));991 assertFalse('module "l" should not be loaded (3)', mm.isModuleLoaded('l'));992 clock.tick(5);993 assertTrue('module "l" should be loaded', mm.isModuleLoaded('l'));994}995/**996 * Test loading dependencies transitively and in dependency order.997 */998function testLoadingDepsInNonBatchMode2() {999 var mm = getModuleManager({1000 'h': [],1001 'i': ['h'],1002 'j': ['i'],1003 'k': ['j'],1004 'l': ['i', 'j', 'k'],1005 'm': ['l']1006 });1007 mm.setLoader(createSuccessfulNonBatchLoader(mm));1008 // When loading a module in non-batch mode, its dependencies should be1009 // requested independently, and in dependency order. The order in this1010 // case should be h,i,j,k,l,m.1011 mm.preloadModule('m');1012 clock.tick(5);1013 assertTrue('module "h" should be loaded', mm.isModuleLoaded('h'));1014 assertFalse('module "i" should not be loaded (1)', mm.isModuleLoaded('i'));1015 assertFalse('module "j" should not be loaded (1)', mm.isModuleLoaded('j'));1016 assertFalse('module "k" should not be loaded (1)', mm.isModuleLoaded('k'));1017 assertFalse('module "l" should not be loaded (1)', mm.isModuleLoaded('l'));1018 assertFalse('module "m" should not be loaded (1)', mm.isModuleLoaded('m'));1019 clock.tick(5);1020 assertTrue('module "i" should be loaded', mm.isModuleLoaded('i'));1021 assertFalse('module "j" should not be loaded (2)', mm.isModuleLoaded('j'));1022 assertFalse('module "k" should not be loaded (2)', mm.isModuleLoaded('k'));1023 assertFalse('module "l" should not be loaded (2)', mm.isModuleLoaded('l'));1024 assertFalse('module "m" should not be loaded (2)', mm.isModuleLoaded('m'));1025 clock.tick(5);1026 assertTrue('module "j" should be loaded', mm.isModuleLoaded('j'));1027 assertFalse('module "k" should not be loaded (3)', mm.isModuleLoaded('k'));1028 assertFalse('module "l" should not be loaded (3)', mm.isModuleLoaded('l'));1029 assertFalse('module "m" should not be loaded (3)', mm.isModuleLoaded('m'));1030 clock.tick(5);1031 assertTrue('module "k" should be loaded', mm.isModuleLoaded('k'));1032 assertFalse('module "l" should not be loaded (4)', mm.isModuleLoaded('l'));1033 assertFalse('module "m" should not be loaded (4)', mm.isModuleLoaded('m'));1034 clock.tick(5);1035 assertTrue('module "l" should be loaded', mm.isModuleLoaded('l'));1036 assertFalse('module "m" should not be loaded (5)', mm.isModuleLoaded('m'));1037 clock.tick(5);1038 assertTrue('module "m" should be loaded', mm.isModuleLoaded('m'));1039}1040function testLoadingDepsInBatchMode() {1041 var mm =1042 getModuleManager({'e': [], 'f': [], 'g': ['f'], 'h': ['e', 'f', 'g']});1043 mm.setLoader(createSuccessfulBatchLoader(mm));1044 mm.setBatchModeEnabled(true);1045 mm.preloadModule('f');1046 clock.tick(5);1047 assertTrue('module "f" should be loaded', mm.isModuleLoaded('f'));1048 assertFalse('module "e" should not be loaded (1)', mm.isModuleLoaded('e'));1049 assertFalse('module "g" should not be loaded (1)', mm.isModuleLoaded('g'));1050 assertFalse('module "h" should not be loaded (1)', mm.isModuleLoaded('h'));1051 // When loading a module in batch mode, its not-yet-loaded dependencies1052 // should be requested at the same time, and in dependency order.1053 mm.preloadModule('h');1054 clock.tick(5);1055 assertTrue('module "e" should be loaded', mm.isModuleLoaded('e'));1056 assertFalse('module "g" should not be loaded (2)', mm.isModuleLoaded('g'));1057 assertFalse('module "h" should not be loaded (2)', mm.isModuleLoaded('h'));1058 clock.tick(2);1059 assertTrue('module "g" should be loaded', mm.isModuleLoaded('g'));1060 assertFalse('module "h" should not be loaded (3)', mm.isModuleLoaded('h'));1061 clock.tick(2);1062 assertTrue('module "h" should be loaded', mm.isModuleLoaded('h'));1063}1064/**1065 * Test unauthorized errors while loading modules.1066 */1067function testUnauthorizedLoading() {1068 var mm = getModuleManager({'m': [], 'n': [], 'o': ['n']});1069 mm.setLoader(createUnsuccessfulLoader(mm, 401));1070 // Callback checks for an unauthorized error1071 var firedLoadFailed = false;1072 mm.registerCallback(1073 goog.module.ModuleManager.CallbackType.ERROR,1074 function(callbackType, id, cause) {1075 assertEquals(1076 'Failure cause was not as expected',1077 goog.module.ModuleManager.FailureType.UNAUTHORIZED, cause);1078 firedLoadFailed = true;1079 });1080 mm.execOnLoad('o', function() {});1081 assertTrue('module "o" should be loading', mm.isModuleLoading('o'));1082 assertTrue('module "n" should be loading', mm.isModuleLoading('n'));1083 clock.tick(5);1084 assertTrue(1085 'should have called unauthorized module callback', firedLoadFailed);1086 assertFalse('module "o" should not be loaded', mm.isModuleLoaded('o'));1087 assertFalse('module "o" should not be loading', mm.isModuleLoading('o'));1088 assertFalse('module "n" should not be loaded', mm.isModuleLoaded('n'));1089 assertFalse('module "n" should not be loading', mm.isModuleLoading('n'));1090}1091/**1092 * Test error loading modules which are retried.1093 */1094function testErrorLoadingModule() {1095 var mm = getModuleManager({'p': ['q'], 'q': [], 'r': ['q', 'p']});1096 mm.setLoader(createUnsuccessfulLoader(mm, 500));1097 mm.preloadModule('r');1098 clock.tick(4);1099 // A module request is now underway using the unsuccessful loader.1100 // We substitute a successful loader for future module load requests.1101 mm.setLoader(createSuccessfulNonBatchLoader(mm));1102 clock.tick(1);1103 assertFalse('module "q" should not be loaded (1)', mm.isModuleLoaded('q'));1104 assertFalse('module "p" should not be loaded (1)', mm.isModuleLoaded('p'));1105 assertFalse('module "r" should not be loaded (1)', mm.isModuleLoaded('r'));1106 // Failed loads are automatically retried after a backOff.1107 clock.tick(5 + mm.getBackOff_());1108 assertTrue('module "q" should be loaded', mm.isModuleLoaded('q'));1109 assertFalse('module "p" should not be loaded (2)', mm.isModuleLoaded('p'));1110 assertFalse('module "r" should not be loaded (2)', mm.isModuleLoaded('r'));1111 // A successful load decrements the backOff.1112 clock.tick(5);1113 assertTrue('module "p" should be loaded', mm.isModuleLoaded('p'));1114 assertFalse('module "r" should not be loaded (3)', mm.isModuleLoaded('r'));1115 clock.tick(5);1116 assertTrue('module "r" should be loaded', mm.isModuleLoaded('r'));1117}1118/**1119 * Tests error loading modules which are retried.1120 */1121function testErrorLoadingModule_batchMode() {1122 var mm = getModuleManager({'p': ['q'], 'q': [], 'r': ['q', 'p']});1123 mm.setLoader(createUnsuccessfulBatchLoader(mm, 500));1124 mm.setBatchModeEnabled(true);1125 mm.preloadModule('r');1126 clock.tick(4);1127 // A module request is now underway using the unsuccessful loader.1128 // We substitute a successful loader for future module load requests.1129 mm.setLoader(createSuccessfulBatchLoader(mm));1130 clock.tick(1);1131 assertFalse('module "q" should not be loaded (1)', mm.isModuleLoaded('q'));1132 assertFalse('module "p" should not be loaded (1)', mm.isModuleLoaded('p'));1133 assertFalse('module "r" should not be loaded (1)', mm.isModuleLoaded('r'));1134 // Failed loads are automatically retried after a backOff.1135 clock.tick(5 + mm.getBackOff_());1136 assertTrue('module "q" should be loaded', mm.isModuleLoaded('q'));1137 clock.tick(2);1138 assertTrue('module "p" should not be loaded (2)', mm.isModuleLoaded('p'));1139 clock.tick(2);1140 assertTrue('module "r" should not be loaded (2)', mm.isModuleLoaded('r'));1141}1142/**1143 * Test consecutive errors in loading modules.1144 */1145function testConsecutiveErrors() {1146 var mm = getModuleManager({'s': []});1147 mm.setLoader(createUnsuccessfulLoader(mm, 500));1148 // Register an error callback for consecutive failures.1149 var firedLoadFailed = false;1150 mm.registerCallback(1151 goog.module.ModuleManager.CallbackType.ERROR,1152 function(callbackType, id, cause) {1153 assertEquals(1154 'Failure cause was not as expected',1155 goog.module.ModuleManager.FailureType.CONSECUTIVE_FAILURES, cause);1156 firedLoadFailed = true;1157 });1158 mm.preloadModule('s');1159 assertFalse('module "s" should not be loaded (0)', mm.isModuleLoaded('s'));1160 // Fail twice.1161 for (var i = 0; i < 2; i++) {1162 clock.tick(5 + mm.getBackOff_());1163 assertFalse('module "s" should not be loaded (1)', mm.isModuleLoaded('s'));1164 assertFalse('should not fire failed callback (1)', firedLoadFailed);1165 }1166 // Fail a third time and check that the callback is fired.1167 clock.tick(5 + mm.getBackOff_());1168 assertFalse('module "s" should not be loaded (2)', mm.isModuleLoaded('s'));1169 assertTrue('should have fired failed callback', firedLoadFailed);1170 // Check that it doesn't attempt to load the module anymore after it has1171 // failed.1172 var triedLoad = false;1173 mm.setLoader({1174 loadModules: function(ids, moduleInfoMap, opt_successFn, opt_errFn) {1175 triedLoad = true;1176 }1177 });1178 // Also reset the failed callback flag and make sure it isn't called1179 // again.1180 firedLoadFailed = false;1181 clock.tick(10 + mm.getBackOff_());1182 assertFalse('module "s" should not be loaded (3)', mm.isModuleLoaded('s'));1183 assertFalse('No more loads should have been tried', triedLoad);1184 assertFalse(1185 'The load failed callback should be fired only once', firedLoadFailed);1186}1187/**1188 * Test loading errors due to old code.1189 */1190function testOldCodeGoneError() {1191 var mm = getModuleManager({'s': []});1192 mm.setLoader(createUnsuccessfulLoader(mm, 410));1193 // Callback checks for an old code failure1194 var firedLoadFailed = false;1195 mm.registerCallback(1196 goog.module.ModuleManager.CallbackType.ERROR,1197 function(callbackType, id, cause) {1198 assertEquals(1199 'Failure cause was not as expected',1200 goog.module.ModuleManager.FailureType.OLD_CODE_GONE, cause);1201 firedLoadFailed = true;1202 });1203 mm.preloadModule('s', 0);1204 assertFalse('module "s" should not be loaded (0)', mm.isModuleLoaded('s'));1205 clock.tick(5);1206 assertFalse('module "s" should not be loaded (1)', mm.isModuleLoaded('s'));1207 assertTrue('should have called old code gone callback', firedLoadFailed);1208}1209/**1210 * Test timeout.1211 */1212function testTimeout() {1213 var mm = getModuleManager({'s': []});1214 mm.setLoader(createTimeoutLoader(mm));1215 // Callback checks for timeout1216 var firedTimeout = false;1217 mm.registerCallback(1218 goog.module.ModuleManager.CallbackType.ERROR,1219 function(callbackType, id, cause) {1220 assertEquals(1221 'Failure cause was not as expected',1222 goog.module.ModuleManager.FailureType.TIMEOUT, cause);1223 firedTimeout = true;1224 });1225 mm.preloadModule('s', 0);1226 assertFalse('module "s" should not be loaded (0)', mm.isModuleLoaded('s'));1227 clock.tick(5);1228 assertFalse('module "s" should not be loaded (1)', mm.isModuleLoaded('s'));1229 assertTrue('should have called timeout callback', firedTimeout);1230}1231/**1232 * Tests that an error during execOnLoad will trigger the error callback.1233 */1234function testExecOnLoadError() {1235 // Expect two callbacks, each of which will be called with callback type1236 // ERROR, the right module id and failure type INIT_ERROR.1237 var errorCallback1 = goog.testing.createFunctionMock('callback1');1238 errorCallback1(1239 goog.module.ModuleManager.CallbackType.ERROR, 'b',1240 goog.module.ModuleManager.FailureType.INIT_ERROR);1241 var errorCallback2 = goog.testing.createFunctionMock('callback2');1242 errorCallback2(1243 goog.module.ModuleManager.CallbackType.ERROR, 'b',1244 goog.module.ModuleManager.FailureType.INIT_ERROR);1245 errorCallback1.$replay();1246 errorCallback2.$replay();1247 var mm = new goog.module.ModuleManager();1248 mm.setLoader(createSuccessfulNonBatchLoader(mm));1249 // Register the first callback before setting the module info map.1250 mm.registerCallback(1251 goog.module.ModuleManager.CallbackType.ERROR, errorCallback1);1252 mm.setAllModuleInfo({'a': [], 'b': [], 'c': []});1253 // Register the second callback after setting the module info map.1254 mm.registerCallback(1255 goog.module.ModuleManager.CallbackType.ERROR, errorCallback2);1256 var execOnLoadBCalled = false;1257 mm.execOnLoad('b', function() {1258 execOnLoadBCalled = true;1259 throw new Error();1260 });1261 assertThrows(function() { clock.tick(5); });1262 assertTrue(1263 'execOnLoad should have been called on module b.', execOnLoadBCalled);1264 errorCallback1.$verify();1265 errorCallback2.$verify();1266}1267/**1268 * Tests that an error during execOnLoad will trigger the error callback.1269 * Uses setAllModuleInfoString rather than setAllModuleInfo.1270 */1271function testExecOnLoadErrorModuleInfoString() {1272 // Expect a callback to be called with callback type ERROR, the right module1273 // id and failure type INIT_ERROR.1274 var errorCallback = goog.testing.createFunctionMock('callback');1275 errorCallback(1276 goog.module.ModuleManager.CallbackType.ERROR, 'b',1277 goog.module.ModuleManager.FailureType.INIT_ERROR);1278 errorCallback.$replay();1279 var mm = new goog.module.ModuleManager();1280 mm.setLoader(createSuccessfulNonBatchLoader(mm));1281 // Register the first callback before setting the module info map.1282 mm.registerCallback(1283 goog.module.ModuleManager.CallbackType.ERROR, errorCallback);1284 mm.setAllModuleInfoString('a/b/c');1285 var execOnLoadBCalled = false;1286 mm.execOnLoad('b', function() {1287 execOnLoadBCalled = true;1288 throw new Error();1289 });1290 assertThrows(function() { clock.tick(5); });1291 assertTrue(1292 'execOnLoad should have been called on module b.', execOnLoadBCalled);1293 errorCallback.$verify();1294}1295/**1296 * Make sure ModuleInfo objects in moduleInfoMap_ get disposed.1297 */1298function testDispose() {1299 var mm = getModuleManager({'a': [], 'b': [], 'c': []});1300 var moduleInfoA = mm.getModuleInfo('a');1301 assertNotNull(moduleInfoA);1302 var moduleInfoB = mm.getModuleInfo('b');1303 assertNotNull(moduleInfoB);1304 var moduleInfoC = mm.getModuleInfo('c');1305 assertNotNull(moduleInfoC);1306 mm.dispose();1307 assertTrue(moduleInfoA.isDisposed());1308 assertTrue(moduleInfoB.isDisposed());1309 assertTrue(moduleInfoC.isDisposed());1310}1311function testDependencyOrderingWithSimpleDeps() {1312 var mm = getModuleManager({1313 'a': ['b', 'c'],1314 'b': ['d'],1315 'c': ['e', 'f'],1316 'd': [],1317 'e': [],1318 'f': []1319 });1320 var ids = mm.getNotYetLoadedTransitiveDepIds_('a');1321 assertDependencyOrder(ids, mm);1322 assertArrayEquals(['d', 'e', 'f', 'b', 'c', 'a'], ids);1323}1324function testDependencyOrderingWithCommonDepsInDeps() {1325 // Tests to make sure that if dependencies of the root are loaded before1326 // their common dependencies.1327 var mm = getModuleManager({'a': ['b', 'c'], 'b': ['d'], 'c': ['d'], 'd': []});1328 var ids = mm.getNotYetLoadedTransitiveDepIds_('a');1329 assertDependencyOrder(ids, mm);1330 assertArrayEquals(['d', 'b', 'c', 'a'], ids);1331}1332function testDependencyOrderingWithCommonDepsInRoot1() {1333 // Tests the case where a dependency of the root depends on another1334 // dependency of the root. Regardless of ordering in the root's1335 // deps.1336 var mm = getModuleManager({'a': ['b', 'c'], 'b': ['c'], 'c': []});1337 var ids = mm.getNotYetLoadedTransitiveDepIds_('a');1338 assertDependencyOrder(ids, mm);1339 assertArrayEquals(['c', 'b', 'a'], ids);1340}1341function testDependencyOrderingWithCommonDepsInRoot2() {1342 // Tests the case where a dependency of the root depends on another1343 // dependency of the root. Regardless of ordering in the root's1344 // deps.1345 var mm = getModuleManager({'a': ['b', 'c'], 'b': [], 'c': ['b']});1346 var ids = mm.getNotYetLoadedTransitiveDepIds_('a');1347 assertDependencyOrder(ids, mm);1348 assertArrayEquals(['b', 'c', 'a'], ids);1349}1350function testDependencyOrderingWithGmailExample() {1351 // Real dependency graph taken from gmail.1352 var mm = getModuleManager({1353 's': ['dp', 'ml', 'md'],1354 'dp': ['a'],1355 'ml': ['ld', 'm'],1356 'ld': ['a'],1357 'm': ['ad', 'mh', 'n'],1358 'md': ['mh', 'ld'],1359 'a': [],1360 'mh': [],1361 'ad': [],1362 'n': []1363 });1364 mm.setLoaded('a');1365 mm.setLoaded('m');1366 mm.setLoaded('n');1367 mm.setLoaded('ad');1368 mm.setLoaded('mh');1369 var ids = mm.getNotYetLoadedTransitiveDepIds_('s');1370 assertDependencyOrder(ids, mm);1371 assertArrayEquals(['ld', 'dp', 'ml', 'md', 's'], ids);1372}1373function assertDependencyOrder(list, mm) {1374 var seen = {};1375 for (var i = 0; i < list.length; i++) {1376 var id = list[i];1377 seen[id] = true;1378 var deps = mm.getModuleInfo(id).getDependencies();1379 for (var j = 0; j < deps.length; j++) {1380 var dep = deps[j];1381 assertTrue(1382 'Unresolved dependency [' + dep + '] for [' + id + '].',1383 seen[dep] || mm.getModuleInfo(dep).isLoaded());1384 }1385 }1386}1387function testRegisterInitializationCallback() {1388 var initCalled = 0;1389 var mm = getModuleManager({'a': [], 'b': [], 'c': []});1390 mm.setLoader(1391 createSuccessfulNonBatchLoaderWithRegisterInitCallback(1392 mm, function() { ++initCalled; }));1393 execOnLoad_(mm);1394 // execOnLoad_ loads modules a and c1395 assertTrue(initCalled == 2);1396}1397function createSuccessfulNonBatchLoaderWithRegisterInitCallback(moduleMgr, fn) {1398 return {1399 loadModules: function(1400 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {1401 moduleMgr.beforeLoadModuleCode(ids[0]);1402 moduleMgr.registerInitializationCallback(fn);1403 setTimeout(function() {1404 moduleMgr.setLoaded(ids[0]);1405 moduleMgr.afterLoadModuleCode(ids[0]);1406 if (opt_successFn) {1407 opt_successFn();1408 }1409 }, 5);1410 }1411 };1412}1413function testSetModuleConstructor() {1414 var initCalled = 0;1415 var mm = getModuleManager({'a': [], 'b': [], 'c': []});1416 var info = {1417 'a': {ctor: AModule, count: 0},1418 'b': {ctor: BModule, count: 0},1419 'c': {ctor: CModule, count: 0}1420 };1421 function AModule() {1422 ++info['a'].count;1423 goog.module.BaseModule.call(this);1424 }1425 goog.inherits(AModule, goog.module.BaseModule);1426 function BModule() {1427 ++info['b'].count;1428 goog.module.BaseModule.call(this);1429 }1430 goog.inherits(BModule, goog.module.BaseModule);1431 function CModule() {1432 ++info['c'].count;1433 goog.module.BaseModule.call(this);1434 }1435 goog.inherits(CModule, goog.module.BaseModule);1436 mm.setLoader(createSuccessfulNonBatchLoaderWithConstructor(mm, info));1437 execOnLoad_(mm);1438 assertTrue(info['a'].count == 1);1439 assertTrue(info['b'].count == 0);1440 assertTrue(info['c'].count == 1);1441 assertTrue(mm.getModuleInfo('a').getModule() instanceof AModule);1442 assertTrue(mm.getModuleInfo('c').getModule() instanceof CModule);1443}1444/**1445 * Tests that a call to load the loading module during module initialization1446 * doesn't trigger a second load.1447 */1448function testLoadWhenInitializing() {1449 var mm = getModuleManager({'a': []});1450 mm.setLoader(createSuccessfulNonBatchLoader(mm));1451 var info = {'a': {ctor: AModule, count: 0}};1452 function AModule() {1453 ++info['a'].count;1454 goog.module.BaseModule.call(this);1455 }1456 goog.inherits(AModule, goog.module.BaseModule);1457 AModule.prototype.initialize = function() { mm.load('a'); };1458 mm.setLoader(createSuccessfulNonBatchLoaderWithConstructor(mm, info));1459 mm.preloadModule('a');1460 clock.tick(5);1461 assertEquals(info['a'].count, 1);1462}1463function testErrorInEarlyCallback() {1464 var errback = goog.testing.recordFunction();1465 var callback = goog.testing.recordFunction();1466 var mm = getModuleManager({'a': [], 'b': ['a']});1467 mm.getModuleInfo('a').registerEarlyCallback(goog.functions.error('error'));1468 mm.getModuleInfo('a').registerCallback(callback);1469 mm.getModuleInfo('a').registerErrback(errback);1470 mm.setLoader(1471 createSuccessfulNonBatchLoaderWithConstructor(1472 mm, createModulesFor('a', 'b')));1473 mm.preloadModule('b');1474 var e = assertThrows(function() { clock.tick(5); });1475 assertEquals('error', e.message);1476 assertEquals(0, callback.getCallCount());1477 assertEquals(1, errback.getCallCount());1478 assertEquals(1479 goog.module.ModuleManager.FailureType.INIT_ERROR,1480 errback.getLastCall().getArguments()[0]);1481 assertTrue(mm.getModuleInfo('a').isLoaded());1482 assertFalse(mm.getModuleInfo('b').isLoaded());1483 clock.tick(5);1484 assertTrue(mm.getModuleInfo('b').isLoaded());1485}1486function testErrorInNormalCallback() {1487 var earlyCallback = goog.testing.recordFunction();1488 var errback = goog.testing.recordFunction();1489 var mm = getModuleManager({'a': [], 'b': ['a']});1490 mm.getModuleInfo('a').registerEarlyCallback(earlyCallback);1491 mm.getModuleInfo('a').registerEarlyCallback(goog.functions.error('error'));1492 mm.getModuleInfo('a').registerErrback(errback);1493 mm.setLoader(1494 createSuccessfulNonBatchLoaderWithConstructor(1495 mm, createModulesFor('a', 'b')));1496 mm.preloadModule('b');1497 var e = assertThrows(function() { clock.tick(10); });1498 clock.tick(10);1499 assertEquals('error', e.message);1500 assertEquals(1, errback.getCallCount());1501 assertEquals(1502 goog.module.ModuleManager.FailureType.INIT_ERROR,1503 errback.getLastCall().getArguments()[0]);1504 assertTrue(mm.getModuleInfo('a').isLoaded());1505 assertTrue(mm.getModuleInfo('b').isLoaded());1506}1507function testErrorInErrback() {1508 var mm = getModuleManager({'a': [], 'b': ['a']});1509 mm.getModuleInfo('a').registerCallback(goog.functions.error('error1'));1510 mm.getModuleInfo('a').registerErrback(goog.functions.error('error2'));1511 mm.setLoader(1512 createSuccessfulNonBatchLoaderWithConstructor(1513 mm, createModulesFor('a', 'b')));1514 mm.preloadModule('a');1515 var e = assertThrows(function() { clock.tick(10); });1516 assertEquals('error1', e.message);1517 var e = assertThrows(function() { clock.tick(10); });1518 assertEquals('error2', e.message);1519 assertTrue(mm.getModuleInfo('a').isLoaded());1520}1521function createModulesFor(var_args) {1522 var result = {};1523 for (var i = 0; i < arguments.length; i++) {1524 var key = arguments[i];1525 result[key] = {ctor: goog.module.BaseModule};1526 }1527 return result;1528}1529function createSuccessfulNonBatchLoaderWithConstructor(moduleMgr, info) {1530 return {1531 loadModules: function(1532 ids, moduleInfoMap, opt_successFn, opt_errFn, opt_timeoutFn) {1533 setTimeout(function() {1534 moduleMgr.beforeLoadModuleCode(ids[0]);1535 moduleMgr.setModuleConstructor(info[ids[0]].ctor);1536 moduleMgr.setLoaded(ids[0]);1537 moduleMgr.afterLoadModuleCode(ids[0]);1538 if (opt_successFn) {1539 opt_successFn();1540 }1541 }, 5);1542 }1543 };1544}1545function testInitCallbackInBaseModule() {1546 var mm = new goog.module.ModuleManager();1547 var called = false;1548 var context;1549 mm.registerInitializationCallback(function(mcontext) {1550 called = true;1551 context = mcontext;1552 });1553 mm.setAllModuleInfo({'a': [], 'b': ['a']});1554 assertTrue('Base initialization not called', called);1555 assertNull('Context should still be null', context);1556 var mm = new goog.module.ModuleManager();1557 called = false;1558 mm.registerInitializationCallback(function(mcontext) {1559 called = true;1560 context = mcontext;1561 });1562 var appContext = {};1563 mm.setModuleContext(appContext);1564 assertTrue('Base initialization not called after setModuleContext', called);1565 assertEquals('Did not receive module context', appContext, context);1566}1567function testSetAllModuleInfoString() {1568 var info = 'base/one:0/two:0/three:0,1,2/four:0,3/five:';1569 var mm = new goog.module.ModuleManager();1570 mm.setAllModuleInfoString(info);1571 assertNotNull('Base should exist', mm.getModuleInfo('base'));1572 assertNotNull('One should exist', mm.getModuleInfo('one'));1573 assertNotNull('Two should exist', mm.getModuleInfo('two'));1574 assertNotNull('Three should exist', mm.getModuleInfo('three'));1575 assertNotNull('Four should exist', mm.getModuleInfo('four'));1576 assertNotNull('Five should exist', mm.getModuleInfo('five'));1577 assertArrayEquals(1578 ['base', 'one', 'two'], mm.getModuleInfo('three').getDependencies());1579 assertArrayEquals(1580 ['base', 'three'], mm.getModuleInfo('four').getDependencies());1581 assertArrayEquals([], mm.getModuleInfo('five').getDependencies());1582}1583function testSetAllModuleInfoStringWithEmptyString() {1584 var mm = new goog.module.ModuleManager();1585 var called = false;1586 var context;1587 mm.registerInitializationCallback(function(mcontext) {1588 called = true;1589 context = mcontext;1590 });1591 mm.setAllModuleInfoString('');1592 assertTrue('Initialization not called', called);1593}1594function testBackOffAmounts() {1595 var mm = new goog.module.ModuleManager();1596 assertEquals(0, mm.getBackOff_());1597 mm.consecutiveFailures_++;1598 assertEquals(5000, mm.getBackOff_());1599 mm.consecutiveFailures_++;1600 assertEquals(20000, mm.getBackOff_());1601}1602/**1603 * Tests that the IDLE callbacks are executed for active->idle transitions1604 * after setAllModuleInfoString with currently loading modules.1605 */1606function testIdleCallbackWithInitialModules() {1607 var callback = goog.testing.recordFunction();1608 var mm = new goog.module.ModuleManager();1609 mm.setAllModuleInfoString('a', ['a']);1610 mm.registerCallback(goog.module.ModuleManager.CallbackType.IDLE, callback);1611 assertTrue(mm.isActive());1612 mm.beforeLoadModuleCode('a');1613 assertEquals(0, callback.getCallCount());1614 mm.setLoaded('a');1615 mm.afterLoadModuleCode('a');1616 assertFalse(mm.isActive());1617 assertEquals(1, callback.getCallCount());...

Full Screen

Full Screen

store.js

Source:store.js Github

copy

Full Screen

1const { Flux, FluxDispatcher } = require('powercord/webpack');2const { FluxActions } = require('../constants');3let songsLoaded = false;4let topSongsLoaded = false;5let albumsLoaded = false;6let playlistsLoaded = false;7let songs = {};8let topSongs = {};9let albums = {};10let playlists = {};11function handleTopSongsLoaded (topSongsData) {12 topSongsLoaded = true;13 topSongs = topSongsData;14}15function handleSongsLoaded (songsData) {16 songsLoaded = true;17 songs = songsData;18}19function handleAlbumsLoaded (albumsData) {20 albumsLoaded = true;21 albums = albumsData;22}23function handleAlbumTracksLoaded (albumId, tracks) {24 albums = {25 ...albums,26 [albumId]: {27 ...albums[albumId],28 tracksLoaded: true,29 tracks30 }31 };32}33function handlePlaylistsLoaded (playlistsData) {34 playlistsLoaded = true;35 playlists = playlistsData;36}37function handlePlaylistTracksLoaded (playlistId, tracks) {38 playlists = {39 ...playlists,40 [playlistId]: {41 ...playlists[playlistId],42 tracksLoaded: true,43 tracks44 }45 };46}47function handlePlaylistTrackAdded (playlistId, trackId, trackDetails) {48 if (playlists[playlistId]) { // If the playlist doesn't exist it means it hasn't been loaded; Let's skip the event49 playlists = {50 ...playlists,51 [playlistId]: {52 ...playlists[playlistId],53 tracks: {54 ...playlists[playlistId].tracks,55 [trackId]: trackDetails56 }57 }58 };59 }60}61function handlePlaylistTrackRemoved (playlistId, trackId) {62 if (playlists[playlistId]) { // If the playlist doesn't exist it means it hasn't been loaded; Let's skip the event63 delete playlists[playlistId].tracks[trackId];64 playlists = global._.cloneDeep(playlists);65 }66}67function handlePurgeSongs () {68 songsLoaded = false;69 topSongsLoaded = false;70 albumsLoaded = false;71 playlistsLoaded = false;72 songs = {};73 topSongs = {};74 albums = {};75 playlists = {};76}77class SpotifyPlaylistsStore extends Flux.Store {78 getStore () {79 return {80 songsLoaded,81 topSongsLoaded,82 albumsLoaded,83 playlistsLoaded,84 songs,85 topSongs,86 albums,87 playlists88 };89 }90 getSongsLoaded () {91 return songsLoaded;92 }93 getSongs () {94 return songs;95 }96 getTopSongsLoaded () {97 return topSongsLoaded;98 }99 getTopSongs () {100 return topSongs;101 }102 getPlaylistsLoaded () {103 return playlistsLoaded;104 }105 getPlaylists () {106 return playlists;107 }108 getPlaylist (playlistId) {109 return playlists[playlistId];110 }111 isInPlaylist (playlistId, trackId) {112 if (!playlists[playlistId]) {113 return false;114 }115 return Object.keys(playlists[playlistId].tracks).includes(trackId);116 }117 getAlbumsLoaded () {118 return albumsLoaded;119 }120 getAlbumbs () {121 return albums;122 }123 getAlbum (albumId) {124 return albums[albumId];125 }126}127module.exports = new SpotifyPlaylistsStore(FluxDispatcher, {128 [FluxActions.SONGS_LOADED]: ({ songs }) => handleSongsLoaded(songs),129 [FluxActions.TOP_SONGS_LOADED]: ({ topSongs }) => handleTopSongsLoaded(topSongs),130 [FluxActions.ALBUMS_LOADED]: ({ albums }) => handleAlbumsLoaded(albums),131 [FluxActions.ALBUM_TRACKS_LOADED]: ({ albumId, tracks }) => handleAlbumTracksLoaded(albumId, tracks),132 [FluxActions.PLAYLISTS_LOADED]: ({ playlists }) => handlePlaylistsLoaded(playlists),133 [FluxActions.PLAYLIST_TRACKS_LOADED]: ({ playlistId, tracks }) => handlePlaylistTracksLoaded(playlistId, tracks),134 [FluxActions.PLAYLIST_TRACK_ADDED]: ({ playlistId, trackId, trackDetails }) => handlePlaylistTrackAdded(playlistId, trackId, trackDetails),135 [FluxActions.PLAYLIST_TRACK_REMOVED]: ({ playlistId, trackId }) => handlePlaylistTrackRemoved(playlistId, trackId),136 [FluxActions.PURGE_SONGS]: () => handlePurgeSongs()...

Full Screen

Full Screen

frame_loaded.js

Source:frame_loaded.js Github

copy

Full Screen

1(function() {2 function onReadyStateComplete(document, callback) {3 var handler;4 function checkReadyState() {5 if (document.readyState === 'complete') {6 if (handler) handler.stop();7 callback();8 return true;9 } else {10 return false;11 }12 }13 handler = Element.on(document, 'readystatechange', checkReadyState);14 checkReadyState();15 }16 function observeFrameContentLoaded(element) {17 element = $(element);18 var loaded, contentLoadedHandler;19 loaded = false;20 function fireFrameLoaded() {21 if (loaded) return;22 loaded = true;23 if (contentLoadedHandler) contentLoadedHandler.stop();24 element.fire('frame:loaded');25 }26 if (window.addEventListener) {27 contentLoadedHandler = document.on("DOMFrameContentLoaded", function(event) {28 if (element == event.element())29 fireFrameLoaded();30 });31 }32 element.on('load', function() {33 var frameDocument;34 if (typeof element.contentDocument !== 'undefined') {35 frameDocument = element.contentDocument;36 } else if (typeof element.contentWindow !== 'undefined' && typeof element.contentWindow.document !== 'undefined') {37 frameDocument = element.contentWindow.document;38 }39 onReadyStateComplete(frameDocument, fireFrameLoaded);40 });41 return element;42 }43 function onFrameLoaded(element, callback) {44 element.on('frame:loaded', callback);45 element.observeFrameContentLoaded();46 }47 Element.addMethods({48 observeFrameContentLoaded: observeFrameContentLoaded,49 onFrameLoaded: onFrameLoaded50 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const storybookRoot = require('storybook-root');2storybookRoot.loaded();3module.exports = {4 loaded: () => {5 console.log('storybook-root loaded');6 }7};8 at Object.<anonymous> (test.js:4:27)9 at Module._compile (internal/modules/cjs/loader.js:955:30)10 at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)11 at Module.load (internal/modules/cjs/loader.js:811:32)12 at Function.Module._load (internal/modules/cjs/loader.js:723:14)13 at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)14import15test('submitting form calls onSubmit method', () => {16 const onSubmit = jest.fn();17 const wrapper = shallow(<Form onSubmit={onSubmit} />);18 wrapper.find('form').simulate('submit');19 expect(onSubmit).toHaveBeenCalled();20});21wrapper.find('form').props().onSubmit();22TypeError: wrapper.find(...).props(...).onSubmit is not a function

Full Screen

Using AI Code Generation

copy

Full Screen

1var storybook = require('storybook-root');2storybook.loaded();3module.exports = {4 loaded: function() {5 console.log('loaded');6 }7}8module.exports = {9 loaded: function() {10 console.log('loaded');11 }12}13var storybook = require('storybook-root');14storybook.loaded();15module.exports = {16 loaded: function() {17 console.log('loaded');18 }19}20module.exports = {21 loaded: function() {22 console.log('loaded');23 }24}25var storybook = require('storybook-root');26storybook.loaded();27module.exports = {28 loaded: function() {29 console.log('loaded');30 }31}32module.exports = {33 loaded: function() {34 console.log('loaded');35 }36}37var storybook = require('storybook-root');38storybook.loaded();39module.exports = {40 loaded: function() {41 console.log('loaded');42 }43}44module.exports = {45 loaded: function() {46 console.log('loaded');47 }48}49var storybook = require('storybook-root');50storybook.loaded();51module.exports = {52 loaded: function() {53 console.log('loaded');54 }55}56module.exports = {57 loaded: function() {58 console.log('loaded');59 }60}61var storybook = require('storybook-root');62storybook.loaded();63module.exports = {64 loaded: function() {65 console.log('loaded');66 }67}68module.exports = {69 loaded: function() {70 console.log('loaded');71 }72}

Full Screen

Using AI Code Generation

copy

Full Screen

1const storybook = require('storybook-root').default;2storybook.loadStories().then(() => {3 console.log('done');4});5storybook.loadConfig().then(() => {6 console.log('done');7});8storybook.loadConfig().then(() => {9 console.log('done');10});11storybook.loadConfig().then(() => {12 console.log('done');13});14storybook.loadConfig().then(() => {15 console.log('done');16});17storybook.loadConfig().then(() => {18 console.log('done');19});20storybook.loadConfig().then(() => {21 console.log('done');22});23storybook.loadConfig().then(() => {24 console.log('done');25});26storybook.loadConfig().then(() => {27 console.log('done');28});29storybook.loadConfig().then(() => {30 console.log('done');31});32storybook.loadConfig().then(() => {33 console.log('done');34});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { loaded } from 'storybook-root';2loaded.then(() => {3 console.log('loaded');4});5import { loaded } from 'storybook-root';6loaded.then(() => {7 console.log('loaded');8});9import { loaded } from 'storybook-root';10loaded.then(() => {11 console.log('loaded');12});13import { loaded } from 'storybook-root';14loaded.then(() => {15 console.log('loaded');16});17import { loaded } from 'storybook-root';18loaded.then(() => {19 console.log('loaded');20});21import { loaded } from 'storybook-root';22loaded.then(() => {23 console.log('loaded');24});25import { loaded } from 'storybook-root';26loaded.then(() => {27 console.log('loaded');28});29import { loaded } from 'storybook-root';30loaded.then(() => {31 console.log('loaded');32});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful