Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding installJestDom and installUserEvent as option to the ng-add schematic #478

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions projects/testing-library/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import {
addPackageJsonDependency,
getPackageJsonDependency,
NodeDependencyType,
} from '@schematics/angular/utility/dependencies';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { Schema } from './schema';

const dtl = '@testing-library/dom';
export default function ({ installJestDom, installUserEvent }: Schema): Rule {
return () => {
return chain([
addDependency('@testing-library/dom', '^10.0.0', NodeDependencyType.Dev),
installJestDom ? addDependency('@testing-library/jest-dom', '^6.4.8', NodeDependencyType.Dev) : noop(),
installUserEvent ? addDependency('@testing-library/user-event', '^14.5.2', NodeDependencyType.Dev) : noop(),
installDependencies(),
]);
};
}

export default function (): Rule {
function addDependency(packageName: string, version: string, dependencyType: NodeDependencyType) {
return (tree: Tree, context: SchematicContext) => {
const dtlDep = getPackageJsonDependency(tree, dtl);
const dtlDep = getPackageJsonDependency(tree, packageName);
if (dtlDep) {
context.logger.info(`Skipping installation of '@testing-library/dom' because it's already installed.`);
context.logger.info(`Skipping installation of '${packageName}' because it's already installed.`);
} else {
context.logger.info(`Adding '@testing-library/dom' as a dev dependency.`);
addPackageJsonDependency(tree, { name: dtl, type: NodeDependencyType.Dev, overwrite: false, version: '^10.0.0' });
context.logger.info(`Adding '${packageName}' as a dev dependency.`);
addPackageJsonDependency(tree, { name: packageName, type: dependencyType, overwrite: false, version });
}

return tree;
};
}

export function installDependencies(packageManager = 'npm') {
return (_tree: Tree, context: SchematicContext) => {
context.addTask(new NodePackageInstallTask({ packageManager }));

context.logger.info(
`Correctly installed @testing-library/angular.
See our docs at https://testing-library.com/docs/angular-testing-library/intro/ to get started.`,
);

return tree;
};
}
24 changes: 23 additions & 1 deletion projects/testing-library/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
"$id": "SchematicsTestingLibraryAngular",
"title": "testing-library-angular",
"type": "object",
"properties": {},
"properties": {
"installJestDom": {
"type": "boolean",
"description": "Install jest-dom as a dependency.",
"$default": {
"$source": "argv",
"index": 0
},
"default": false,
"x-prompt": "Would you like to install jest-dom?"
},
"installUserEvent": {
Hyperxq marked this conversation as resolved.
Show resolved Hide resolved
"type": "boolean",
"description": "Install user-event as a dependency.",
"$default": {
"$source": "argv",
"index": 1
},
"default": false,
"x-prompt": "Would you like to install user-event?"
}
},
"additionalProperties": false,
"required": []
}
5 changes: 4 additions & 1 deletion projects/testing-library/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Schema {}
export interface Schema {
installJestDom: boolean;
installUserEvent: boolean;
}