|
| 1 | +import { tags } from '@angular-devkit/core'; |
| 2 | +import { createTypescriptContext, transformTypescript } from "@ngtools/webpack/src/transformers"; |
| 3 | +import { nsReplaceLazyLoader, NgLazyLoaderCode, getConfigObjectSetupCode } from './ns-replace-lazy-loader'; |
| 4 | +import { AngularCompilerPlugin } from '@ngtools/webpack'; |
| 5 | + |
| 6 | +describe('@ngtools/webpack transformers', () => { |
| 7 | + describe('ns-replace-lazy-loader', () => { |
| 8 | + const configObjectName = "testIdentifier"; |
| 9 | + const configObjectSetupCode = getConfigObjectSetupCode(configObjectName, "providers", "NgModuleFactoryLoader", "{ provide: nsNgCoreImport_Generated.NgModuleFactoryLoader, useClass: NSLazyModulesLoader_Generated }"); |
| 10 | + const testCases = [ |
| 11 | + { |
| 12 | + name: "should add providers and NgModuleFactoryLoader when providers is missing", |
| 13 | + rawAppModule: ` |
| 14 | + import { NgModule } from "@angular/core"; |
| 15 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 16 | + import { AppComponent } from "./app.component"; |
| 17 | +
|
| 18 | + @NgModule({ |
| 19 | + bootstrap: [ |
| 20 | + AppComponent |
| 21 | + ], |
| 22 | + imports: [ |
| 23 | + NativeScriptModule |
| 24 | + ], |
| 25 | + declarations: [ |
| 26 | + AppComponent, |
| 27 | + ] |
| 28 | + }) |
| 29 | + export class AppModule { } |
| 30 | + `, |
| 31 | + transformedAppModule: ` |
| 32 | + import * as tslib_1 from "tslib"; import { NgModule } from "@angular/core"; |
| 33 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 34 | + import { AppComponent } from "./app.component"; |
| 35 | + ${NgLazyLoaderCode} |
| 36 | + let AppModule = class AppModule { }; |
| 37 | + AppModule = tslib_1.__decorate([ NgModule({ |
| 38 | + bootstrap: [ AppComponent ], |
| 39 | + imports: [ NativeScriptModule ], |
| 40 | + declarations: [ AppComponent, ], |
| 41 | + providers: [{ provide: nsNgCoreImport_Generated.NgModuleFactoryLoader, useClass: NSLazyModulesLoader_Generated }] }) |
| 42 | + ], |
| 43 | + AppModule); |
| 44 | + export { AppModule };` |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "should add NgModuleFactoryLoader when the providers array is empty", |
| 48 | + rawAppModule: ` |
| 49 | + import { NgModule } from "@angular/core"; |
| 50 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 51 | + import { AppComponent } from "./app.component"; |
| 52 | +
|
| 53 | + @NgModule({ |
| 54 | + bootstrap: [ |
| 55 | + AppComponent |
| 56 | + ], |
| 57 | + imports: [ |
| 58 | + NativeScriptModule |
| 59 | + ], |
| 60 | + declarations: [ |
| 61 | + AppComponent, |
| 62 | + ], |
| 63 | + providers: [] |
| 64 | + }) |
| 65 | + export class AppModule { } |
| 66 | + `, |
| 67 | + transformedAppModule: ` |
| 68 | + import * as tslib_1 from "tslib"; import { NgModule } from "@angular/core"; |
| 69 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 70 | + import { AppComponent } from "./app.component"; |
| 71 | + ${NgLazyLoaderCode} |
| 72 | + let AppModule = class AppModule { }; |
| 73 | + AppModule = tslib_1.__decorate([ NgModule({ |
| 74 | + bootstrap: [ AppComponent ], |
| 75 | + imports: [ NativeScriptModule ], |
| 76 | + declarations: [ AppComponent, ], |
| 77 | + providers: [{ provide: nsNgCoreImport_Generated.NgModuleFactoryLoader, useClass: NSLazyModulesLoader_Generated }] }) |
| 78 | + ], |
| 79 | + AppModule); |
| 80 | + export { AppModule };` |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "should add NgModuleFactoryLoader at the end when the providers array is containing other providers", |
| 84 | + rawAppModule: ` |
| 85 | + import { NgModule } from "@angular/core"; |
| 86 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 87 | + import { AppComponent } from "./app.component"; |
| 88 | + @NgModule({ |
| 89 | + bootstrap: [ |
| 90 | + AppComponent |
| 91 | + ], |
| 92 | + imports: [ |
| 93 | + NativeScriptModule |
| 94 | + ], |
| 95 | + declarations: [ |
| 96 | + AppComponent, |
| 97 | + ], |
| 98 | + providers: [MyCoolProvider] |
| 99 | + }) |
| 100 | + export class AppModule { } |
| 101 | + `, |
| 102 | + transformedAppModule: ` |
| 103 | + import * as tslib_1 from "tslib"; import { NgModule } from "@angular/core"; |
| 104 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 105 | + import { AppComponent } from "./app.component"; |
| 106 | + ${NgLazyLoaderCode} |
| 107 | + let AppModule = class AppModule { }; |
| 108 | + AppModule = tslib_1.__decorate([ NgModule({ |
| 109 | + bootstrap: [ AppComponent ], |
| 110 | + imports: [ NativeScriptModule ], |
| 111 | + declarations: [ AppComponent, ], |
| 112 | + providers: [MyCoolProvider, { provide: nsNgCoreImport_Generated.NgModuleFactoryLoader, useClass: NSLazyModulesLoader_Generated }] }) |
| 113 | + ], |
| 114 | + AppModule); |
| 115 | + export { AppModule };` |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "should NOT add NgModuleFactoryLoader when its already defined", |
| 119 | + rawAppModule: ` |
| 120 | + import { NgModule } from "@angular/core"; |
| 121 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 122 | + import { AppComponent } from "./app.component"; |
| 123 | +
|
| 124 | + @NgModule({ |
| 125 | + bootstrap: [ |
| 126 | + AppComponent |
| 127 | + ], |
| 128 | + imports: [ |
| 129 | + NativeScriptModule |
| 130 | + ], |
| 131 | + declarations: [ |
| 132 | + AppComponent, |
| 133 | + ], |
| 134 | + providers: [{ provide: NgModuleFactoryLoader, useClass: CustomLoader }] |
| 135 | + }) |
| 136 | + export class AppModule { } |
| 137 | + `, |
| 138 | + transformedAppModule: ` |
| 139 | + import * as tslib_1 from "tslib"; import { NgModule } from "@angular/core"; |
| 140 | + import { NativeScriptModule } from "nativescript-angular/nativescript.module"; |
| 141 | + import { AppComponent } from "./app.component"; |
| 142 | + let AppModule = class AppModule { }; |
| 143 | + AppModule = tslib_1.__decorate([ NgModule({ |
| 144 | + bootstrap: [ AppComponent ], |
| 145 | + imports: [ NativeScriptModule ], |
| 146 | + declarations: [ AppComponent, ], |
| 147 | + providers: [{ provide: NgModuleFactoryLoader, useClass: CustomLoader }] }) |
| 148 | + ], |
| 149 | + AppModule); |
| 150 | + export { AppModule };` |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "should setup the object when an object is passed to the NgModule", |
| 154 | + rawAppModule: ` |
| 155 | + import { NgModule } from "@angular/core"; |
| 156 | + import { ${configObjectName} } from "somewhere"; |
| 157 | +
|
| 158 | + @NgModule(${configObjectName}) |
| 159 | + export class AppModule { } |
| 160 | + `, |
| 161 | + transformedAppModule: ` |
| 162 | + import * as tslib_1 from "tslib"; |
| 163 | + import { NgModule } from "@angular/core"; |
| 164 | + import { ${configObjectName} } from "somewhere"; |
| 165 | +
|
| 166 | + ${NgLazyLoaderCode} |
| 167 | + ${configObjectSetupCode} |
| 168 | + let AppModule = class AppModule { }; |
| 169 | + AppModule = tslib_1.__decorate([ NgModule(${configObjectName}) ], AppModule); |
| 170 | +
|
| 171 | + export { AppModule }; |
| 172 | + ` |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "should setup the object after its initialization when a local object is passed to the NgModule", |
| 176 | + rawAppModule: ` |
| 177 | + import { NgModule } from "@angular/core"; |
| 178 | + const ${configObjectName} = { |
| 179 | + bootstrap: [ |
| 180 | + AppComponent |
| 181 | + ], |
| 182 | + declarations: [ |
| 183 | + AppComponent |
| 184 | + ] |
| 185 | + }; |
| 186 | +
|
| 187 | + @NgModule(${configObjectName}) |
| 188 | + export class AppModule { } |
| 189 | + `, |
| 190 | + transformedAppModule: ` |
| 191 | + import * as tslib_1 from "tslib"; |
| 192 | + import { NgModule } from "@angular/core"; |
| 193 | + ${NgLazyLoaderCode} |
| 194 | + const ${configObjectName} = { |
| 195 | + bootstrap: [ |
| 196 | + AppComponent |
| 197 | + ], |
| 198 | + declarations: [ |
| 199 | + AppComponent |
| 200 | + ] |
| 201 | + }; |
| 202 | + ${configObjectSetupCode} |
| 203 | + let AppModule = class AppModule { }; |
| 204 | + AppModule = tslib_1.__decorate([ NgModule(${configObjectName}) ], AppModule); |
| 205 | + export { AppModule }; |
| 206 | + ` |
| 207 | + } |
| 208 | + ]; |
| 209 | + testCases.forEach((testCase: any) => { |
| 210 | + it(`${testCase.name}`, async () => { |
| 211 | + const input = tags.stripIndent`${testCase.rawAppModule}`; |
| 212 | + const output = tags.stripIndent`${testCase.transformedAppModule}`; |
| 213 | + const { program, compilerHost } = createTypescriptContext(input); |
| 214 | + const ngCompiler = <AngularCompilerPlugin>{ |
| 215 | + typeChecker: program.getTypeChecker(), |
| 216 | + entryModule: { |
| 217 | + path: '/project/src/test-file', |
| 218 | + className: 'AppModule', |
| 219 | + }, |
| 220 | + }; |
| 221 | + const transformer = nsReplaceLazyLoader(() => ngCompiler); |
| 222 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 223 | + |
| 224 | + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); |
| 225 | + }); |
| 226 | + }); |
| 227 | + }); |
| 228 | +}); |
0 commit comments