refactor(elements): ng-add schematics (#33723)

This PR brings a couple of changes;
- Removes undeed dependencies in bazel targets such as `//packages/common` & `//packages/core`
- Removes RxJs usage
- Adds `document-register-element` to architect test targets
- Use @schematics/angular helpers
- Uses the standard `$source": "projectName"` to get the projectName, which is defined in the `schema.json`
- Use workspace writer to update the workspace config

PR Close #33723
This commit is contained in:
Alan Agius
2019-11-11 09:27:36 +01:00
committed by Matias Niemelä
parent 85a4a1ac60
commit 21ec7d92dd
5 changed files with 100 additions and 73 deletions

View File

@ -7,19 +7,14 @@
*/
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import {Observable} from 'rxjs';
import {concatMap} from 'rxjs/operators';
import {Schema as ElementsOptions} from './schema';
const polyfillPath = 'node_modules/document-register-element/build/document-register-element.js';
// tslint:disable:max-line-length
describe('Elements Schematics', () => {
const schematicRunner = new SchematicTestRunner(
'@angular/elements', path.join(__dirname, '../test-collection.json'), );
const defaultOptions: ElementsOptions = {project: 'bar', skipPackageJson: false};
const defaultOptions: ElementsOptions = {project: 'elements', skipPackageJson: false};
let appTree: UnitTestTree;
@ -40,20 +35,29 @@ describe('Elements Schematics', () => {
skipTests: false,
};
beforeEach((done) => {
schematicRunner.runExternalSchematicAsync('@schematics/angular', 'workspace', workspaceOptions)
.pipe(concatMap(
(tree) => schematicRunner.runExternalSchematicAsync(
'@schematics/angular', 'application', appOptions, tree)))
.subscribe((tree: UnitTestTree) => appTree = tree, done.fail, done);
beforeEach(async() => {
appTree = await schematicRunner
.runExternalSchematicAsync('@schematics/angular', 'workspace', workspaceOptions)
.toPromise();
appTree =
await schematicRunner
.runExternalSchematicAsync('@schematics/angular', 'application', appOptions, appTree)
.toPromise();
});
it('should run the ng-add schematic', async() => {
const tree =
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, appTree).toPromise();
const configText = tree.readContent('/angular.json');
const config = JSON.parse(configText);
const scripts = config.projects.elements.architect.build.options.scripts;
expect(scripts[0].input).toEqual(polyfillPath);
expect(tree.readContent('/projects/elements/src/polyfills.ts'))
.toContain(`import 'document-register-element';`);
});
it('should add polyfill as a dependency in package.json', async() => {
const tree =
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, appTree).toPromise();
const pkgJsonText = tree.readContent('/package.json');
const pkgJson = JSON.parse(pkgJsonText);
const {dependencies} = pkgJson;
expect(dependencies['document-register-element']).toBeDefined();
});
});