Skip to content

Commit 96b8b7a

Browse files
Copilotfarfromrefug
andcommitted
Add tests for watch app service enhancements
Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>
1 parent b552d0e commit 96b8b7a

1 file changed

Lines changed: 211 additions & 0 deletions

File tree

test/ios-watch-app-service.ts

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import { assert } from "chai";
2+
import * as yok from "../lib/common/yok";
3+
import { IOSWatchAppService } from "../lib/services/ios-watch-app-service";
4+
import { ProjectDataStub } from "./stubs";
5+
import { IFileSystem } from "../lib/common/declarations";
6+
import { IProjectData } from "../lib/definitions/project";
7+
import { IInjector } from "../lib/common/definitions/yok";
8+
9+
describe("ios-watch-app-service", () => {
10+
let testInjector: IInjector;
11+
let service: IOSWatchAppService;
12+
let fs: IFileSystem;
13+
let projectData: IProjectData;
14+
15+
beforeEach(() => {
16+
testInjector = new yok.Yok();
17+
testInjector.register("fs", {
18+
exists: (p: string): boolean => false,
19+
readJson: (p: string): any => ({}),
20+
readDirectory: (p: string): string[] => [],
21+
getFsStats: (p: string): any => ({ isDirectory: () => false }),
22+
writeFile: (): void => {},
23+
});
24+
testInjector.register("pbxprojDomXcode", {});
25+
testInjector.register("xcode", {
26+
project: function () {
27+
return {
28+
parseSync: () => {},
29+
writeSync: () => "test",
30+
getFirstTarget: () => ({ uuid: "test-uuid" }),
31+
};
32+
},
33+
});
34+
testInjector.register("iOSNativeTargetService", {
35+
getTargetDirectories: () => ["test"],
36+
addTargetToProject: () => ({
37+
uuid: "target-uuid",
38+
pbxNativeTarget: { productName: "test" },
39+
}),
40+
setXcodeTargetBuildConfigurationProperties: () => {},
41+
setConfigurationsFromJsonFile: () => {},
42+
prepareSigning: () => {},
43+
});
44+
testInjector.register("logger", {
45+
trace: () => {},
46+
warn: () => {},
47+
info: () => {},
48+
});
49+
testInjector.register("projectConfigService", {
50+
getValue: (key: string, defaultValue: any) => defaultValue,
51+
});
52+
53+
fs = testInjector.resolve("fs");
54+
service = testInjector.resolve(IOSWatchAppService);
55+
projectData = new ProjectDataStub();
56+
});
57+
58+
describe("addWatchAppFromPath", () => {
59+
it("should return false when watch app folders don't exist", async () => {
60+
const result = await service.addWatchAppFromPath({
61+
watchAppFolderPath: "/test/path",
62+
projectData,
63+
platformData: {
64+
projectRoot: "/test",
65+
normalizedPlatformName: "iOS",
66+
platformNameLowerCase: "ios",
67+
} as any,
68+
pbxProjPath: "/test/project.pbxproj",
69+
});
70+
71+
assert.isFalse(result);
72+
});
73+
74+
it("should return true when watch app folders exist", async () => {
75+
fs.exists = (p: string) => true;
76+
fs.readDirectory = (p: string) => ["TestApp"];
77+
78+
const result = await service.addWatchAppFromPath({
79+
watchAppFolderPath: "/test/path",
80+
projectData,
81+
platformData: {
82+
projectRoot: "/test",
83+
normalizedPlatformName: "iOS",
84+
platformNameLowerCase: "ios",
85+
} as any,
86+
pbxProjPath: "/test/project.pbxproj",
87+
});
88+
89+
assert.isTrue(result);
90+
});
91+
});
92+
93+
describe("hasWatchApp", () => {
94+
it("should return false when watch app folder doesn't exist", () => {
95+
const result = service.hasWatchApp(
96+
{
97+
projectRoot: "/test",
98+
normalizedPlatformName: "iOS",
99+
} as any,
100+
projectData
101+
);
102+
103+
assert.isFalse(result);
104+
});
105+
106+
it("should return true when watch app folder exists", () => {
107+
fs.exists = (p: string) => true;
108+
109+
const result = service.hasWatchApp(
110+
{
111+
projectRoot: "/test",
112+
normalizedPlatformName: "iOS",
113+
} as any,
114+
projectData
115+
);
116+
117+
assert.isTrue(result);
118+
});
119+
});
120+
121+
describe("getWatchSPMPackages", () => {
122+
it("should return empty array when no packages configured", () => {
123+
const packages = (service as any).getWatchSPMPackages(projectData, {
124+
platformNameLowerCase: "ios",
125+
});
126+
127+
assert.isArray(packages);
128+
assert.lengthOf(packages, 0);
129+
});
130+
131+
it("should return configured watch SPM packages", () => {
132+
const mockPackages = [
133+
{
134+
name: "TestPackage",
135+
repositoryURL: "https://github.com/test/package",
136+
version: "1.0.0",
137+
libs: ["TestLib"],
138+
},
139+
];
140+
141+
testInjector.resolve("projectConfigService").getValue = (
142+
key: string,
143+
defaultValue: any
144+
) => {
145+
if (key === "ios.watchApp.SPMPackages") {
146+
return mockPackages;
147+
}
148+
return defaultValue;
149+
};
150+
151+
const packages = (service as any).getWatchSPMPackages(projectData, {
152+
platformNameLowerCase: "ios",
153+
});
154+
155+
assert.deepEqual(packages, mockPackages);
156+
});
157+
});
158+
159+
describe("processWatchAppConfiguration", () => {
160+
it("should handle missing config file gracefully", async () => {
161+
await (service as any).processWatchAppConfiguration(
162+
"/nonexistent/path",
163+
"TestTarget",
164+
{ uuid: "test-uuid", pbxNativeTarget: { productName: "test" } },
165+
{
166+
addFramework: () => {},
167+
addBuildProperty: () => {},
168+
addToHeaderSearchPaths: () => {},
169+
},
170+
projectData,
171+
{ projectRoot: "/test" }
172+
);
173+
174+
// Should not throw
175+
});
176+
177+
it("should process module configurations", async () => {
178+
const config = {
179+
modules: [
180+
{
181+
name: "Data",
182+
path: "Frameworks/Data.xcframework",
183+
},
184+
],
185+
};
186+
187+
fs.exists = (p: string) => true;
188+
fs.readJson = (p: string) => config;
189+
190+
let frameworkAdded = false;
191+
const mockProject = {
192+
addFramework: () => {
193+
frameworkAdded = true;
194+
},
195+
addBuildProperty: () => {},
196+
addToHeaderSearchPaths: () => {},
197+
};
198+
199+
await (service as any).processWatchAppConfiguration(
200+
"/test/config.json",
201+
"TestTarget",
202+
{ uuid: "test-uuid", pbxNativeTarget: { productName: "test" } },
203+
mockProject,
204+
projectData,
205+
{ projectRoot: "/test", platformNameLowerCase: "ios" }
206+
);
207+
208+
assert.isTrue(frameworkAdded);
209+
});
210+
});
211+
});

0 commit comments

Comments
 (0)