1
- import * as mockFs from 'mock-fs' ;
2
- import { expect } from 'chai' ;
3
- import * as ts from 'typescript' ;
4
- import * as fs from 'fs' ;
5
- import { InsertChange , RemoveChange } from '../../addon/ng2/utilities/change' ;
6
- import * as Promise from 'ember-cli/lib/ext/promise ' ;
7
- import {
8
- findNodes ,
9
- insertAfterLastOccurrence ,
10
- addComponentToModule
11
- } from '../../addon/ng2/utilities/ast-utils' ;
1
+ import denodeify = require ( 'denodeify' ) ;
2
+ import mockFs = require ( 'mock-fs' ) ;
3
+ import ts = require ( 'typescript' ) ;
4
+ import fs = require ( 'fs' ) ;
5
+
6
+ import { InsertChange , RemoveChange } from './change ' ;
7
+ import { insertAfterLastOccurrence , addComponentToModule } from './ast-utils' ;
8
+ import { findNodes } from './node' ;
9
+ import { it } from './spec-utils' ;
10
+
11
+ const readFile = < any > denodeify ( fs . readFile ) ;
12
12
13
- const readFile = Promise . denodeify ( fs . readFile ) ;
14
13
15
14
describe ( 'ast-utils: findNodes' , ( ) => {
16
15
const sourceFile = 'tmp/tmp.ts' ;
@@ -19,7 +18,7 @@ describe('ast-utils: findNodes', () => {
19
18
let mockDrive = {
20
19
'tmp' : {
21
20
'tmp.ts' : `import * as myTest from 'tests' \n` +
22
- 'hello.'
21
+ 'hello.'
23
22
}
24
23
} ;
25
24
mockFs ( mockDrive ) ;
@@ -32,42 +31,42 @@ describe('ast-utils: findNodes', () => {
32
31
it ( 'finds no imports' , ( ) => {
33
32
let editedFile = new RemoveChange ( sourceFile , 0 , `import * as myTest from 'tests' \n` ) ;
34
33
return editedFile
35
- . apply ( )
36
- . then ( ( ) => {
37
- let rootNode = getRootNode ( sourceFile ) ;
38
- let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
39
- expect ( nodes ) . to . be . empty ;
40
- } ) ;
34
+ . apply ( )
35
+ . then ( ( ) => {
36
+ let rootNode = getRootNode ( sourceFile ) ;
37
+ let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
38
+ expect ( nodes ) . toEqual ( [ ] ) ;
39
+ } ) ;
41
40
} ) ;
42
41
it ( 'finds one import' , ( ) => {
43
42
let rootNode = getRootNode ( sourceFile ) ;
44
43
let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
45
- expect ( nodes . length ) . to . equal ( 1 ) ;
44
+ expect ( nodes . length ) . toEqual ( 1 ) ;
46
45
} ) ;
47
46
it ( 'finds two imports from inline declarations' , ( ) => {
48
47
// remove new line and add an inline import
49
48
let editedFile = new RemoveChange ( sourceFile , 32 , '\n' ) ;
50
49
return editedFile
51
- . apply ( )
52
- . then ( ( ) => {
53
- let insert = new InsertChange ( sourceFile , 32 , `import {Routes} from '@angular/routes'` ) ;
54
- return insert . apply ( ) ;
55
- } )
56
- . then ( ( ) => {
57
- let rootNode = getRootNode ( sourceFile ) ;
58
- let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
59
- expect ( nodes . length ) . to . equal ( 2 ) ;
60
- } ) ;
50
+ . apply ( )
51
+ . then ( ( ) => {
52
+ let insert = new InsertChange ( sourceFile , 32 , `import {Routes} from '@angular/routes'` ) ;
53
+ return insert . apply ( ) ;
54
+ } )
55
+ . then ( ( ) => {
56
+ let rootNode = getRootNode ( sourceFile ) ;
57
+ let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
58
+ expect ( nodes . length ) . toEqual ( 2 ) ;
59
+ } ) ;
61
60
} ) ;
62
61
it ( 'finds two imports from new line separated declarations' , ( ) => {
63
62
let editedFile = new InsertChange ( sourceFile , 33 , `import {Routes} from '@angular/routes'` ) ;
64
63
return editedFile
65
- . apply ( )
66
- . then ( ( ) => {
67
- let rootNode = getRootNode ( sourceFile ) ;
68
- let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
69
- expect ( nodes . length ) . to . equal ( 2 ) ;
70
- } ) ;
64
+ . apply ( )
65
+ . then ( ( ) => {
66
+ let rootNode = getRootNode ( sourceFile ) ;
67
+ let nodes = findNodes ( rootNode , ts . SyntaxKind . ImportDeclaration ) ;
68
+ expect ( nodes . length ) . toEqual ( 2 ) ;
69
+ } ) ;
71
70
} ) ;
72
71
} ) ;
73
72
@@ -89,86 +88,93 @@ describe('ast-utils: insertAfterLastOccurrence', () => {
89
88
it ( 'inserts at beginning of file' , ( ) => {
90
89
let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
91
90
return insertAfterLastOccurrence ( imports , `\nimport { Router } from '@angular/router';` ,
92
- sourceFile , 0 )
93
- . apply ( )
94
- . then ( ( ) => {
95
- return readFile ( sourceFile , 'utf8' ) ;
96
- } ) . then ( ( content ) => {
97
- let expected = '\nimport { Router } from \'@angular/router\';' ;
98
- expect ( content ) . to . equal ( expected ) ;
99
- } ) ;
91
+ sourceFile , 0 )
92
+ . apply ( )
93
+ . then ( ( ) => {
94
+ return readFile ( sourceFile , 'utf8' ) ;
95
+ } ) . then ( ( content ) => {
96
+ let expected = '\nimport { Router } from \'@angular/router\';' ;
97
+ expect ( content ) . toEqual ( expected ) ;
98
+ } ) ;
100
99
} ) ;
101
100
it ( 'throws an error if first occurence with no fallback position' , ( ) => {
102
101
let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
103
102
expect ( ( ) => insertAfterLastOccurrence ( imports , `import { Router } from '@angular/router';` ,
104
- sourceFile ) ) . to . throw ( Error ) ;
103
+ sourceFile ) ) . toThrowError ( ) ;
105
104
} ) ;
106
105
it ( 'inserts after last import' , ( ) => {
107
106
let content = `import { foo, bar } from 'fizz';` ;
108
107
let editedFile = new InsertChange ( sourceFile , 0 , content ) ;
109
108
return editedFile
110
- . apply ( )
111
- . then ( ( ) => {
112
- let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
113
- return insertAfterLastOccurrence ( imports , ', baz' , sourceFile ,
114
- 0 , ts . SyntaxKind . Identifier )
115
- . apply ( ) ;
116
- } ) . then ( ( ) => {
117
- return readFile ( sourceFile , 'utf8' ) ;
118
- } ) . then ( newContent => expect ( newContent ) . to . equal ( `import { foo, bar, baz } from 'fizz';` ) ) ;
109
+ . apply ( )
110
+ . then ( ( ) => {
111
+ let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
112
+ return insertAfterLastOccurrence ( imports , ', baz' , sourceFile ,
113
+ 0 , ts . SyntaxKind . Identifier )
114
+ . apply ( ) ;
115
+ } )
116
+ . then ( ( ) => {
117
+ return readFile ( sourceFile , 'utf8' ) ;
118
+ } )
119
+ . then ( newContent => expect ( newContent ) . toEqual ( `import { foo, bar, baz } from 'fizz';` ) ) ;
119
120
} ) ;
120
121
it ( 'inserts after last import declaration' , ( ) => {
121
122
let content = `import * from 'foo' \n import { bar } from 'baz'` ;
122
123
let editedFile = new InsertChange ( sourceFile , 0 , content ) ;
123
124
return editedFile
124
- . apply ( )
125
- . then ( ( ) => {
126
- let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
127
- return insertAfterLastOccurrence ( imports , `\nimport Router from '@angular/router'` ,
128
- sourceFile )
129
- . apply ( ) ;
130
- } ) . then ( ( ) => {
131
- return readFile ( sourceFile , 'utf8' ) ;
132
- } ) . then ( newContent => {
133
- let expected = `import * from 'foo' \n import { bar } from 'baz'` +
134
- `\nimport Router from '@angular/router'` ;
135
- expect ( newContent ) . to . equal ( expected ) ;
136
- } ) ;
125
+ . apply ( )
126
+ . then ( ( ) => {
127
+ let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
128
+ return insertAfterLastOccurrence ( imports , `\nimport Router from '@angular/router'` ,
129
+ sourceFile )
130
+ . apply ( ) ;
131
+ } )
132
+ . then ( ( ) => {
133
+ return readFile ( sourceFile , 'utf8' ) ;
134
+ } )
135
+ . then ( newContent => {
136
+ let expected = `import * from 'foo' \n import { bar } from 'baz'` +
137
+ `\nimport Router from '@angular/router'` ;
138
+ expect ( newContent ) . toEqual ( expected ) ;
139
+ } ) ;
137
140
} ) ;
138
141
it ( 'inserts correctly if no imports' , ( ) => {
139
142
let content = `import {} from 'foo'` ;
140
143
let editedFile = new InsertChange ( sourceFile , 0 , content ) ;
141
144
return editedFile
142
- . apply ( )
143
- . then ( ( ) => {
144
- let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
145
- return insertAfterLastOccurrence ( imports , ', bar' , sourceFile , undefined ,
146
- ts . SyntaxKind . Identifier )
147
- . apply ( ) ;
148
- } ) . catch ( ( ) => {
149
- return readFile ( sourceFile , 'utf8' ) ;
150
- } )
151
- . then ( newContent => {
152
- expect ( newContent ) . to . equal ( content ) ;
153
- // use a fallback position for safety
154
- let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
155
- let pos = findNodes ( imports . sort ( ( a , b ) => a . pos - b . pos ) . pop ( ) ,
156
- ts . SyntaxKind . CloseBraceToken ) . pop ( ) . pos ;
157
- return insertAfterLastOccurrence ( imports , ' bar ' ,
158
- sourceFile , pos , ts . SyntaxKind . Identifier )
159
- . apply ( ) ;
160
- } ) . then ( ( ) => {
161
- return readFile ( sourceFile , 'utf8' ) ;
162
- } ) . then ( newContent => {
163
- expect ( newContent ) . to . equal ( `import { bar } from 'foo'` ) ;
164
- } ) ;
145
+ . apply ( )
146
+ . then ( ( ) => {
147
+ let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
148
+ return insertAfterLastOccurrence ( imports , ', bar' , sourceFile , undefined ,
149
+ ts . SyntaxKind . Identifier )
150
+ . apply ( ) ;
151
+ } )
152
+ . catch ( ( ) => {
153
+ return readFile ( sourceFile , 'utf8' ) ;
154
+ } )
155
+ . then ( newContent => {
156
+ expect ( newContent ) . toEqual ( content ) ;
157
+ // use a fallback position for safety
158
+ let imports = getNodesOfKind ( ts . SyntaxKind . ImportDeclaration , sourceFile ) ;
159
+ let pos = findNodes ( imports . sort ( ( a , b ) => a . pos - b . pos ) . pop ( ) ,
160
+ ts . SyntaxKind . CloseBraceToken ) . pop ( ) . pos ;
161
+ return insertAfterLastOccurrence ( imports , ' bar ' ,
162
+ sourceFile , pos , ts . SyntaxKind . Identifier )
163
+ . apply ( ) ;
164
+ } )
165
+ . then ( ( ) => {
166
+ return readFile ( sourceFile , 'utf8' ) ;
167
+ } )
168
+ . then ( newContent => {
169
+ expect ( newContent ) . toEqual ( `import { bar } from 'foo'` ) ;
170
+ } ) ;
165
171
} ) ;
166
172
} ) ;
167
173
168
174
169
175
describe ( 'addComponentToModule' , ( ) => {
170
176
beforeEach ( ( ) => {
171
- mockFs ( {
177
+ mockFs ( {
172
178
'1.ts' : `
173
179
import {NgModule} from '@angular/core';
174
180
@@ -208,7 +214,7 @@ class Module {}`
208
214
. then ( change => change . apply ( ) )
209
215
. then ( ( ) => readFile ( '1.ts' , 'utf-8' ) )
210
216
. then ( content => {
211
- expect ( content ) . to . equal (
217
+ expect ( content ) . toEqual (
212
218
'\n' +
213
219
'import {NgModule} from \'@angular/core\';\n' +
214
220
'import { MyClass } from \'MyImportPath\';\n' +
@@ -218,15 +224,15 @@ class Module {}`
218
224
'})\n' +
219
225
'class Module {}'
220
226
) ;
221
- } )
227
+ } ) ;
222
228
} ) ;
223
229
224
230
it ( 'works with array with declarations' , ( ) => {
225
231
return addComponentToModule ( '2.ts' , 'MyClass' , 'MyImportPath' )
226
232
. then ( change => change . apply ( ) )
227
233
. then ( ( ) => readFile ( '2.ts' , 'utf-8' ) )
228
234
. then ( content => {
229
- expect ( content ) . to . equal (
235
+ expect ( content ) . toEqual (
230
236
'\n' +
231
237
'import {NgModule} from \'@angular/core\';\n' +
232
238
'import { MyClass } from \'MyImportPath\';\n' +
@@ -239,15 +245,15 @@ class Module {}`
239
245
'})\n' +
240
246
'class Module {}'
241
247
) ;
242
- } )
248
+ } ) ;
243
249
} ) ;
244
250
245
251
it ( 'works without any declarations' , ( ) => {
246
252
return addComponentToModule ( '3.ts' , 'MyClass' , 'MyImportPath' )
247
253
. then ( change => change . apply ( ) )
248
254
. then ( ( ) => readFile ( '3.ts' , 'utf-8' ) )
249
255
. then ( content => {
250
- expect ( content ) . to . equal (
256
+ expect ( content ) . toEqual (
251
257
'\n' +
252
258
'import {NgModule} from \'@angular/core\';\n' +
253
259
'import { MyClass } from \'MyImportPath\';\n' +
@@ -257,15 +263,15 @@ class Module {}`
257
263
'})\n' +
258
264
'class Module {}'
259
265
) ;
260
- } )
266
+ } ) ;
261
267
} ) ;
262
268
263
269
it ( 'works without a declaration field' , ( ) => {
264
270
return addComponentToModule ( '4.ts' , 'MyClass' , 'MyImportPath' )
265
271
. then ( change => change . apply ( ) )
266
272
. then ( ( ) => readFile ( '4.ts' , 'utf-8' ) )
267
273
. then ( content => {
268
- expect ( content ) . to . equal (
274
+ expect ( content ) . toEqual (
269
275
'\n' +
270
276
'import {NgModule} from \'@angular/core\';\n' +
271
277
'import { MyClass } from \'MyImportPath\';\n' +
@@ -277,11 +283,11 @@ class Module {}`
277
283
'})\n' +
278
284
'class Module {}'
279
285
) ;
280
- } )
286
+ } ) ;
281
287
} ) ;
282
288
} ) ;
283
289
284
- /**
290
+ /**
285
291
* Gets node of kind kind from sourceFile
286
292
*/
287
293
function getNodesOfKind ( kind : ts . SyntaxKind , sourceFile : string ) {
@@ -290,5 +296,5 @@ function getNodesOfKind(kind: ts.SyntaxKind, sourceFile: string) {
290
296
291
297
function getRootNode ( sourceFile : string ) {
292
298
return ts . createSourceFile ( sourceFile , fs . readFileSync ( sourceFile ) . toString ( ) ,
293
- ts . ScriptTarget . ES6 , true ) ;
299
+ ts . ScriptTarget . ES6 , true ) ;
294
300
}
0 commit comments