test(ivy): test listing lazy routes to different root directories (#28542)

PR Close #28542
This commit is contained in:
George Kalpakas 2019-02-11 13:01:01 +02:00 committed by Miško Hevery
parent 188f20fb16
commit cdabda1fc0
2 changed files with 141 additions and 6 deletions

View File

@ -2119,6 +2119,69 @@ describe('ngtsc behavioral tests', () => {
'./lazy#LazyModule', /\/test\.ts$/, 'TestModule', /\/lazy\.ts$/, 'LazyModule'),
]);
});
it('should detect lazy routes in all root directories', () => {
env.tsconfig({}, ['./foo/other-root-dir', './bar/other-root-dir']);
env.write('src/test.ts', `
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot([
{path: '', loadChildren: './lazy-foo#LazyFooModule'},
]),
],
})
export class TestModule {}
`);
env.write('foo/other-root-dir/src/lazy-foo.ts', `
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forChild([
{path: '', loadChildren: './lazy-bar#LazyBarModule'},
]),
],
})
export class LazyFooModule {}
`);
env.write('bar/other-root-dir/src/lazy-bar.ts', `
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forChild([
{path: '', loadChildren: './lazier-bar#LazierBarModule'},
]),
],
})
export class LazyBarModule {}
`);
env.write('bar/other-root-dir/src/lazier-bar.ts', `
import {NgModule} from '@angular/core';
@NgModule({})
export class LazierBarModule {}
`);
const routes = env.driveRoutes();
expect(routes).toEqual([
lazyRouteMatching(
'./lazy-foo#LazyFooModule', /\/test\.ts$/, 'TestModule',
/\/foo\/other-root-dir\/src\/lazy-foo\.ts$/, 'LazyFooModule'),
lazyRouteMatching(
'./lazy-bar#LazyBarModule', /\/foo\/other-root-dir\/src\/lazy-foo\.ts$/,
'LazyFooModule', /\/bar\/other-root-dir\/src\/lazy-bar\.ts$/, 'LazyBarModule'),
lazyRouteMatching(
'./lazier-bar#LazierBarModule', /\/bar\/other-root-dir\/src\/lazy-bar\.ts$/,
'LazyBarModule', /\/bar\/other-root-dir\/src\/lazier-bar\.ts$/, 'LazierBarModule'),
]);
});
});
describe('when called with entry module', () => {
@ -2412,6 +2475,69 @@ describe('ngtsc behavioral tests', () => {
]);
});
it('should detect lazy routes in all root directories', () => {
env.tsconfig({}, ['./foo/other-root-dir', './bar/other-root-dir']);
env.write('src/test.ts', `
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forRoot([
{path: '', loadChildren: './lazy-foo#LazyFooModule'},
]),
],
})
export class TestModule {}
`);
env.write('foo/other-root-dir/src/lazy-foo.ts', `
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forChild([
{path: '', loadChildren: './lazy-bar#LazyBarModule'},
]),
],
})
export class LazyFooModule {}
`);
env.write('bar/other-root-dir/src/lazy-bar.ts', `
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
@NgModule({
imports: [
RouterModule.forChild([
{path: '', loadChildren: './lazier-bar#LazierBarModule'},
]),
],
})
export class LazyBarModule {}
`);
env.write('bar/other-root-dir/src/lazier-bar.ts', `
import {NgModule} from '@angular/core';
@NgModule({})
export class LazierBarModule {}
`);
const routes = env.driveRoutes(path.join(env.basePath, 'src/test#TestModule'));
expect(routes).toEqual([
lazyRouteMatching(
'./lazy-foo#LazyFooModule', /\/test\.ts$/, 'TestModule',
/\/foo\/other-root-dir\/src\/lazy-foo\.ts$/, 'LazyFooModule'),
lazyRouteMatching(
'./lazy-bar#LazyBarModule', /\/foo\/other-root-dir\/src\/lazy-foo\.ts$/,
'LazyFooModule', /\/bar\/other-root-dir\/src\/lazy-bar\.ts$/, 'LazyBarModule'),
lazyRouteMatching(
'./lazier-bar#LazierBarModule', /\/bar\/other-root-dir\/src\/lazy-bar\.ts$/,
'LazyBarModule', /\/bar\/other-root-dir\/src\/lazier-bar\.ts$/, 'LazierBarModule'),
]);
});
it('should ignore modules not (transitively) referenced by the entry module', () => {
env.write('test.ts', `
import {NgModule} from '@angular/core';

View File

@ -75,13 +75,22 @@ function createTestSupportFor(basePath: string) {
shouldNotExist
};
function write(fileName: string, content: string) {
const dir = path.dirname(fileName);
if (dir != '.') {
const newDir = path.resolve(basePath, dir);
if (!fs.existsSync(newDir)) fs.mkdirSync(newDir);
function ensureDirExists(absolutePathToDir: string) {
if (fs.existsSync(absolutePathToDir)) {
if (!fs.statSync(absolutePathToDir).isDirectory()) {
throw new Error(`'${absolutePathToDir}' exists and is not a directory.`);
}
} else {
const parentDir = path.dirname(absolutePathToDir);
ensureDirExists(parentDir);
fs.mkdirSync(absolutePathToDir);
}
fs.writeFileSync(path.resolve(basePath, fileName), content, {encoding: 'utf-8'});
}
function write(fileName: string, content: string) {
const absolutePathToFile = path.resolve(basePath, fileName);
ensureDirExists(path.dirname(absolutePathToFile));
fs.writeFileSync(absolutePathToFile, content);
}
function writeFiles(...mockDirs: {[fileName: string]: string}[]) {