mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
test(module-loader): add unit tests for module-loader load
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
69
src/util/test/module-loader.spec.ts
Normal file
69
src/util/test/module-loader.spec.ts
Normal 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);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user