UNPKG

7.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var fs = require("fs");
4var path_1 = require("path");
5var upgradeScript = require("./add-default-ngmodules");
6var deeplinkUtils = require("../deep-linking/util");
7var file_cache_1 = require("../util/file-cache");
8var globUtil = require("../util/glob-util");
9var helpers = require("../util/helpers");
10describe('add default ngmodules upgrade script', function () {
11 describe('getTsFilePaths', function () {
12 it('should return a list of absolute file paths', function () {
13 var srcDirectory = path_1.join('Users', 'noone', 'this', 'path', 'is', 'fake', 'src');
14 var context = {
15 srcDir: srcDirectory
16 };
17 var knownFileOne = path_1.join(srcDirectory, 'pages', 'page-one', 'page-one.ts');
18 var knownFileTwo = path_1.join(srcDirectory, 'pages', 'page-two', 'page-two.ts');
19 var knownFileThree = path_1.join(srcDirectory, 'pages', 'page-three', 'page-three.ts');
20 var knownFileFour = path_1.join(srcDirectory, 'util', 'some-util.ts');
21 var globResults = [
22 { absolutePath: knownFileOne },
23 { absolutePath: knownFileTwo },
24 { absolutePath: knownFileThree },
25 { absolutePath: knownFileFour },
26 ];
27 spyOn(globUtil, globUtil.globAll.name).and.returnValue(Promise.resolve(globResults));
28 var promise = upgradeScript.getTsFilePaths(context);
29 return promise.then(function (filePaths) {
30 expect(filePaths.length).toEqual(4);
31 expect(filePaths[0]).toEqual(knownFileOne);
32 expect(filePaths[1]).toEqual(knownFileTwo);
33 expect(filePaths[2]).toEqual(knownFileThree);
34 expect(filePaths[3]).toEqual(knownFileFour);
35 });
36 });
37 });
38 describe('readTsFiles', function () {
39 it('should read the ts files', function () {
40 var context = {
41 fileCache: new file_cache_1.FileCache()
42 };
43 var srcDirectory = path_1.join('Users', 'noone', 'this', 'path', 'is', 'fake', 'src');
44 var knownFileOne = path_1.join(srcDirectory, 'pages', 'page-one', 'page-one.ts');
45 var knownFileTwo = path_1.join(srcDirectory, 'pages', 'page-two', 'page-two.ts');
46 var knownFileThree = path_1.join(srcDirectory, 'pages', 'page-three', 'page-three.ts');
47 var knownFileFour = path_1.join(srcDirectory, 'util', 'some-util.ts');
48 var fileList = [knownFileOne, knownFileTwo, knownFileThree, knownFileFour];
49 spyOn(helpers, helpers.readFileAsync.name).and.callFake(function (filePath) {
50 // just set the file content to the path name + 'content' to keep things simple
51 return Promise.resolve(filePath + 'content');
52 });
53 var promise = upgradeScript.readTsFiles(context, fileList);
54 return promise.then(function () {
55 // the files should be cached now
56 var fileOne = context.fileCache.get(knownFileOne);
57 expect(fileOne.content).toEqual(knownFileOne + 'content');
58 var fileTwo = context.fileCache.get(knownFileTwo);
59 expect(fileTwo.content).toEqual(knownFileTwo + 'content');
60 var fileThree = context.fileCache.get(knownFileThree);
61 expect(fileThree.content).toEqual(knownFileThree + 'content');
62 var fileFour = context.fileCache.get(knownFileFour);
63 expect(fileFour.content).toEqual(knownFileFour + 'content');
64 });
65 });
66 });
67 describe('generateAndWriteNgModules', function () {
68 it('should generate NgModules for only the pages with deeplink decorator AND if the module.ts file doesnt exist', function () {
69 var srcDirectory = path_1.join('Users', 'noone', 'this', 'path', 'is', 'fake', 'src');
70 var knownFileOne = path_1.join(srcDirectory, 'pages', 'page-one', 'page-one.ts');
71 var knownFileTwo = path_1.join(srcDirectory, 'pages', 'page-two', 'page-two.ts');
72 var knownFileThree = path_1.join(srcDirectory, 'pages', 'page-three', 'page-three.ts');
73 var knownFileThreeModule = path_1.join(srcDirectory, 'pages', 'page-three', 'page-three.module.ts');
74 var knownFileFour = path_1.join(srcDirectory, 'util', 'some-util.ts');
75 var knownFileFive = path_1.join(srcDirectory, 'pages', 'page-three', 'provider.ts');
76 var knownFileSix = path_1.join(srcDirectory, 'modals', 'modal-one', 'modal-one.ts');
77 var context = {
78 fileCache: new file_cache_1.FileCache()
79 };
80 context.fileCache.set(knownFileOne, { path: knownFileOne, content: getClassContent('PageOne', 'page-one') });
81 context.fileCache.set(knownFileTwo, { path: knownFileTwo, content: getClassContent('PageTwo', 'page-two') });
82 context.fileCache.set(knownFileThree, { path: knownFileThree, content: getClassContent('PageThree', 'page-three') });
83 context.fileCache.set(knownFileThreeModule, { path: knownFileThreeModule, content: deeplinkUtils.generateDefaultDeepLinkNgModuleContent(knownFileThree, 'PageThree') });
84 context.fileCache.set(knownFileFour, { path: knownFileFour, content: knownFileFour + " content" });
85 context.fileCache.set(knownFileFive, { path: knownFileFive, content: knownFileFive + " content" });
86 context.fileCache.set(knownFileSix, { path: knownFileSix, content: getClassContent('ModalOne', 'modal-one') });
87 var ngModuleFileExtension = '.module.ts';
88 var knownNgModulePageOne = "\nimport { NgModule } from '@angular/core';\nimport { IonicPageModule } from 'ionic-angular';\nimport { PageOne } from './page-one';\n\n@NgModule({\n declarations: [\n PageOne,\n ],\n imports: [\n IonicPageModule.forChild(PageOne)\n ]\n})\nexport class PageOneModule {}\n\n";
89 var knownNgModulePageTwo = "\nimport { NgModule } from '@angular/core';\nimport { IonicPageModule } from 'ionic-angular';\nimport { PageTwo } from './page-two';\n\n@NgModule({\n declarations: [\n PageTwo,\n ],\n imports: [\n IonicPageModule.forChild(PageTwo)\n ]\n})\nexport class PageTwoModule {}\n\n";
90 var knownNgModuleModalPage = "\nimport { NgModule } from '@angular/core';\nimport { IonicPageModule } from 'ionic-angular';\nimport { ModalOne } from './modal-one';\n\n@NgModule({\n declarations: [\n ModalOne,\n ],\n imports: [\n IonicPageModule.forChild(ModalOne)\n ]\n})\nexport class ModalOneModule {}\n\n";
91 spyOn(helpers, helpers.getStringPropertyValue.name).and.returnValue(ngModuleFileExtension);
92 var fsSpy = spyOn(fs, 'writeFileSync');
93 upgradeScript.generateAndWriteNgModules(context.fileCache);
94 expect(fsSpy.calls.count()).toEqual(3);
95 expect(fsSpy.calls.argsFor(0)[0]).toEqual(helpers.changeExtension(knownFileOne, ngModuleFileExtension));
96 expect(fsSpy.calls.argsFor(0)[1]).toEqual(knownNgModulePageOne);
97 expect(fsSpy.calls.argsFor(1)[0]).toEqual(helpers.changeExtension(knownFileTwo, ngModuleFileExtension));
98 expect(fsSpy.calls.argsFor(1)[1]).toEqual(knownNgModulePageTwo);
99 expect(fsSpy.calls.argsFor(2)[0]).toEqual(helpers.changeExtension(knownFileSix, ngModuleFileExtension));
100 expect(fsSpy.calls.argsFor(2)[1]).toEqual(knownNgModuleModalPage);
101 });
102 });
103});
104function getClassContent(className, folderName) {
105 return "\nimport { Component } from '@angular/core';\nimport { IonicPage, NavController } from 'ionic-angular';\n\n@IonicPage()\n@Component({\n selector: '" + folderName + "',\n templateUrl: './" + folderName + ".html'\n})\nexport class " + className + " {\n\n constructor(public navCtrl: NavController) {}\n\n}\n";
106}