|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const mockFs = require('mock-fs'); |
| 4 | + |
| 5 | +import * as ts from 'typescript'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as dependentFilesUtils from '../../addon/ng2/utilities/get-dependent-files'; |
| 8 | + |
| 9 | +import { expect } from 'chai'; |
| 10 | +import { ModuleResolver } from '../../addon/ng2/utilities/module-resolver'; |
| 11 | + |
| 12 | +describe('ModuleResolver', () => { |
| 13 | + let rootPath = 'src/app'; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + let mockDrive = { |
| 17 | + 'src/app': { |
| 18 | + 'foo': { |
| 19 | + 'foo.component.ts': `import * from "../bar/baz/baz.component";`, |
| 20 | + }, |
| 21 | + 'bar': { |
| 22 | + 'baz': { |
| 23 | + 'baz.component.ts': `import * from "../bar.component" |
| 24 | + import * from '../../foo-baz/qux/quux/foobar/foobar.component' |
| 25 | + ` |
| 26 | + }, |
| 27 | + 'bar.component.ts': `import * from './baz/baz.component' |
| 28 | + import * from '../foo/foo.component'`, |
| 29 | + }, |
| 30 | + 'foo-baz': { |
| 31 | + 'qux': { |
| 32 | + 'quux': { |
| 33 | + 'foobar': { |
| 34 | + 'foobar.component.ts': `import * from "../../../../foo/foo.component" |
| 35 | + import * from '../fooqux.component' |
| 36 | + `, |
| 37 | + }, |
| 38 | + 'fooqux': { |
| 39 | + 'fooqux.component.ts': 'import * from "../foobar/foobar.component"' |
| 40 | + } |
| 41 | + } |
| 42 | + }, |
| 43 | + 'no-module.component.ts': '', |
| 44 | + 'foo-baz.component.ts': 'import * from \n"../foo/foo.component"\n' |
| 45 | + }, |
| 46 | + 'empty-dir': {} |
| 47 | + } |
| 48 | + }; |
| 49 | + mockFs(mockDrive); |
| 50 | + }); |
| 51 | + afterEach(() => { |
| 52 | + mockFs.restore(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('Rewrite imports', () => { |
| 56 | + // Normalize paths for platform specific delimeter. |
| 57 | + let barFile = path.join(rootPath, 'bar/bar.component.ts'); |
| 58 | + let fooFile = path.join(rootPath, 'foo/foo.component.ts'); |
| 59 | + let bazFile = path.join(rootPath, 'bar/baz/baz.component.ts'); |
| 60 | + let fooBazFile = path.join(rootPath, 'foo-baz/foo-baz.component.ts'); |
| 61 | + let fooBarFile = path.join(rootPath, 'foo-baz/qux/quux/foobar/foobar.component.ts'); |
| 62 | + let fooQuxFile = path.join(rootPath, 'foo-baz/qux/quux/fooqux/fooqux.component.ts'); |
| 63 | + |
| 64 | + it('when there is no index.ts in oldPath', () => { |
| 65 | + let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts'); |
| 66 | + let newFilePath = path.join(rootPath, 'foo'); |
| 67 | + let resolver = new ModuleResolver(oldFilePath, newFilePath); |
| 68 | + return resolver.resolveDependentFiles() |
| 69 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 70 | + .then(() => dependentFilesUtils.createTsSourceFile(barFile)) |
| 71 | + .then((tsFileBar: ts.SourceFile) => { |
| 72 | + let contentsBar = dependentFilesUtils.getImportClauses(tsFileBar); |
| 73 | + let bazExpectedContent = path.normalize('../foo/baz.component'); |
| 74 | + expect(contentsBar[0].specifierText).to.equal(bazExpectedContent); |
| 75 | + }) |
| 76 | + .then(() => dependentFilesUtils.createTsSourceFile(fooFile)) |
| 77 | + .then((tsFileFoo: ts.SourceFile) => { |
| 78 | + let contentsFoo = dependentFilesUtils.getImportClauses(tsFileFoo); |
| 79 | + let bazExpectedContent = './baz.component'.replace('/', path.sep); |
| 80 | + expect(contentsFoo[0].specifierText).to.equal(bazExpectedContent); |
| 81 | + }) |
| 82 | + .then(() => resolver.resolveOwnImports()) |
| 83 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 84 | + .then(() => dependentFilesUtils.createTsSourceFile(bazFile)) |
| 85 | + .then((tsFileBaz: ts.SourceFile) => { |
| 86 | + let contentsBaz = dependentFilesUtils.getImportClauses(tsFileBaz); |
| 87 | + let barExpectedContent = path.normalize('../bar/bar.component'); |
| 88 | + let fooBarExpectedContent = path.normalize('../foo-baz/qux/quux/foobar/foobar.component'); |
| 89 | + expect(contentsBaz[0].specifierText).to.equal(barExpectedContent); |
| 90 | + expect(contentsBaz[1].specifierText).to.equal(fooBarExpectedContent); |
| 91 | + }); |
| 92 | + }); |
| 93 | + it('when no files are importing the given file', () => { |
| 94 | + let oldFilePath = path.join(rootPath, 'foo-baz/foo-baz.component.ts'); |
| 95 | + let newFilePath = path.join(rootPath, 'bar'); |
| 96 | + let resolver = new ModuleResolver(oldFilePath, newFilePath); |
| 97 | + return resolver.resolveDependentFiles() |
| 98 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 99 | + .then(() => resolver.resolveOwnImports()) |
| 100 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 101 | + .then(() => dependentFilesUtils.createTsSourceFile(fooBazFile)) |
| 102 | + .then((tsFile: ts.SourceFile) => { |
| 103 | + let contents = dependentFilesUtils.getImportClauses(tsFile); |
| 104 | + let fooExpectedContent = path.normalize('../foo/foo.component'); |
| 105 | + expect(contents[0].specifierText).to.equal(fooExpectedContent); |
| 106 | + }); |
| 107 | + }); |
| 108 | + it('when oldPath and newPath both do not have index.ts', () => { |
| 109 | + let oldFilePath = path.join(rootPath, 'bar/baz/baz.component.ts'); |
| 110 | + let newFilePath = path.join(rootPath, 'foo-baz'); |
| 111 | + let resolver = new ModuleResolver(oldFilePath, newFilePath); |
| 112 | + return resolver.resolveDependentFiles() |
| 113 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 114 | + .then(() => dependentFilesUtils.createTsSourceFile(barFile)) |
| 115 | + .then((tsFileBar: ts.SourceFile) => { |
| 116 | + let contentsBar = dependentFilesUtils.getImportClauses(tsFileBar); |
| 117 | + let bazExpectedContent = path.normalize('../foo-baz/baz.component'); |
| 118 | + expect(contentsBar[0].specifierText).to.equal(bazExpectedContent); |
| 119 | + }) |
| 120 | + .then(() => dependentFilesUtils.createTsSourceFile(fooFile)) |
| 121 | + .then((tsFileFoo: ts.SourceFile) => { |
| 122 | + let contentsFoo = dependentFilesUtils.getImportClauses(tsFileFoo); |
| 123 | + let bazExpectedContent = path.normalize('../foo-baz/baz.component'); |
| 124 | + expect(contentsFoo[0].specifierText).to.equal(bazExpectedContent); |
| 125 | + }) |
| 126 | + .then(() => resolver.resolveOwnImports()) |
| 127 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 128 | + .then(() => dependentFilesUtils.createTsSourceFile(bazFile)) |
| 129 | + .then((tsFile: ts.SourceFile) => { |
| 130 | + let contentsBaz = dependentFilesUtils.getImportClauses(tsFile); |
| 131 | + let barExpectedContent = path.normalize('../bar/bar.component'); |
| 132 | + let fooBarExpectedContent = `.${path.sep}qux${path.sep}quux${path.sep}foobar${path.sep}foobar.component`; |
| 133 | + expect(contentsBaz[0].specifierText).to.equal(barExpectedContent); |
| 134 | + expect(contentsBaz[1].specifierText).to.equal(fooBarExpectedContent); |
| 135 | + }); |
| 136 | + }); |
| 137 | + it('when there are multiple spaces between symbols and specifier', () => { |
| 138 | + let oldFilePath = path.join(rootPath, 'foo-baz/qux/quux/foobar/foobar.component.ts'); |
| 139 | + let newFilePath = path.join(rootPath, 'foo'); |
| 140 | + let resolver = new ModuleResolver(oldFilePath, newFilePath); |
| 141 | + return resolver.resolveDependentFiles() |
| 142 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 143 | + .then(() => dependentFilesUtils.createTsSourceFile(fooQuxFile)) |
| 144 | + .then((tsFileFooQux: ts.SourceFile) => { |
| 145 | + let contentsFooQux = dependentFilesUtils.getImportClauses(tsFileFooQux); |
| 146 | + let fooQuxExpectedContent = path.normalize('../../../../foo/foobar.component'); |
| 147 | + expect(contentsFooQux[0].specifierText).to.equal(fooQuxExpectedContent); |
| 148 | + }) |
| 149 | + .then(() => dependentFilesUtils.createTsSourceFile(bazFile)) |
| 150 | + .then((tsFileBaz: ts.SourceFile) => { |
| 151 | + let contentsBaz = dependentFilesUtils.getImportClauses(tsFileBaz); |
| 152 | + let bazExpectedContent = path.normalize('../../foo/foobar.component'); |
| 153 | + expect(contentsBaz[1].specifierText).to.equal(bazExpectedContent); |
| 154 | + }) |
| 155 | + .then(() => resolver.resolveOwnImports()) |
| 156 | + .then((changes) => resolver.applySortedChangePromise(changes)) |
| 157 | + .then(() => dependentFilesUtils.createTsSourceFile(fooBarFile)) |
| 158 | + .then((tsFileFooBar: ts.SourceFile) => { |
| 159 | + let contentsFooBar = dependentFilesUtils.getImportClauses(tsFileFooBar); |
| 160 | + let fooExpectedContent = `.${path.sep}foo.component`; |
| 161 | + let fooQuxExpectedContent = path.normalize('../foo-baz/qux/quux/fooqux.component'); |
| 162 | + expect(contentsFooBar[0].specifierText).to.equal(fooExpectedContent); |
| 163 | + expect(contentsFooBar[1].specifierText).to.equal(fooQuxExpectedContent); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments