-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex_spec.ts
More file actions
489 lines (417 loc) · 16 KB
/
index_spec.ts
File metadata and controls
489 lines (417 loc) · 16 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import * as path from "path";
import { EmptyTree } from "@angular-devkit/schematics";
import { SchematicTestRunner, UnitTestTree } from "@angular-devkit/schematics/testing";
import { FEED_ANGULAR, NPM_ANGULAR } from "@igniteui/cli-core";
describe("cli-config schematic", () => {
const collectionPath = path.join(__dirname, "../collection.json");
const runner: SchematicTestRunner = new SchematicTestRunner("cli-schematics", collectionPath);
let tree: UnitTestTree;
const sourceRoot = "src";
// tslint:disable: object-literal-sort-keys
const ngJsonConfig = {
projects: {
testProj: {
root: "",
sourceRoot,
architect: {
build: {
options: {
main: `${sourceRoot}/main.ts`,
polyfills: `${sourceRoot}/polyfills.ts`,
scripts: [],
index: `${sourceRoot}/index.html`
}
},
serve: {},
test: {}
}
}
},
version: 1
};
const pkgJsonConfig = {
dependencies: {},
devDependencies: {},
peerDependencies: {}
};
function createIgPkgJson(igxPkg = NPM_ANGULAR) {
const filePath = `node_modules/${igxPkg}/package.json`;
tree.create(filePath, JSON.stringify(pkgJsonConfig));
const pkgJson = JSON.parse(tree.readContent(filePath));
const angularCommon = "@angular/common";
const angularCore = "@angular/core";
const targetVersion = "^7.0.3";
pkgJson.peerDependencies[angularCommon] = targetVersion;
pkgJson.peerDependencies[angularCore] = targetVersion;
tree.overwrite(filePath, JSON.stringify(pkgJson));
}
function populatePkgJson(igxPkg = NPM_ANGULAR) {
const targetFile = "/package.json";
const angularCore = "@angular/core";
const angularCommon = "@angular/common";
const version = "^6.1.0";
const pkgJson = JSON.parse(tree.readContent(targetFile));
pkgJson.dependencies[angularCore] = version;
pkgJson.dependencies[angularCommon] = version;
pkgJson.dependencies[igxPkg] = "~7.0.0";
tree.overwrite(targetFile, JSON.stringify(pkgJson));
}
function resetTree() {
tree.overwrite("/angular.json", JSON.stringify(ngJsonConfig));
tree.overwrite("/package.json", JSON.stringify(pkgJsonConfig));
tree.overwrite("/src/index.html",
`<head>
</head>
<body>
</body>`);
tree.delete("/ignite-ui-cli.json");
}
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
tree.create("/angular.json", JSON.stringify(ngJsonConfig));
tree.create("/package.json", JSON.stringify(pkgJsonConfig));
tree.create("/src/index.html",
`<head>
</head>
<body>
</body>`);
createIgPkgJson();
populatePkgJson();
});
it("should create the needed files correctly", () => {
expect(tree).toBeTruthy();
expect(tree.exists("/angular.json")).toBeTruthy();
expect(tree.exists("/package.json")).toBeTruthy();
expect(tree.exists("/src/index.html"));
});
it("should create an ignite-ui-cli.json file correctly", async () => {
await runner.runSchematic("cli-config", {}, tree);
expect(tree.exists("ignite-ui-cli.json")).toBeTruthy();
const cliJsonData = JSON.parse(tree.readContent("/ignite-ui-cli.json"));
expect(cliJsonData.project.projectTemplate).toEqual("ng-cli");
});
it("should add typography correctly", async () => {
const targetFile = "/src/index.html";
await runner.runSchematic("cli-config", {}, tree);
const content = tree.readContent(targetFile);
expect(content.includes("<body class=\"ig-typography ig-scrollbar\">")).toBeTruthy();
});
it("should add Titillium and Material Icons stylesheets correctly", async () => {
const targetFile = "/src/index.html";
await runner.runSchematic("cli-config", {}, tree);
const content = tree.readContent(targetFile);
const headContentsRegex = /(?:<head>)([\s\S]*)(?:<\/head>)/;
expect(headContentsRegex.test(content)).toBeTruthy();
expect(headContentsRegex.exec(content)!.pop()).toContain("family=Titillium+Web");
expect(headContentsRegex.exec(content)!.pop()).toContain("family=Material+Icons");
});
it("should add the default scss theme correctly", async () => {
const targetFile = "/src/styles.scss";
tree.create(targetFile, "");
await runner.runSchematic("cli-config", {}, tree);
let content = tree.readContent(targetFile);
expect(content.includes(`@use "${NPM_ANGULAR}`)).toBeTruthy();
tree.overwrite(targetFile, "");
resetTree();
createIgPkgJson(FEED_ANGULAR);
populatePkgJson(FEED_ANGULAR);
await runner.runSchematic("cli-config", {}, tree);
content = tree.readContent(targetFile);
expect(content.includes(`@use "${FEED_ANGULAR}`)).toBeTruthy();
});
it("should add the default css theme to the workspace", async () => {
const targetFile = "/angular.json";
expect(tree.exists(targetFile)).toBeTruthy();
let targetImport = `node_modules/${NPM_ANGULAR}/styles/igniteui-angular.css`;
await runner.runSchematic("cli-config", {}, tree);
let workspace = JSON.parse(tree.read("/angular.json")!.toString());
expect(
workspace.projects.testProj.architect.build.options.styles.filter(
(s: string) => s.includes(targetImport)).length
)
.toBeGreaterThan(0);
expect(
workspace.projects.testProj.architect.test.options.styles.filter(
(s: string) => s.includes(targetImport)).length
)
.toBeGreaterThan(0);
resetTree();
createIgPkgJson(FEED_ANGULAR);
populatePkgJson(FEED_ANGULAR);
targetImport = `node_modules/${FEED_ANGULAR}/styles/igniteui-angular.css`;
await runner.runSchematic("cli-config", {}, tree);
workspace = JSON.parse(tree.read("/angular.json")!.toString());
expect(
workspace.projects.testProj.architect.build.options.styles.filter(
(s: string) => s.includes(targetImport)).length
)
.toBeGreaterThan(0);
expect(
workspace.projects.testProj.architect.test.options.styles.filter(
(s: string) => s.includes(targetImport)).length
)
.toBeGreaterThan(0);
});
it("should not add the default css theme to the workspace if the global styles file is scss", async () => {
// if the global styles file is scss or sass - the default theme is imported there
const stylesheet = "/src/styles.scss";
tree.create(stylesheet, "");
const targetFile = "/angular.json";
expect(tree.exists(targetFile)).toBeTruthy();
await runner.runSchematic("cli-config", {}, tree);
const workspace = JSON.parse(tree.read("/angular.json")!.toString());
// the schematic creates the hierarchy that leads to the styles object within the workspace,
// providing that it is not already present
expect(workspace.projects.testProj.architect.build.styles).toBeUndefined();
expect(workspace.projects.testProj.architect.test.styles).toBeUndefined();
});
it("should add the default sass theme correctly", async () => {
const targetFile = "/src/styles.sass";
tree.create(targetFile, "");
await runner.runSchematic("cli-config", {}, tree);
let content = tree.readContent(targetFile);
expect(content.includes(`@use "${NPM_ANGULAR}`)).toBeTruthy();
tree.overwrite(targetFile, "");
resetTree();
createIgPkgJson(FEED_ANGULAR);
populatePkgJson(FEED_ANGULAR);
await runner.runSchematic("cli-config", {}, tree);
content = tree.readContent(targetFile);
expect(content.includes(`@use "${FEED_ANGULAR}`)).toBeTruthy();
});
it("should add BrowserAnimationsModule to app.module.ts", async () => {
const moduleContent =
`import { NgModule } from '@angular/core';
@NgModule({
imports: []
})
export class AppModule {
}
`;
const moduleContentAfterSchematic =
`import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
@NgModule({
imports: [BrowserAnimationsModule]
})
export class AppModule {
}
`;
const targetFile = "./src/app/app.module.ts";
tree.create(targetFile, moduleContent);
await runner.runSchematic("cli-config", {}, tree);
const content = tree.readContent(targetFile);
expect(content.replace(/\r\n/g, "\n")).toEqual(moduleContentAfterSchematic.replace(/\r\n/g, "\n"));
});
it("should add provideAnimations to app.config.ts", async () => {
const moduleContent =
`import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)]
};
`;
const moduleContentAfterSchematic =
`import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import { provideAnimations } from "@angular/platform-browser/animations";
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes), provideAnimations()]
};
`;
const targetFile = "./src/app/app.config.ts";
tree.create(targetFile, moduleContent);
await runner.runSchematic("cli-config", {}, tree);
const content = tree.readContent(targetFile);
expect(content.replace(/\r\n/g, "\n")).toEqual(moduleContentAfterSchematic.replace(/\r\n/g, "\n"));
});
it("should NOT add provideAnimations to app.config.ts if it already exists", async () => {
const moduleContent =
`import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import { provideAnimations } from "@angular/platform-browser/animations";
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes), provideAnimations()]
};
`;
const targetFile = "./src/app/app.config.ts";
tree.create(targetFile, moduleContent);
await runner.runSchematic("cli-config", {}, tree);
const content = tree.readContent(targetFile);
expect(content.replace(/\r\n/g, "\n")).toEqual(moduleContent.replace(/\r\n/g, "\n"));
});
it("should properly display the dependency mismatch warning", async () => {
const warns: string[] = [];
runner.logger.subscribe(entry => {
if (entry.level === "warn") {
warns.push(entry.message);
}
});
await runner.runSchematic("cli-config", {}, tree);
let pattern = new RegExp(`WARNING Version mismatch detected - ${NPM_ANGULAR}`);
expect(warns).toContain(jasmine.stringMatching(pattern));
resetTree();
createIgPkgJson(FEED_ANGULAR);
populatePkgJson(FEED_ANGULAR);
pattern = new RegExp(`WARNING Version mismatch detected - ${FEED_ANGULAR}`);
await runner.runSchematic("cli-config", {}, tree);
expect(warns).toContain(jasmine.stringMatching(pattern));
});
describe("addAISkills", () => {
const mockSkillContent = "# Ignite UI for Angular - AI Skills\nBest practices...";
const copilotDest = ".github/copilot-instructions.md";
const claudeDest = "CLAUDE.md";
const cursorDest = ".cursor/skills";
const agentsDest = ".agents/skills";
function createSkillFiles(igxPkg = NPM_ANGULAR) {
const dir = `node_modules/${igxPkg}/skills`;
tree.create(`${dir}/igniteui-angular.md`, mockSkillContent);
}
it("should copy skill file to .github/copilot-instructions.md for copilot target", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["copilot"]
}, tree);
expect(tree.exists(copilotDest)).toBeTruthy();
expect(tree.readContent(copilotDest)).toEqual(mockSkillContent);
});
it("should copy skill file to CLAUDE.md for claude target", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["claude"]
}, tree);
expect(tree.exists(claudeDest)).toBeTruthy();
expect(tree.readContent(claudeDest)).toEqual(mockSkillContent);
});
it("should copy skill files to .cursor/skills/ for cursor target", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["cursor"]
}, tree);
expect(tree.exists(`${cursorDest}/igniteui-angular.md`)).toBeTruthy();
expect(tree.readContent(`${cursorDest}/igniteui-angular.md`)).toEqual(mockSkillContent);
});
it("should copy skill files to .agents/skills/ for agents target", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["agents"]
}, tree);
expect(tree.exists(`${agentsDest}/igniteui-angular.md`)).toBeTruthy();
expect(tree.readContent(`${agentsDest}/igniteui-angular.md`)).toEqual(mockSkillContent);
});
it("should copy skill files to custom path", async () => {
createSkillFiles();
const customPath = "my-custom/ai-skills";
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["custom"],
aiSkillsCustomPath: customPath
}, tree);
expect(tree.exists(`${customPath}/igniteui-angular.md`)).toBeTruthy();
expect(tree.readContent(`${customPath}/igniteui-angular.md`)).toEqual(mockSkillContent);
});
it("should handle multiple targets at once", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["copilot", "claude", "cursor", "agents"]
}, tree);
expect(tree.exists(copilotDest)).toBeTruthy();
expect(tree.exists(claudeDest)).toBeTruthy();
expect(tree.exists(`${cursorDest}/igniteui-angular.md`)).toBeTruthy();
expect(tree.exists(`${agentsDest}/igniteui-angular.md`)).toBeTruthy();
});
it("should NOT create skill files when addAISkills is false", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: false,
aiSkillsTargets: ["copilot", "claude"]
}, tree);
expect(tree.exists(copilotDest)).toBeFalsy();
expect(tree.exists(claudeDest)).toBeFalsy();
});
it("should NOT create skill files when aiSkillsTargets is empty", async () => {
createSkillFiles();
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: []
}, tree);
expect(tree.exists(copilotDest)).toBeFalsy();
expect(tree.exists(claudeDest)).toBeFalsy();
});
it("should not overwrite existing copilot-instructions.md", async () => {
createSkillFiles();
const existingContent = "# Existing instructions";
tree.create(copilotDest, existingContent);
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["copilot"]
}, tree);
expect(tree.readContent(copilotDest)).toEqual(existingContent);
});
it("should not overwrite existing CLAUDE.md", async () => {
createSkillFiles();
const existingContent = "# Existing CLAUDE.md";
tree.create(claudeDest, existingContent);
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["claude"]
}, tree);
expect(tree.readContent(claudeDest)).toEqual(existingContent);
});
it("should not overwrite existing cursor skill files", async () => {
createSkillFiles();
const existingContent = "# Existing cursor skill";
tree.create(`${cursorDest}/igniteui-angular.md`, existingContent);
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["cursor"]
}, tree);
expect(tree.readContent(`${cursorDest}/igniteui-angular.md`)).toEqual(existingContent);
});
it("should not overwrite existing agents skill files", async () => {
createSkillFiles();
const existingContent = "# Existing agents skill";
tree.create(`${agentsDest}/igniteui-angular.md`, existingContent);
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["agents"]
}, tree);
expect(tree.readContent(`${agentsDest}/igniteui-angular.md`)).toEqual(existingContent);
});
it("should warn when custom target has no path", async () => {
createSkillFiles();
const warns: string[] = [];
runner.logger.subscribe(entry => {
if (entry.level === "warn") {
warns.push(entry.message);
}
});
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["custom"]
}, tree);
expect(warns).toContain(jasmine.stringMatching(/Custom AI skills path was selected but no path was provided/));
});
it("should work with FEED_ANGULAR package", async () => {
// Run schematic first to create ignite-ui-cli.json (required by resetTree)
await runner.runSchematic("cli-config", {}, tree);
resetTree();
createIgPkgJson(FEED_ANGULAR);
populatePkgJson(FEED_ANGULAR);
createSkillFiles(FEED_ANGULAR);
await runner.runSchematic("cli-config", {
addAISkills: true,
aiSkillsTargets: ["copilot"]
}, tree);
expect(tree.exists(copilotDest)).toBeTruthy();
});
});
});