forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchange.spec.ts
131 lines (120 loc) · 4.65 KB
/
change.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
'use strict';
// This needs to be first so fs module can be mocked correctly.
let mockFs = require('mock-fs');
import {it} from './spec-utils';
import {InsertChange, NodeHost, RemoveChange, ReplaceChange} from './change';
import fs = require('fs');
let path = require('path');
let Promise = require('angular-cli/ember-cli/lib/ext/promise');
const readFile = Promise.denodeify(fs.readFile);
describe('Change', () => {
let sourcePath = 'src/app/my-component';
beforeEach(() => {
let mockDrive = {
'src/app/my-component': {
'add-file.txt': 'hello',
'remove-replace-file.txt': 'import * as foo from "./bar"',
'replace-file.txt': 'import { FooComponent } from "./baz"'
}
};
mockFs(mockDrive);
});
afterEach(() => {
mockFs.restore();
});
describe('InsertChange', () => {
let sourceFile = path.join(sourcePath, 'add-file.txt');
it('adds text to the source code', () => {
let changeInstance = new InsertChange(sourceFile, 6, ' world!');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('hello world!');
});
});
it('fails for negative position', () => {
expect(() => new InsertChange(sourceFile, -6, ' world!')).toThrowError();
});
it('adds nothing in the source code if empty string is inserted', () => {
let changeInstance = new InsertChange(sourceFile, 6, '');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('hello');
});
});
});
describe('RemoveChange', () => {
let sourceFile = path.join(sourcePath, 'remove-replace-file.txt');
it('removes given text from the source code', () => {
let changeInstance = new RemoveChange(sourceFile, 9, 'as foo');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('import * from "./bar"');
});
});
it('fails for negative position', () => {
expect(() => new RemoveChange(sourceFile, -6, ' world!')).toThrow();
});
it('does not change the file if told to remove empty string', () => {
let changeInstance = new RemoveChange(sourceFile, 9, '');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('import * as foo from "./bar"');
});
});
});
describe('ReplaceChange', () => {
it('replaces the given text in the source code', () => {
let sourceFile = path.join(sourcePath, 'remove-replace-file.txt');
let changeInstance = new ReplaceChange(sourceFile, 7, '* as foo', '{ fooComponent }');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('import { fooComponent } from "./bar"');
});
});
it('fails for negative position', () => {
let sourceFile = path.join(sourcePath, 'remove-replace-file.txt');
expect(() => new ReplaceChange(sourceFile, -6, 'hello', ' world!')).toThrow();
});
it('fails for invalid replacement', () => {
let sourceFile = path.join(sourcePath, 'replace-file.txt');
let changeInstance = new ReplaceChange(sourceFile, 0, 'foobar', '');
return changeInstance
.apply(NodeHost)
.then(() => expect(false).toBe(true), err => {
// Check that the message contains the string to replace and the string from the file.
expect(err.message).toContain('foobar');
expect(err.message).toContain('import');
});
});
it('adds string to the position of an empty string', () => {
let sourceFile = path.join(sourcePath, 'replace-file.txt');
let changeInstance = new ReplaceChange(sourceFile, 9, '', 'BarComponent, ');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('import { BarComponent, FooComponent } from "./baz"');
});
});
it('removes the given string only if an empty string to add is given', () => {
let sourceFile = path.join(sourcePath, 'remove-replace-file.txt');
let changeInstance = new ReplaceChange(sourceFile, 8, ' as foo', '');
return changeInstance
.apply(NodeHost)
.then(() => readFile(sourceFile, 'utf8'))
.then(contents => {
expect(contents).toEqual('import * from "./bar"');
});
});
});
});