Skip to content

Commit f1ddfa2

Browse files
committed
fix(ng-add): Add hammerjs in test.ts #7515
1 parent 6621dd2 commit f1ddfa2

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

projects/igniteui-angular/schematics/ng-add/index.spec.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ describe('ng-add schematics', () => {
2525
polyfills: `${sourceRoot}/polyfills.ts`,
2626
scripts: []
2727
}
28-
}
28+
},
29+
test: {
30+
options: {
31+
main: `${sourceRoot}/test.ts`,
32+
polyfills: `${sourceRoot}/polyfills.ts`,
33+
scripts: []
34+
}
35+
},
2936
}
3037
}
3138
}
@@ -46,6 +53,7 @@ describe('ng-add schematics', () => {
4653
tree.create('/angular.json', JSON.stringify(ngJsonConfig));
4754
tree.create('/package.json', JSON.stringify(pkgJsonConfig));
4855
tree.create(`${sourceRoot}/main.ts`, '// test comment');
56+
tree.create(`${sourceRoot}/test.ts`, '// test comment');
4957
});
5058

5159
it('should create the needed files correctly', () => {
@@ -101,7 +109,7 @@ describe('ng-add schematics', () => {
101109
expect(mainTs).toContain('import \'hammerjs\';');
102110
});
103111

104-
it('should not add hammer.js if it exists in angular.json', () => {
112+
it('should not add hammer.js if it exists in angular.json build options', () => {
105113
const workspace = getWorkspace(tree) as any;
106114
const currentProjectName = workspace.defaultProject;
107115
workspace.projects[currentProjectName].architect.build.options.scripts.push('./node_modules/hammerjs/hammer.min.js');
@@ -112,6 +120,23 @@ describe('ng-add schematics', () => {
112120
expect(newContent.split('import \'hammerjs\';\n// test comment').length).toEqual(1);
113121
});
114122

123+
it('should add hammer.js to the test.ts file', () => {
124+
runner.runSchematic('ng-add', { normalizeCss: false }, tree);
125+
const testTs = tree.read(`${sourceRoot}/test.ts`).toString();
126+
expect(testTs).toContain('import \'hammerjs\';');
127+
});
128+
129+
it('should not add hammer.js if it exists in angular.json test options', () => {
130+
const workspace = getWorkspace(tree) as any;
131+
const currentProjectName = workspace.defaultProject;
132+
workspace.projects[currentProjectName].architect.test.options.scripts.push('./node_modules/hammerjs/hammer.min.js');
133+
tree.overwrite('angular.json', JSON.stringify(workspace));
134+
runner.runSchematic('ng-add', { normalizeCss: false }, tree);
135+
136+
const testTs = tree.read(`${sourceRoot}/test.ts`).toString();
137+
expect(testTs).toMatch('// test comment');
138+
});
139+
115140
it('should not add hammer.js if it exists in main.ts', () => {
116141
const mainTsPath = `${sourceRoot}/main.ts`;
117142
const content = tree.read(mainTsPath).toString();

projects/igniteui-angular/schematics/ng-add/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function propertyExistsInWorkspace(targetProp: string, workspace: WorkspaceSchem
2020
function enablePolyfills(tree: Tree, context: SchematicContext): string {
2121
const workspace = getWorkspace(tree);
2222
const project = workspace.projects[workspace.defaultProject];
23-
const targetFile = getConfigFile(project, 'polyfills');
23+
const targetFile = getConfigFile(project, 'polyfills', 'build');
2424
if (!tree.exists(targetFile)) {
2525
context.logger.warn(`${targetFile} not found. You may need to update polyfills.ts manually.`);
2626
return;
@@ -54,7 +54,7 @@ function readInput(options: Options): Rule {
5454
const workspace = getWorkspace(tree);
5555
const targetProperty = 'es5BrowserSupport';
5656
const project = workspace.projects[workspace.defaultProject];
57-
const polyfillsFile = getConfigFile(project, 'polyfills');
57+
const polyfillsFile = getConfigFile(project, 'polyfills', 'build');
5858
const propertyExists = propertyExistsInWorkspace(targetProperty, workspace);
5959
let polyfillsData = tree.read(polyfillsFile).toString();
6060
if (propertyExists) {

projects/igniteui-angular/schematics/utils/dependency-handler.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,28 @@ function getTargetedProjectOptions(project: WorkspaceProject<ProjectType>, targe
5959
throw new SchematicsException(`Cannot determine the project's configuration for: ${target}`);
6060
}
6161

62-
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string): string {
63-
const buildOptions = getTargetedProjectOptions(project, 'build');
64-
if (!buildOptions[option]) {
65-
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
66-
`workspace config (${project.sourceRoot})`);
67-
}
62+
export function getConfigFile(project: WorkspaceProject<ProjectType>, option: string, configSection: string): string {
63+
switch (configSection) {
64+
case 'build':
65+
const buildOptions = getTargetedProjectOptions(project, 'build');
66+
if (!buildOptions[option]) {
67+
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
68+
`workspace config ${project.sourceRoot}`);
69+
}
70+
return buildOptions[option];
6871

69-
return buildOptions[option];
72+
case 'test':
73+
const testOptions = getTargetedProjectOptions(project, 'test');
74+
if (!testOptions[option]) {
75+
throw new SchematicsException(`Could not find the project ${option} file inside of the ` +
76+
`workspace config ${project.sourceRoot}`);
77+
}
78+
return testOptions[option];
79+
default:
80+
throw new SchematicsException(`Could not find matching ${configSection} section` +
81+
`inside of the workspace config ${project.sourceRoot} `);
82+
}
7083
}
71-
7284
export function overwriteJsonFile(tree: Tree, targetFile: string, data: any) {
7385
tree.overwrite(targetFile, JSON.stringify(data, null, 2) + '\n');
7486
}
@@ -148,15 +160,25 @@ function includeDependencies(pkgJson: any, context: SchematicContext, tree: Tree
148160
const workspace = getWorkspace(tree);
149161
const project = workspace.projects[workspace.defaultProject];
150162
const projectOptions = getTargetedProjectOptions(project, 'build');
151-
const mainTsPath = getConfigFile(project, 'main');
163+
const projectTestOptions = getTargetedProjectOptions(project, 'test');
164+
const mainTsPath = getConfigFile(project, 'main', 'build');
165+
const testTsPath = getConfigFile(project, 'main', 'test');
152166
const hammerImport = 'import \'hammerjs\';\n';
153167
const mainTsContent = tree.read(mainTsPath).toString();
168+
const testTsContent = tree.read(testTsPath).toString();
154169
// if there are no elements in the architect.build.options.scripts array that contain hammerjs
155170
// and main.ts does not contain an import with hammerjs
156171
if (!projectOptions.scripts.some(el => el.includes('hammerjs')) && !mainTsContent.includes(hammerImport)) {
157172
// import hammerjs in the main.ts file
158-
const contents = hammerImport + mainTsContent;
159-
tree.overwrite(mainTsPath, contents);
173+
const mainContents = hammerImport + mainTsContent;
174+
tree.overwrite(mainTsPath, mainContents);
175+
}
176+
// make sure test.ts has hammerjs import even for projects already containing it in main.ts
177+
// if there are no elements in the architect.test.options.scripts array that contain hammerjs
178+
// and test.ts does not contain an import with hammerjs
179+
if (!projectTestOptions.scripts.some(el => el.includes('hammerjs')) && !testTsContent.includes(hammerImport)) {
180+
const testContents = hammerImport + testTsContent;
181+
tree.overwrite(testTsPath, testContents);
160182
}
161183
break;
162184
default:

0 commit comments

Comments
 (0)