This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
/
OrganizeImportsOnSaveExtension.test.ts
128 lines (100 loc) · 4.42 KB
/
OrganizeImportsOnSaveExtension.test.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
import * as chai from 'chai';
import { join } from 'path';
import { DeclarationIndex } from 'typescript-parser';
import * as vscode from 'vscode';
import { Container } from '../../../../src/extension/IoC';
import { iocSymbols } from '../../../../src/extension/IoCSymbols';
import { ImportManager } from '../../../../src/extension/managers';
chai.should();
describe('OrganizeImportsOnSaveExtension', () => {
const rootPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
let document: vscode.TextDocument;
let index: DeclarationIndex;
before(async () => {
const file = join(
rootPath,
'extension/extensions/organizeImportsOnSaveExtension/organizeFile.ts',
);
document = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(document);
index = new DeclarationIndex(Container.get(iocSymbols.typescriptParser), rootPath);
await index.buildIndex(
[
join(
rootPath,
'server/indices/MyClass.ts',
),
],
);
const config = vscode.workspace.getConfiguration('typescriptHero');
await config.update('resolver.organizeOnSave', true);
});
after(async () => {
const config = vscode.workspace.getConfiguration('typescriptHero');
await config.update('resolver.organizeOnSave', false);
});
afterEach(async () => {
await vscode.window.activeTextEditor!.edit((builder) => {
builder.delete(new vscode.Range(
new vscode.Position(0, 0),
document.lineAt(document.lineCount - 1).rangeIncludingLineBreak.end,
));
});
});
it('should remove an unused import on save', async function () {
this.timeout(4000);
const ctrl = await ImportManager.create(document);
const declaration = index.declarationInfos.find(o => o.declaration.name === 'Class1');
ctrl.addDeclarationImport(declaration!);
await ctrl.commit();
document.lineAt(0).text.should.equals(
`import { Class1 } from '../../../server/indices/MyClass';`,
);
await document.save();
document.lineAt(0).text.should.equals('');
});
it('should not remove a used import on save', async function () {
this.timeout(4000);
const ctrl = await ImportManager.create(document);
const declaration = index.declarationInfos.find(o => o.declaration.name === 'Class1');
const declaration2 = index.declarationInfos.find(o => o.declaration.name === 'Class2');
ctrl.addDeclarationImport(declaration!).addDeclarationImport(declaration2!);
await ctrl.commit();
document.lineAt(0).text.should.equals(
`import { Class1, Class2 } from '../../../server/indices/MyClass';`,
);
await vscode.window.activeTextEditor!.edit((builder) => {
builder.insert(
new vscode.Position(1, 0),
'let a = new Class2()',
);
});
await document.save();
document.lineAt(0).text.should.equals(
`import { Class2 } from '../../../server/indices/MyClass';`,
);
});
it('should not remove an unused import on save when disableImportRemovalOnOrganize is true', async () => {
const config = vscode.workspace.getConfiguration('typescriptHero');
await config.update('resolver.disableImportRemovalOnOrganize', true);
const ctrl = await ImportManager.create(document);
const declaration = index.declarationInfos.find(o => o.declaration.name === 'Class1');
const declaration2 = index.declarationInfos.find(o => o.declaration.name === 'Class2');
ctrl.addDeclarationImport(declaration!).addDeclarationImport(declaration2!);
await ctrl.commit();
document.lineAt(0).text.should.equals(
`import { Class1, Class2 } from '../../../server/indices/MyClass';`,
);
await vscode.window.activeTextEditor!.edit((builder) => {
builder.insert(
new vscode.Position(1, 0),
'let a = new Class2()',
);
});
await document.save();
document.lineAt(0).text.should.equals(
`import { Class1, Class2 } from '../../../server/indices/MyClass';`,
);
await config.update('resolver.disableImportRemovalOnOrganize', false);
});
});