• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.projectSystem {
2    describe("unittests:: tsserver:: syntax operations", () => {
3        function navBarFull(session: TestSession, file: File) {
4            return JSON.stringify(session.executeCommandSeq<protocol.FileRequest>({
5                command: protocol.CommandTypes.NavBarFull,
6                arguments: { file: file.path }
7            }).response);
8        }
9
10        function openFile(session: TestSession, file: File) {
11            session.executeCommandSeq<protocol.OpenRequest>({
12                command: protocol.CommandTypes.Open,
13                arguments: { file: file.path, fileContent: file.content }
14            });
15        }
16
17        it("works when file is removed and added with different content", () => {
18            const app: File = {
19                path: `${tscWatch.projectRoot}/app.ts`,
20                content: "console.log('Hello world');"
21            };
22            const unitTest1: File = {
23                path: `${tscWatch.projectRoot}/unitTest1.ts`,
24                content: `import assert = require('assert');
25
26describe("Test Suite 1", () => {
27    it("Test A", () => {
28        assert.ok(true, "This shouldn't fail");
29    });
30
31    it("Test B", () => {
32        assert.ok(1 === 1, "This shouldn't fail");
33        assert.ok(false, "This should fail");
34    });
35});`
36            };
37            const tsconfig: File = {
38                path: `${tscWatch.projectRoot}/tsconfig.json`,
39                content: "{}"
40            };
41            const files = [app, libFile, tsconfig];
42            const host = createServerHost(files);
43            const session = createSession(host);
44            const service = session.getProjectService();
45            openFile(session, app);
46
47            checkNumberOfProjects(service, { configuredProjects: 1 });
48            const project = service.configuredProjects.get(tsconfig.path)!;
49            const expectedFilesWithoutUnitTest1 = files.map(f => f.path);
50            checkProjectActualFiles(project, expectedFilesWithoutUnitTest1);
51
52            host.writeFile(unitTest1.path, unitTest1.content);
53            host.runQueuedTimeoutCallbacks();
54            const expectedFilesWithUnitTest1 = expectedFilesWithoutUnitTest1.concat(unitTest1.path);
55            checkProjectActualFiles(project, expectedFilesWithUnitTest1);
56
57            openFile(session, unitTest1);
58            checkProjectActualFiles(project, expectedFilesWithUnitTest1);
59
60            const navBarResultUnitTest1 = navBarFull(session, unitTest1);
61            host.deleteFile(unitTest1.path);
62            host.checkTimeoutQueueLengthAndRun(0);
63            checkProjectActualFiles(project, expectedFilesWithUnitTest1);
64
65            session.executeCommandSeq<protocol.CloseRequest>({
66                command: protocol.CommandTypes.Close,
67                arguments: { file: unitTest1.path }
68            });
69            host.checkTimeoutQueueLengthAndRun(2);
70            checkProjectActualFiles(project, expectedFilesWithoutUnitTest1);
71
72            const unitTest1WithChangedContent: File = {
73                path: unitTest1.path,
74                content: `import assert = require('assert');
75
76export function Test1() {
77    assert.ok(true, "This shouldn't fail");
78};
79
80export function Test2() {
81    assert.ok(1 === 1, "This shouldn't fail");
82    assert.ok(false, "This should fail");
83};`
84            };
85            host.writeFile(unitTest1.path, unitTest1WithChangedContent.content);
86            host.runQueuedTimeoutCallbacks();
87            checkProjectActualFiles(project, expectedFilesWithUnitTest1);
88
89            openFile(session, unitTest1WithChangedContent);
90            checkProjectActualFiles(project, expectedFilesWithUnitTest1);
91            const sourceFile = project.getLanguageService().getNonBoundSourceFile(unitTest1WithChangedContent.path);
92            assert.strictEqual(sourceFile.text, unitTest1WithChangedContent.content);
93
94            const navBarResultUnitTest1WithChangedContent = navBarFull(session, unitTest1WithChangedContent);
95            assert.notStrictEqual(navBarResultUnitTest1WithChangedContent, navBarResultUnitTest1, "With changes in contents of unitTest file, we should see changed naviagation bar item result");
96        });
97    });
98}
99