add-default-ngmodules.spec.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var fs = require("fs");
  4. var path_1 = require("path");
  5. var upgradeScript = require("./add-default-ngmodules");
  6. var deeplinkUtils = require("../deep-linking/util");
  7. var file_cache_1 = require("../util/file-cache");
  8. var globUtil = require("../util/glob-util");
  9. var helpers = require("../util/helpers");
  10. describe('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. });
  104. function 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. }