forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-dependent-files.spec.ts
217 lines (209 loc) · 7.85 KB
/
get-dependent-files.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
// This needs to be first so fs module can be mocked correctly.
let mockFs = require('mock-fs');
import { expect, assert } from 'chai';
import * as path from 'path';
import * as ts from 'typescript';
import * as dependentFilesUtils from 'angular-cli/utilities/get-dependent-files';
describe('Get Dependent Files: ', () => {
let rootPath = 'src/app';
beforeEach(() => {
let mockDrive = {
'src/app': {
'foo': {
'foo.component.ts': `import * from '../bar/baz/baz.component'
import * from '../bar/bar.component'`,
'foo.component.html': '',
'foo.component.css': '',
'foo.component.spec.ts': '',
'foo.ts': '',
'index.ts': `export * from './foo.component'`
},
'bar': {
'baz': {
'baz.component.ts': 'import * from "../bar.component"',
'baz.html': '<h1> Hello </h1>'
},
'bar.component.ts': `import * from './baz/baz.component'
import * from '../foo'`,
'bar.component.spec.ts': ''
},
'foo-baz': {
'no-module.component.ts': '',
'no-module.component.spec.ts': 'import * from "../bar/bar.component";'
},
'quux': {
'quux.ts': '',
'quux.html': '',
'quux.css': '',
'quux.spec.ts': ''
},
'noAngular.tag.ts': '',
'noAngular.tag.html': '',
'noAngular.tag.sass': '',
'noAngular.tag.spec.ts': '',
}
};
mockFs(mockDrive);
});
afterEach(() => {
mockFs.restore();
});
describe('getImportClauses', () => {
it('returns import specifiers when there is a single import statement', () => {
let sourceFile = path.join(rootPath, 'bar/baz/baz.component.ts');
return dependentFilesUtils.createTsSourceFile(sourceFile)
.then((tsFile: ts.SourceFile) => {
let contents = dependentFilesUtils.getImportClauses(tsFile);
let expectedContents = [{
specifierText: '../bar.component',
pos: 13,
end: 32
}];
assert.deepEqual(contents, expectedContents);
});
});
it('returns imports specifiers when there are multiple import statements', () => {
let sourceFile = path.join(rootPath, 'foo/foo.component.ts');
return dependentFilesUtils.createTsSourceFile(sourceFile)
.then((tsFile: ts.SourceFile) => {
let contents = dependentFilesUtils.getImportClauses(tsFile);
let expectedContents = [
{
specifierText: '../bar/baz/baz.component',
pos: 13,
end: 40
},
{
specifierText: '../bar/bar.component',
pos: 85,
end: 108
}
];
assert.deepEqual(contents, expectedContents);
});
});
});
describe('createTsSourceFile', () => {
it('creates ts.SourceFile give a file path', () => {
let sourceFile = path.join(rootPath, 'foo/foo.component.ts');
return dependentFilesUtils.createTsSourceFile(sourceFile)
.then((tsFile: ts.SourceFile) => {
let isTsSourceFile = (tsFile.kind === ts.SyntaxKind.SourceFile);
expect(isTsSourceFile).to.be.true;
});
});
});
describe('hasIndexFile', () => {
it('returns true when there is a index file', () => {
let sourceFile = path.join(rootPath, 'foo');
dependentFilesUtils.hasIndexFile(sourceFile)
.then((booleanValue: boolean) => {
expect(booleanValue).to.be.true;
});
});
it('returns false when there is no index file', () => {
let sourceFile = path.join(rootPath, 'bar');
dependentFilesUtils.hasIndexFile(sourceFile)
.then((booleanValue: boolean) => {
expect(booleanValue).to.be.false;
});
});
});
describe('returns an array of all the associated files of a given component unit.', () => {
it('when the component name has a special Angular tag(component/pipe/service)', () => {
let sourceFile = path.join(rootPath, 'foo/foo.component.ts');
return dependentFilesUtils.getAllAssociatedFiles(sourceFile)
.then((files: string[]) => {
let expectedContents = [
'src/app/foo/foo.component.css',
'src/app/foo/foo.component.html',
'src/app/foo/foo.component.spec.ts',
'src/app/foo/foo.component.ts'
];
assert.deepEqual(files, expectedContents);
});
});
it('when the component name has non-Angular tag', () => {
let sourceFile = path.join(rootPath, 'noAngular.tag.ts');
return dependentFilesUtils.getAllAssociatedFiles(sourceFile)
.then((files: string[]) => {
let expectedContents = [
'src/app/noAngular.tag.html',
'src/app/noAngular.tag.sass',
'src/app/noAngular.tag.spec.ts',
'src/app/noAngular.tag.ts'
];
assert.deepEqual(files, expectedContents);
});
});
it('when the component name has no tag after the unique file name', () => {
let sourceFile = path.join(rootPath, 'quux/quux.ts');
return dependentFilesUtils.getAllAssociatedFiles(sourceFile)
.then((files: string[]) => {
let expectedContents = [
'src/app/quux/quux.css',
'src/app/quux/quux.html',
'src/app/quux/quux.spec.ts',
'src/app/quux/quux.ts'
];
assert.deepEqual(files, expectedContents);
});
});
});
describe('returns a map of all files which depend on a given file ', () => {
it('when the given component unit has no index file', () => {
let sourceFile = path.join(rootPath, 'bar/bar.component.ts');
return dependentFilesUtils.getDependentFiles(sourceFile, rootPath)
.then((contents: dependentFilesUtils.ModuleMap) => {
let bazFile = path.join(rootPath, 'bar/baz/baz.component.ts');
let fooFile = path.join(rootPath, 'foo/foo.component.ts');
let noModuleSpecFile = path.join(rootPath, 'foo-baz/no-module.component.spec.ts');
let expectedContents: dependentFilesUtils.ModuleMap = {};
expectedContents[bazFile] = [{
specifierText: '../bar.component',
pos: 13,
end: 32
}];
expectedContents[fooFile] = [{
specifierText: '../bar/bar.component',
pos: 85,
end: 108
}];
expectedContents[noModuleSpecFile] = [{
specifierText: '../bar/bar.component',
pos: 13,
end: 36
}];
assert.deepEqual(contents, expectedContents);
});
});
it('when the given component unit has no index file [More Test]', () => {
let sourceFile = path.join(rootPath, 'bar/baz/baz.component.ts');
return dependentFilesUtils.getDependentFiles(sourceFile, rootPath)
.then((contents: dependentFilesUtils.ModuleMap) => {
let expectedContents: dependentFilesUtils.ModuleMap = {};
let barFile = path.join(rootPath, 'bar/bar.component.ts');
let fooFile = path.join(rootPath, 'foo/foo.component.ts');
expectedContents[barFile] = [{
specifierText: './baz/baz.component',
pos: 13,
end: 35
}];
expectedContents[fooFile] = [{
specifierText: '../bar/baz/baz.component',
pos: 13,
end: 40
}];
assert.deepEqual(contents, expectedContents);
});
});
it('when there are no dependent files', () => {
let sourceFile = path.join(rootPath, 'foo-baz/no-module.component.ts');
return dependentFilesUtils.getDependentFiles(sourceFile, rootPath)
.then((contents: dependentFilesUtils.ModuleMap) => {
assert.deepEqual(contents, {});
});
});
});
});