test(module-loader): add unit tests for module-loader load

This commit is contained in:
Brandy Carney
2017-03-06 17:16:03 -05:00
parent 999f33a5b9
commit a1a762c0e6
3 changed files with 80 additions and 4 deletions

View File

@@ -23,6 +23,8 @@ import { Tabs } from '../components/tabs/tabs';
import { TransitionController } from '../transitions/transition-controller';
import { UrlSerializer } from '../navigation/url-serializer';
import { ViewController } from '../navigation/view-controller';
import { ModuleLoader } from './module-loader';
import { NgModuleLoader } from './ng-module-loader';
import { DeepLinkConfig, STATE_INITIALIZED } from '../navigation/nav-util';
@@ -526,8 +528,13 @@ export class MockView5 {}
export function noop(): any { return 'noop'; };
export function mockModuleLoader() {
return { };
export function mockModuleLoader(ngModuleLoader?: NgModuleLoader): ModuleLoader {
ngModuleLoader = ngModuleLoader || mockNgModuleLoader();
return new ModuleLoader(ngModuleLoader, null);
}
export function mockNgModuleLoader(): NgModuleLoader {
return new NgModuleLoader(null);
}
export function mockOverlay() {

View File

@@ -65,9 +65,9 @@ export interface LoadedModule {
/**
* @private
*/
export function setupPreloading(deeplinkConfig: DeepLinkConfig, moduleLoader: ModuleLoader) {
export function setupPreloading(deepLinkConfig: DeepLinkConfig, moduleLoader: ModuleLoader) {
return function() {
const linksToLoad = deeplinkConfig.links.filter(link => !!link.loadChildren);
const linksToLoad = deepLinkConfig.links.filter(link => !!link.loadChildren);
for (const link of linksToLoad) {
moduleLoader.load(link.loadChildren);
}

View File

@@ -0,0 +1,69 @@
import { DeepLinkConfig } from '../../navigation/nav-util';
import { DeepLinker } from '../../navigation/deep-linker';
import { ModuleLoader } from '../module-loader';
import { mockDeepLinkConfig, mockDeepLinker, mockModuleLoader, mockNgModuleLoader } from '../mock-providers';
import { NgModuleLoader } from '../ng-module-loader';
describe('module-loader', () => {
describe('load', () => {
it('should call ngModuleLoader and receive a promise back', (done: Function) => {
spyOn(ngModuleLoader, 'load').and.returnValue(Promise.resolve());
const knownPathPrefix = '../some/known/path';
const knownExportSuffix = 'SomeModule';
const loadChildrenValue = knownPathPrefix + '#' + knownExportSuffix;
const promise = moduleLoader.load(loadChildrenValue);
promise.then((response) => {
expect(ngModuleLoader.load).toHaveBeenCalledWith(knownPathPrefix, knownExportSuffix);
}).catch((err: Error) => {
done(err);
});
});
it('should only call the ngModuleLoader when there is not an active request', () => {
let resolve: any = null;
let reject: any = null;
let promise = new Promise((scopedResolved, scopedReject) => {
resolve = scopedResolved;
reject = scopedReject;
});
spyOn(ngModuleLoader, 'load').and.returnValue(promise);
const knownPathPrefix = '../some/known/path';
const knownExportSuffix = 'SomeModule';
const loadChildrenValue = knownPathPrefix + '#' + knownExportSuffix;
promise = moduleLoader.load(loadChildrenValue);
// the promise is not resolved
let secondPromise = moduleLoader.load(loadChildrenValue);
// we would expect the same promise to be returned both times
expect(promise).toEqual(secondPromise);
// TODO enable this with caching
// expect(ngModuleLoader.load).toHaveBeenCalledTimes(1);
});
});
var deepLinker: DeepLinker;
var deepLinkConfig: DeepLinkConfig;
var moduleLoader: ModuleLoader;
var ngModuleLoader: NgModuleLoader;
beforeEach(() => {
deepLinkConfig = mockDeepLinkConfig();
deepLinker = mockDeepLinker(deepLinkConfig);
ngModuleLoader = mockNgModuleLoader();
moduleLoader = mockModuleLoader(ngModuleLoader);
});
});