Skip to content

Commit a87b885

Browse files
committed
test(ios): register the USB tunnel service and its stubs by contract token
The tunnel spec provides the service and the contracted stubs it injects (ChildProcess, FileSystem, HostInfo, Logger, Errors, TempService) by token; the RunController and BundlerCompilerService specs provide and look up the tunnel stub through the IOSDeviceTunnelService contract.
1 parent 5256370 commit a87b885

3 files changed

Lines changed: 57 additions & 32 deletions

File tree

‎test/controllers/run-controller.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { PrepareController } from "../../lib/controllers/prepare-controller";
1616
import { LiveSyncProcessDataService } from "../../lib/services/livesync-process-data-service";
1717
import { IDictionary } from "../../lib/common/declarations";
1818
import { IInjector } from "../../lib/common/definitions/yok";
19+
import { IOSDeviceTunnelService } from "../../lib/contracts/ios-device-tunnel-service";
1920

2021
let isAttachToHmrStatusCalled = false;
2122
let prepareData: IPrepareData = null;
@@ -142,10 +143,13 @@ function createTestInjector() {
142143
injector.register("viteHmrPortService", {
143144
getPort: async () => 5173,
144145
});
145-
injector.register("iOSDeviceTunnelService", {
146-
open: async (): Promise<string> => null,
147-
getHostAddress: (): string => undefined,
148-
close: async () => ({}),
146+
injector.register({
147+
provide: IOSDeviceTunnelService,
148+
useValue: {
149+
open: async (): Promise<string> => null,
150+
getHostAddress: (): string => undefined,
151+
close: async () => ({}),
152+
},
149153
});
150154

151155
const devicesService = injector.resolve("devicesService");
@@ -312,7 +316,7 @@ describe("RunController", () => {
312316
injector.resolve("devicesService").getDeviceByIdentifier = (
313317
identifier: string,
314318
) => map[identifier]?.device;
315-
const tunnelService = injector.resolve("iOSDeviceTunnelService");
319+
const tunnelService = injector.get(IOSDeviceTunnelService);
316320
tunnelService.open = async (device: Mobile.IDevice) => {
317321
openedFor.push(device.deviceInfo.identifier);
318322
events.push("tunnel");

‎test/services/bundler/bundler-compiler-service.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as os from "os";
77
import * as path from "path";
88
import { ErrorsStub } from "../../stubs";
99
import { IInjector } from "../../../lib/common/definitions/yok";
10+
import { IOSDeviceTunnelService } from "../../../lib/contracts/ios-device-tunnel-service";
1011
import {
1112
BUNDLER_APP_RESTART_REQUESTED,
1213
BUNDLER_COMPILATION_COMPLETE,
@@ -92,8 +93,9 @@ function createTestInjector(
9293
testInjector.register("viteHmrPortService", {
9394
getPort: async () => 5173,
9495
});
95-
testInjector.register("iOSDeviceTunnelService", {
96-
getHostAddress: (): string => undefined,
96+
testInjector.register({
97+
provide: IOSDeviceTunnelService,
98+
useValue: { getHostAddress: (): string => undefined },
9799
});
98100

99101
return testInjector;
@@ -383,7 +385,7 @@ describe("BundlerCompilerService", () => {
383385
beforeEach(() => {
384386
previousHost = process.env.NS_HMR_HOST;
385387
delete process.env.NS_HMR_HOST;
386-
testInjector.resolve("iOSDeviceTunnelService").getHostAddress = (
388+
testInjector.get(IOSDeviceTunnelService).getHostAddress = (
387389
platform: string,
388390
) => (platform === "ios" ? "fd6f:72e:8d34::2" : undefined);
389391
});

‎test/services/ios/ios-device-tunnel-service.ts‎

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ import { provide } from "../../../lib/common/di";
66
import { MobileHelper } from "../../../lib/common/mobile/mobile-helper";
77
import { DevicePlatformsConstants } from "../../../lib/common/mobile/device-platforms-constants";
88
import { DeviceConnectionType } from "../../../lib/constants";
9-
import { IOSDeviceTunnelService } from "../../../lib/contracts/ios-device-tunnel-service";
9+
import {
10+
ChildProcess,
11+
Errors,
12+
FileSystem,
13+
HostInfo,
14+
IOSDeviceTunnelService,
15+
Logger,
16+
TempService,
17+
} from "../../../lib/contracts";
1018
import { IOSDeviceTunnelServiceImpl } from "../../../lib/services/ios/ios-device-tunnel-service";
1119
import { ErrorsStub, LoggerStub, TempServiceStub } from "../../stubs";
1220

@@ -115,45 +123,56 @@ describe("IOSDeviceTunnelService", () => {
115123
let lastListing: object[] = [];
116124

117125
const injector: IInjector = new Yok();
118-
injector.register("childProcess", {
119-
trySpawnFromCloseEvent: async () => {
120-
listCalls++;
121-
if (devicectlListings.length) {
122-
lastListing = devicectlListings.shift();
123-
}
124-
return { exitCode: lastListing ? 0 : 1 };
125-
},
126-
spawn: (command: string, args: string[]) => {
127-
const holder = new FakeHolder();
128-
spawned.push({ args: [command, ...args], holder });
129-
return holder;
126+
injector.register({
127+
provide: ChildProcess,
128+
useValue: {
129+
trySpawnFromCloseEvent: async () => {
130+
listCalls++;
131+
if (devicectlListings.length) {
132+
lastListing = devicectlListings.shift();
133+
}
134+
return { exitCode: lastListing ? 0 : 1 };
135+
},
136+
spawn: (command: string, args: string[]) => {
137+
const holder = new FakeHolder();
138+
spawned.push({ args: [command, ...args], holder });
139+
return holder;
140+
},
130141
},
131142
});
132143
injector.register("cleanupService", {
133144
addKillProcess: async () => ({}),
134145
removeKillProcess: async () => ({}),
135146
});
136-
injector.register("fs", {
137-
exists: () => true,
138-
readJson: () => ({ result: { devices: lastListing } }),
147+
injector.register({
148+
provide: FileSystem,
149+
useValue: {
150+
exists: () => true,
151+
readJson: () => ({ result: { devices: lastListing } }),
152+
},
139153
});
140-
injector.register("hostInfo", {
141-
get isDarwin() {
142-
return isDarwin;
154+
injector.register({
155+
provide: HostInfo,
156+
useValue: {
157+
get isDarwin() {
158+
return isDarwin;
159+
},
143160
},
144161
});
145162
const logger = new LoggerStub();
146163
logger.warn = (message: string) => {
147164
warnings.push(message);
148165
};
149-
injector.register("logger", logger);
150-
injector.register("errors", ErrorsStub);
166+
injector.register([
167+
{ provide: Logger, useValue: logger },
168+
{ provide: Errors, useClass: ErrorsStub },
169+
]);
151170
injector.register("devicePlatformsConstants", DevicePlatformsConstants);
152171
injector.register("mobileHelper", MobileHelper);
153-
injector.register("tempService", TempServiceStub);
154-
injector.register(
172+
injector.register([
173+
{ provide: TempService, useClass: TempServiceStub },
155174
provide(IOSDeviceTunnelService, IOSDeviceTunnelServiceImpl),
156-
);
175+
]);
157176

158177
service = <IOSDeviceTunnelServiceImpl>injector.get(IOSDeviceTunnelService);
159178
(<any>service).tunnelReadyTimeoutMs = 200;

0 commit comments

Comments
 (0)