UNPKG

10.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tsUtils = require("./typescript-utils");
4describe('typescript-utils', function () {
5 describe('getNgModuleClassName', function () {
6 it('should return the NgModule class name', function () {
7 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { DeepLinkModule } from 'ionic-angular';\n\nimport { HomePage } from './home';\n\n@NgModule({\n declarations: [\n HomePage,\n ],\n imports: [\n DeepLinkModule.forChild(HomePage),\n ]\n})\nexport class HomePageModule {}\n ";
8 var knownPath = '/Users/noone/idk/some-path.module.ts';
9 var result = tsUtils.getNgModuleClassName(knownPath, knownContent);
10 expect(result).toEqual('HomePageModule');
11 });
12 it('should return the NgModule class name when there are multiple class declarations but only one is decorated', function () {
13 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { DeepLinkModule } from 'ionic-angular';\n\nimport { HomePage } from './home';\n\n@NgModule({\n declarations: [\n HomePage,\n ],\n imports: [\n DeepLinkModule.forChild(HomePage),\n ]\n})\nexport class HomePageModule {}\n\nexport class TacoBell {\n constructor() {\n }\n\n ionViewDidEnter() {\n console.log('tacos yo');\n }\n}\n ";
14 var knownPath = '/Users/noone/idk/some-path.module.ts';
15 var result = tsUtils.getNgModuleClassName(knownPath, knownContent);
16 expect(result).toEqual('HomePageModule');
17 });
18 it('should throw an error an NgModule isn\'t found', function () {
19 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { DeepLinkModule } from 'ionic-angular';\n\nimport { HomePage } from './home';\n\nexport class HomePageModule {}\n\n ";
20 var knownPath = '/Users/noone/idk/some-path.module.ts';
21 var knownError = 'Should never happen';
22 try {
23 tsUtils.getNgModuleClassName(knownPath, knownContent);
24 throw new Error(knownError);
25 }
26 catch (ex) {
27 expect(ex.message).not.toEqual(knownError);
28 }
29 });
30 it('should throw an error an multiple NgModules are found', function () {
31 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { DeepLinkModule } from 'ionic-angular';\n\nimport { HomePage } from './home';\n\n@NgModule({\n declarations: [\n HomePage,\n ],\n imports: [\n DeepLinkModule.forChild(HomePage),\n ]\n})\nexport class HomePageModule {}\n\n@NgModule({\n declarations: [\n HomePage,\n ],\n imports: [\n DeepLinkModule.forChild(HomePage),\n ]\n})\nexport class TacoBellModule {}\n\n ";
32 var knownPath = '/Users/noone/idk/some-path.module.ts';
33 var knownError = 'Should never happen';
34 try {
35 tsUtils.getNgModuleClassName(knownPath, knownContent);
36 throw new Error(knownError);
37 }
38 catch (ex) {
39 expect(ex.message).not.toEqual(knownError);
40 }
41 });
42 });
43 describe('insertNamedImportIfNeeded', function () {
44 it('should return modified file content, which is a string', function () {
45 var filePath = '/path/to/my/file';
46 var fileContent = 'import {A, B, C} from modulePath';
47 var namedImport = 'NamedImport';
48 var fromModule = 'CoolModule';
49 var result = tsUtils.insertNamedImportIfNeeded(filePath, fileContent, namedImport, fromModule);
50 // TODO: figure out how to match the exact string
51 expect(result).toEqual(jasmine.any(String));
52 });
53 it('should return the same file content as the import is already in the file', function () {
54 var filePath = '/path/to/my/file';
55 var fileContent = 'import { A } from "modulePath"';
56 var namedImport = 'A';
57 var fromModule = "modulePath";
58 var result = tsUtils.insertNamedImportIfNeeded(filePath, fileContent, namedImport, fromModule);
59 expect(result).toEqual(fileContent);
60 });
61 });
62 describe('getNgModuleDecorator', function () {
63 it('should return an object', function () {
64 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n})\nexport class AppModule {}\n\n ";
65 var knownPath = '/some/fake/path';
66 var sourceFile = tsUtils.getTypescriptSourceFile(knownPath, knownContent);
67 var result = tsUtils.getNgModuleDecorator('coolFile.ts', sourceFile);
68 expect(result).toEqual(jasmine.any(Object));
69 });
70 it('should throw an error', function () {
71 var messedUpContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n})\nexport class AppModule {}\n\n ";
72 var knownPath = '/some/fake/path';
73 var sourceFile = tsUtils.getTypescriptSourceFile(knownPath, messedUpContent);
74 expect(function () { return tsUtils.getNgModuleDecorator('coolFile.ts', sourceFile); }).toThrowError('Could not find an "NgModule" decorator in coolFile.ts');
75 });
76 });
77});
78describe('appendNgModuleDeclaration', function () {
79 it('should return a modified file content', function () {
80 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n})\nexport class AppModule {}\n";
81 var knownPath = '/some/fake/path';
82 var expectedContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent,\n CoolComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n})\nexport class AppModule {}\n";
83 var result = tsUtils.appendNgModuleDeclaration(knownPath, knownContent, 'CoolComponent');
84 expect(result).toEqual(expectedContent);
85 });
86 it('should return a modified file content for providers', function () {
87 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n providers: []\n})\nexport class AppModule {}\n";
88 var knownPath = '/some/fake/path';
89 var expectedContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n providers: [CoolProvider]\n})\nexport class AppModule {}\n";
90 var result = tsUtils.appendNgModuleProvider(knownPath, knownContent, 'CoolProvider');
91 expect(result).toEqual(expectedContent);
92 });
93 it('should return a modified file content for providers that already has one provider', function () {
94 var knownContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n providers: [AwesomeProvider]\n})\nexport class AppModule {}\n";
95 var knownPath = '/some/fake/path';
96 var expectedContent = "\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { IonicApp, IonicModule } from '../../../../..';\n\nimport { AppComponent } from './app.component';\nimport { RootPageModule } from '../pages/root-page/root-page.module';\n\n@NgModule({\n declarations: [\n AppComponent\n ],\n imports: [\n BrowserModule,\n IonicModule.forRoot(AppComponent),\n RootPageModule\n ],\n bootstrap: [IonicApp],\n providers: [AwesomeProvider,\n CoolProvider]\n})\nexport class AppModule {}\n";
97 var result = tsUtils.appendNgModuleProvider(knownPath, knownContent, 'CoolProvider');
98 expect(result).toEqual(expectedContent);
99 });
100});