|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | + |
| 4 | +import * as upgradeScript from './add-default-ngmodules'; |
| 5 | +import * as deeplinkUtils from '../deep-linking/util'; |
| 6 | +import { FileCache } from '../util/file-cache'; |
| 7 | +import * as globUtil from '../util/glob-util'; |
| 8 | +import * as helpers from '../util/helpers'; |
| 9 | + |
| 10 | +describe('add default ngmodules upgrade script', () => { |
| 11 | + describe('getTsFilePaths', () => { |
| 12 | + it('should return a list of absolute file paths', () => { |
| 13 | + const srcDirectory = join('Users', 'noone', 'this', 'path', 'is', 'fake', 'src'); |
| 14 | + const context = { |
| 15 | + srcDir: srcDirectory |
| 16 | + }; |
| 17 | + |
| 18 | + const knownFileOne = join(srcDirectory, 'pages', 'page-one', 'page-one.ts'); |
| 19 | + const knownFileTwo = join(srcDirectory, 'pages', 'page-two', 'page-two.ts'); |
| 20 | + const knownFileThree = join(srcDirectory, 'pages', 'page-three', 'page-three.ts'); |
| 21 | + const knownFileFour = join(srcDirectory, 'util', 'some-util.ts'); |
| 22 | + const globResults = [ |
| 23 | + { absolutePath: knownFileOne}, |
| 24 | + { absolutePath: knownFileTwo}, |
| 25 | + { absolutePath: knownFileThree}, |
| 26 | + { absolutePath: knownFileFour}, |
| 27 | + ]; |
| 28 | + spyOn(globUtil, globUtil.globAll.name).and.returnValue(Promise.resolve(globResults)); |
| 29 | + const promise = upgradeScript.getTsFilePaths(context); |
| 30 | + |
| 31 | + return promise.then((filePaths: string[]) => { |
| 32 | + expect(filePaths.length).toEqual(4); |
| 33 | + expect(filePaths[0]).toEqual(knownFileOne); |
| 34 | + expect(filePaths[1]).toEqual(knownFileTwo); |
| 35 | + expect(filePaths[2]).toEqual(knownFileThree); |
| 36 | + expect(filePaths[3]).toEqual(knownFileFour); |
| 37 | + }); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('readTsFiles', () => { |
| 42 | + it('should read the ts files', () => { |
| 43 | + const context = { |
| 44 | + fileCache: new FileCache() |
| 45 | + }; |
| 46 | + const srcDirectory = join('Users', 'noone', 'this', 'path', 'is', 'fake', 'src'); |
| 47 | + const knownFileOne = join(srcDirectory, 'pages', 'page-one', 'page-one.ts'); |
| 48 | + const knownFileTwo = join(srcDirectory, 'pages', 'page-two', 'page-two.ts'); |
| 49 | + const knownFileThree = join(srcDirectory, 'pages', 'page-three', 'page-three.ts'); |
| 50 | + const knownFileFour = join(srcDirectory, 'util', 'some-util.ts'); |
| 51 | + |
| 52 | + const fileList = [knownFileOne, knownFileTwo, knownFileThree, knownFileFour]; |
| 53 | + |
| 54 | + spyOn(helpers, helpers.readFileAsync.name).and.callFake((filePath: string) => { |
| 55 | + // just set the file content to the path name + 'content' to keep things simple |
| 56 | + return Promise.resolve(filePath + 'content'); |
| 57 | + }); |
| 58 | + |
| 59 | + const promise = upgradeScript.readTsFiles(context, fileList); |
| 60 | + |
| 61 | + return promise.then(() => { |
| 62 | + // the files should be cached now |
| 63 | + const fileOne = context.fileCache.get(knownFileOne); |
| 64 | + expect(fileOne.content).toEqual(knownFileOne + 'content'); |
| 65 | + |
| 66 | + const fileTwo = context.fileCache.get(knownFileTwo); |
| 67 | + expect(fileTwo.content).toEqual(knownFileTwo + 'content'); |
| 68 | + |
| 69 | + const fileThree = context.fileCache.get(knownFileThree); |
| 70 | + expect(fileThree.content).toEqual(knownFileThree + 'content'); |
| 71 | + |
| 72 | + const fileFour = context.fileCache.get(knownFileFour); |
| 73 | + expect(fileFour.content).toEqual(knownFileFour + 'content'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('generateAndWriteNgModules', () => { |
| 79 | + it('should generate NgModules for only the pages with deeplink decorator AND if the module.ts file doesnt exist', () => { |
| 80 | + const srcDirectory = join('Users', 'noone', 'this', 'path', 'is', 'fake', 'src'); |
| 81 | + const knownFileOne = join(srcDirectory, 'pages', 'page-one', 'page-one.ts'); |
| 82 | + const knownFileTwo = join(srcDirectory, 'pages', 'page-two', 'page-two.ts'); |
| 83 | + const knownFileThree = join(srcDirectory, 'pages', 'page-three', 'page-three.ts'); |
| 84 | + const knownFileThreeModule = join(srcDirectory, 'pages', 'page-three', 'page-three.module.ts'); |
| 85 | + const knownFileFour = join(srcDirectory, 'util', 'some-util.ts'); |
| 86 | + const knownFileFive = join(srcDirectory, 'pages', 'page-three', 'provider.ts'); |
| 87 | + const knownFileSix = join(srcDirectory, 'modals', 'modal-one', 'modal-one.ts'); |
| 88 | + |
| 89 | + const context = { |
| 90 | + fileCache: new FileCache() |
| 91 | + }; |
| 92 | + |
| 93 | + context.fileCache.set(knownFileOne, { path: knownFileOne, content: getClassContent('PageOne', 'page-one')}); |
| 94 | + context.fileCache.set(knownFileTwo, { path: knownFileTwo, content: getClassContent('PageTwo', 'page-two')}); |
| 95 | + context.fileCache.set(knownFileThree, { path: knownFileThree, content: getClassContent('PageThree', 'page-three')}); |
| 96 | + context.fileCache.set(knownFileThreeModule, { path: knownFileThreeModule, content: deeplinkUtils.generateDefaultDeepLinkNgModuleContent(knownFileThree, 'PageThree')}); |
| 97 | + context.fileCache.set(knownFileFour, { path: knownFileFour, content: `${knownFileFour} content`}); |
| 98 | + context.fileCache.set(knownFileFive, { path: knownFileFive, content: `${knownFileFive} content`}); |
| 99 | + context.fileCache.set(knownFileSix, { path: knownFileSix, content: getClassContent('ModalOne', 'modal-one')}); |
| 100 | + |
| 101 | + const ngModuleFileExtension = '.module.ts'; |
| 102 | + |
| 103 | + spyOn(helpers, helpers.getStringPropertyValue.name).and.returnValue(ngModuleFileExtension); |
| 104 | + const fsSpy = spyOn(fs, 'writeFileSync'); |
| 105 | + |
| 106 | + upgradeScript.generateAndWriteNgModules(context.fileCache); |
| 107 | + |
| 108 | + expect(fsSpy.calls.count()).toEqual(3); |
| 109 | + expect(fsSpy.calls.argsFor(0)[0]).toEqual(helpers.changeExtension(knownFileOne, ngModuleFileExtension)); |
| 110 | + expect(fsSpy.calls.argsFor(0)[1]).toEqual(deeplinkUtils.generateDefaultDeepLinkNgModuleContent(knownFileOne, 'PageOne')); |
| 111 | + |
| 112 | + expect(fsSpy.calls.argsFor(1)[0]).toEqual(helpers.changeExtension(knownFileTwo, ngModuleFileExtension)); |
| 113 | + expect(fsSpy.calls.argsFor(1)[1]).toEqual(deeplinkUtils.generateDefaultDeepLinkNgModuleContent(knownFileTwo, 'PageTwo')); |
| 114 | + |
| 115 | + expect(fsSpy.calls.argsFor(2)[0]).toEqual(helpers.changeExtension(knownFileSix, ngModuleFileExtension)); |
| 116 | + expect(fsSpy.calls.argsFor(2)[1]).toEqual(deeplinkUtils.generateDefaultDeepLinkNgModuleContent(knownFileSix, 'ModalOne')); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +function getClassContent(className: string, folderName: string) { |
| 122 | + return ` |
| 123 | +import { Component } from '@angular/core'; |
| 124 | +import { DeepLink, NavController } from 'ionic-angular'; |
| 125 | +
|
| 126 | +@DeepLink() |
| 127 | +@Component({ |
| 128 | + selector: '${folderName}', |
| 129 | + templateUrl: './${folderName}.html' |
| 130 | +}) |
| 131 | +export class ${className} { |
| 132 | +
|
| 133 | + constructor(public navCtrl: NavController) {} |
| 134 | +
|
| 135 | +} |
| 136 | +`; |
| 137 | +} |
0 commit comments