|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This needs to be first so fs module can be mocked correctly. |
| 4 | +let mockFs = require('mock-fs'); |
| 5 | +import { expect, assert } from 'chai'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as ts from 'typescript'; |
| 8 | +import * as dependentFilesUtils from '../../addon/ng2/utilities/get-dependent-files'; |
| 9 | + |
| 10 | +describe('Get Dependent Files: ', () => { |
| 11 | + let rootPath = 'src/app'; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + let mockDrive = { |
| 15 | + 'src/app': { |
| 16 | + 'foo': { |
| 17 | + 'foo.component.ts': `import * from '../bar/baz/baz.component' |
| 18 | + import * from '../bar/bar.component'`, |
| 19 | + 'index.ts': `export * from './foo.component'` |
| 20 | + }, |
| 21 | + 'bar': { |
| 22 | + 'baz': { |
| 23 | + 'baz.component.ts': 'import * from "../bar.component"', |
| 24 | + 'baz.html': '<h1> Hello </h1>' |
| 25 | + }, |
| 26 | + 'bar.component.ts': `import * from './baz/baz.component' |
| 27 | + import * from '../foo'` |
| 28 | + }, |
| 29 | + 'foo-baz': { |
| 30 | + 'no-module.component.ts': '' |
| 31 | + }, |
| 32 | + 'empty-dir': {} |
| 33 | + } |
| 34 | + }; |
| 35 | + mockFs(mockDrive); |
| 36 | + }); |
| 37 | + afterEach(() => { |
| 38 | + mockFs.restore(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('getImportClauses', () => { |
| 42 | + it('returns import specifiers when there is a single import statement', () => { |
| 43 | + let sourceFile = path.join(rootPath, 'bar/baz/baz.component.ts'); |
| 44 | + return dependentFilesUtils.createTsSourceFile(sourceFile) |
| 45 | + .then((tsFile: ts.SourceFile) => { |
| 46 | + let contents = dependentFilesUtils.getImportClauses(tsFile); |
| 47 | + let expectedContents = [{ |
| 48 | + specifierText: '../bar.component', |
| 49 | + pos: 13, |
| 50 | + end: 32 |
| 51 | + }]; |
| 52 | + assert.deepEqual(contents, expectedContents); |
| 53 | + }); |
| 54 | + }); |
| 55 | + it('returns imports specifiers when there are multiple import statements', () => { |
| 56 | + let sourceFile = path.join(rootPath, 'foo/foo.component.ts'); |
| 57 | + return dependentFilesUtils.createTsSourceFile(sourceFile) |
| 58 | + .then((tsFile: ts.SourceFile) => { |
| 59 | + let contents = dependentFilesUtils.getImportClauses(tsFile); |
| 60 | + let expectedContents = [ |
| 61 | + { |
| 62 | + specifierText: '../bar/baz/baz.component', |
| 63 | + pos: 13, |
| 64 | + end: 40 |
| 65 | + }, |
| 66 | + { |
| 67 | + specifierText: '../bar/bar.component', |
| 68 | + pos: 85, |
| 69 | + end: 108 |
| 70 | + } |
| 71 | + ]; |
| 72 | + assert.deepEqual(contents, expectedContents); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('createTsSourceFile', () => { |
| 78 | + it('creates ts.SourceFile give a file path', () => { |
| 79 | + let sourceFile = path.join(rootPath, 'foo/foo.component.ts'); |
| 80 | + return dependentFilesUtils.createTsSourceFile(sourceFile) |
| 81 | + .then((tsFile: ts.SourceFile) => { |
| 82 | + let isTsSourceFile = (tsFile.kind === ts.SyntaxKind.SourceFile); |
| 83 | + expect(isTsSourceFile).to.be.true; |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('hasIndexFile', () => { |
| 89 | + it('returns true when there is a index file', () => { |
| 90 | + let sourceFile = path.join(rootPath, 'foo'); |
| 91 | + dependentFilesUtils.hasIndexFile(sourceFile) |
| 92 | + .then((booleanValue: boolean) => { |
| 93 | + expect(booleanValue).to.be.true; |
| 94 | + }); |
| 95 | + }); |
| 96 | + it('returns false when there is no index file', () => { |
| 97 | + let sourceFile = path.join(rootPath, 'bar'); |
| 98 | + dependentFilesUtils.hasIndexFile(sourceFile) |
| 99 | + .then((booleanValue: boolean) => { |
| 100 | + expect(booleanValue).to.be.false; |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('returns a map of all files which depend on a given file ', () => { |
| 106 | + it('when the given component unit has no index file', () => { |
| 107 | + let sourceFile = path.join(rootPath, 'bar/bar.component.ts'); |
| 108 | + return dependentFilesUtils.getDependentFiles(sourceFile, rootPath) |
| 109 | + .then((contents: dependentFilesUtils.ModuleMap) => { |
| 110 | + let bazFile = path.join(rootPath, 'bar/baz/baz.component.ts'); |
| 111 | + let fooFile = path.join(rootPath, 'foo/foo.component.ts'); |
| 112 | + let expectedContents: dependentFilesUtils.ModuleMap = {}; |
| 113 | + expectedContents[bazFile] = [{ |
| 114 | + specifierText: '../bar.component', |
| 115 | + pos: 13, |
<
10000
code> | 116 | + end: 32 |
| 117 | + }]; |
| 118 | + expectedContents[fooFile] = [{ |
| 119 | + specifierText: '../bar/bar.component', |
| 120 | + pos: 85, |
| 121 | + end: 108 |
| 122 | + }]; |
| 123 | + assert.deepEqual(contents, expectedContents); |
| 124 | + }); |
| 125 | + }); |
| 126 | + it('when the given component unit has no index file [More Test]', () => { |
| 127 | + let sourceFile = path.join(rootPath, 'bar/baz/baz.component.ts'); |
| 128 | + return dependentFilesUtils.getDependentFiles(sourceFile, rootPath) |
| 129 | + .then((contents: dependentFilesUtils.ModuleMap) => { |
| 130 | + let expectedContents: dependentFilesUtils.ModuleMap = {}; |
| 131 | + let barFile = path.join(rootPath, 'bar/bar.component.ts'); |
| 132 | + let fooFile = path.join(rootPath, 'foo/foo.component.ts'); |
| 133 | + expectedContents[barFile] = [{ |
| 134 | + specifierText: './baz/baz.component', |
| 135 | + pos: 13, |
| 136 | + end: 35 |
| 137 | + }]; |
| 138 | + expectedContents[fooFile] = [{ |
| 139 | + specifierText: '../bar/baz/baz.component', |
| 140 | + pos: 13, |
| 141 | + end: 40 |
| 142 | + }]; |
| 143 | + assert.deepEqual(contents, expectedContents); |
| 144 | + }); |
| 145 | + }); |
| 146 | + it('when there are no dependent files', () => { |
| 147 | + let sourceFile = path.join(rootPath, 'foo-baz/no-module.component.ts'); |
| 148 | + return dependentFilesUtils.getDependentFiles(sourceFile, rootPath) |
| 149 | + .then((contents: dependentFilesUtils.ModuleMap) => { |
| 150 | + assert.deepEqual(contents, {}); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments