Repository navigation
Expand file tree
/
Copy pathinProject.test.ts
More file actions
97 lines (85 loc) · 2.93 KB
/
Copy pathinProject.test.ts
File metadata and controls
97 lines (85 loc) · 2.93 KB
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
import assert from "node:assert/strict";
import test from "node:test";
import {
IN_PROJECT_CONTEXT_KEY,
MCPP_MANIFEST_GLOB,
registerInProjectContext,
updateInProjectContext,
type InProjectEnvironment,
} from "../src/inProject";
interface FakeState {
project: unknown | undefined;
contextValues: Map<string, boolean>;
changeListeners: Array<() => void>;
disposed: boolean;
}
function fakeEnvironment(project?: unknown): { env: InProjectEnvironment; state: FakeState } {
const state: FakeState = {
project,
contextValues: new Map(),
changeListeners: [],
disposed: false,
};
const env: InProjectEnvironment = {
currentProject: () => state.project,
setContextValue: (key, value) => {
state.contextValues.set(key, value);
return Promise.resolve();
},
subscribe: (listener) => {
state.changeListeners.push(listener);
return [{
dispose: () => {
state.disposed = true;
},
}];
},
};
return { env, state };
}
async function flushAsync(): Promise<void> {
await new Promise((resolve) => setImmediate(resolve));
}
test("exposes the fixed context key and manifest glob", () => {
assert.equal(IN_PROJECT_CONTEXT_KEY, "mcpp.inProject");
assert.equal(MCPP_MANIFEST_GLOB, "**/mcpp.toml");
});
test("sets the context key to true when the active resource resolves to an mcpp project", async () => {
const { env, state } = fakeEnvironment({ root: "/work/A" });
const result = await updateInProjectContext(env);
assert.equal(result, true);
assert.equal(state.contextValues.get(IN_PROJECT_CONTEXT_KEY), true);
});
test("sets the context key to false when the active resource has no mcpp project", async () => {
const { env, state } = fakeEnvironment();
const result = await updateInProjectContext(env);
assert.equal(result, false);
assert.equal(state.contextValues.get(IN_PROJECT_CONTEXT_KEY), false);
});
test("writes the context on registration and owns the change subscriptions", async () => {
const { env, state } = fakeEnvironment({ root: "/work/A" });
const registration = registerInProjectContext(env);
await flushAsync();
assert.equal(state.contextValues.get(IN_PROJECT_CONTEXT_KEY), true);
assert.equal(state.changeListeners.length, 1);
registration.dispose();
assert.equal(state.disposed, true);
});
test("re-evaluates the context when the active project changes", async () => {
const { env, state } = fakeEnvironment();
registerInProjectContext(env);
await flushAsync();
assert.equal(state.contextValues.get(IN_PROJECT_CONTEXT_KEY), false);
state.project = { root: "/work/A" };
for (const listener of state.changeListeners) {
listener();
}
await flushAsync();
assert.equal(state.contextValues.get(IN_PROJECT_CONTEXT_KEY), true);
state.project = undefined;
for (const listener of state.changeListeners) {
listener();
}
await flushAsync();
assert.equal(state.contextValues.get(IN_PROJECT_CONTEXT_KEY), false);
});