-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathcontracts.ts
More file actions
132 lines (117 loc) · 4.23 KB
/
Copy pathcontracts.ts
File metadata and controls
132 lines (117 loc) · 4.23 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
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
132
import { assert } from "chai";
import { getContractName, Injector, provide } from "../lib/common/di";
import type { InjectionToken, ProviderToken } from "../lib/common/di";
import {
ChildProcess,
DevicesService,
DoctorService,
Errors,
FileSystem,
HostInfo,
HttpClient,
Logger,
PackageManager,
ProjectData,
ProjectDataService,
ProjectNameService,
Prompter,
TempService,
ViteHmrPortService,
PBXPROJ_DOM_XCODE,
XCODE,
} from "../lib/contracts";
import { Yok } from "../lib/common/yok";
import { Logger as LoggerImpl } from "../lib/common/logger/logger";
import { Errors as ErrorsImpl } from "../lib/common/errors";
import { FileSystem as FileSystemImpl } from "../lib/common/file-system";
import type { ISpawnResult } from "../lib/common/declarations";
const tranche: [ProviderToken<any>, string][] = [
[ChildProcess, "childProcess"],
[DevicesService, "devicesService"],
[DoctorService, "doctorService"],
[Errors, "errors"],
[FileSystem, "fs"],
[HostInfo, "hostInfo"],
[HttpClient, "httpClient"],
[Logger, "logger"],
[PackageManager, "packageManager"],
[ProjectData, "projectData"],
[ProjectDataService, "projectDataService"],
[ProjectNameService, "projectNameService"],
[Prompter, "prompter"],
[TempService, "tempService"],
[ViteHmrPortService, "viteHmrPortService"],
];
describe("contracts tranche", () => {
it("carries decorator-set token names matching the Yok-era registrations", () => {
for (const [token, legacyName] of tranche) {
assert.equal(getContractName(token), legacyName);
}
});
it("resolves via the class, the legacy name, and the $-spelling to one instance", () => {
class StubDoctorService extends DoctorService {
async printWarnings(): Promise<void> {}
async runSetupScript(): Promise<ISpawnResult> {
return <ISpawnResult>{};
}
async canExecuteLocalBuild(): Promise<boolean> {
return true;
}
checkForDeprecatedShortImportsInAppDir(): void {}
}
const injector = new Injector([provide(DoctorService, StubDoctorService)]);
const byClass = injector.get(DoctorService);
assert.instanceOf(byClass, StubDoctorService);
assert.strictEqual(injector.get("doctorService"), byClass);
assert.strictEqual(injector.get("$doctorService"), byClass);
});
for (const [token, legacyName] of tranche) {
it(`aliases '${legacyName}' by class, by name and by $-spelling`, () => {
const instance = {};
const injector = new Injector([{ provide: token, useValue: instance }]);
assert.strictEqual(injector.get(token), instance);
assert.strictEqual(injector.get(legacyName), instance);
assert.strictEqual(injector.get(`$${legacyName}`), instance);
});
}
it("finds registrations made under the legacy name only", () => {
const instance = {};
const injector = new Injector([
{ provide: "tempService", useValue: instance },
]);
assert.strictEqual(injector.get(TempService), instance);
});
describe("injection tokens", () => {
const tokens: [InjectionToken<any>, string][] = [
[XCODE, "xcode"],
[PBXPROJ_DOM_XCODE, "pbxprojDomXcode"],
];
for (const [token, legacyName] of tokens) {
it(`aliases '${legacyName}' by token, by name and by $-spelling`, () => {
assert.equal(token.description, legacyName);
// The registration these tokens alias is a module namespace object
// made by `injector.register(name, module)` under lib/node/.
const moduleValue = {};
const injector = new Injector([
{ provide: legacyName, useValue: moduleValue },
]);
assert.strictEqual(injector.get(token), moduleValue);
assert.strictEqual(injector.get(legacyName), moduleValue);
assert.strictEqual(injector.get(`$${legacyName}`), moduleValue);
});
}
});
describe("against a real Yok container", () => {
it("resolves the token and the legacy name to the same instance", () => {
const injector = new Yok();
injector.register("config", { DEBUG: false });
injector.register("logger", LoggerImpl);
injector.register("errors", ErrorsImpl);
injector.register("fs", FileSystemImpl);
assert.instanceOf(injector.get(Logger), LoggerImpl);
assert.strictEqual(injector.get(Logger), injector.resolve("logger"));
assert.strictEqual(injector.get(Errors), injector.resolve("errors"));
assert.strictEqual(injector.get(FileSystem), injector.resolve("fs"));
});
});
});