@yolkai/nx-schematics
Version:
212 lines (201 loc) • 9.74 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const testing_1 = require("@angular-devkit/schematics/testing");
const path_1 = require("path");
const nx_workspace_1 = require("@yolkai/nx-workspace");
const nx_workspace_2 = require("@yolkai/nx-workspace");
const literals_1 = require("@angular-devkit/core/src/utils/literals");
const testing_2 = require("@yolkai/nx-workspace/testing");
const effectContents = `
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Effect, Actions } from '@ngrx/effects';
import { DataPersistence } from '@yolkai/nx';
import { UserPartialState } from './user.reducer';
import {
LoadUser,
UserLoaded,
UserLoadError,
UserActionTypes
} from './user.actions';
export class UserEffects {
effect$ = this.actions$.ofType(LoadUser).pipe(mapTo(UserLoaded));
effect2$ = this.actions$.ofType<UserLoaded>(LoadUser).pipe(mapTo(UserLoaded));
effect3$ = this.actions$.ofType<UserLoaded>(LoadUser).pipe(withLatestFrom(this.store.select(selector)), mapTo(UserLoaded));
constructor(
private actions$: Actions,
private dataPersistence: DataPersistence<UserPartialState>,
private store: Store<AppState>
) {}
}
`;
const selectorContents = `
import { Store } from '@ngrx/store';
import { Component } from '@angular/core';
import { AppState, selector } from '../+state';
export class AppComponent {
slice$ = this.store.select(selector).pipe(
map(a => a)
);
slice2$: Observable<string>;
slice3$ = Observable.from([]).pipe(
withLatestFrom(this.store.select(selector5))
);
constructor(
private store: Store<AppState>
) {}
ngOnInit() {
this.slice2$ = this.store.select(selector2);
this.store.select(selector3).subscribe(console.log);
}
}
`;
describe('Update 7.6.0', () => {
let initialTree;
let schematicRunner;
beforeEach(() => {
initialTree = testing_2.createEmptyWorkspace(schematics_1.Tree.empty());
initialTree.overwrite('package.json', nx_workspace_1.serializeJson({
dependencies: {
'@ngrx/effects': '6.1.2',
'@ngrx/router-store': '6.1.2',
'@ngrx/store': '6.1.2'
},
devDependencies: {
'@ngrx/schematics': '6.1.2',
'@ngrx/store-devtools': '6.1.2'
}
}));
schematicRunner = new testing_1.SchematicTestRunner('@yolkai/nx-schematics', path_1.join(__dirname, '../migrations.json'));
});
describe('VSCode Extension Recommendations', () => {
it('should be added', () => __awaiter(this, void 0, void 0, function* () {
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
expect(nx_workspace_2.readJsonInTree(result, '.vscode/extensions.json')).toEqual({
recommendations: [
'nrwl.angular-console',
'angular.ng-template',
'esbenp.prettier-vscode'
]
});
}));
it('should be added to existing recommendations', () => __awaiter(this, void 0, void 0, function* () {
initialTree = yield schematicRunner
.callRule(nx_workspace_2.updateJsonInTree('.vscode/extensions.json', () => ({
recommendations: ['eamodio.gitlens', 'angular.ng-template']
})), initialTree)
.toPromise();
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
expect(nx_workspace_2.readJsonInTree(result, '.vscode/extensions.json')).toEqual({
recommendations: [
'eamodio.gitlens',
'angular.ng-template',
'nrwl.angular-console',
'esbenp.prettier-vscode'
]
});
}));
});
describe('adding dotenv', () => {
it('should add dotenv as a dev dependency', () => __awaiter(this, void 0, void 0, function* () {
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
expect(nx_workspace_2.readJsonInTree(result, 'package.json').devDependencies['dotenv']).toEqual('6.2.0');
}));
});
describe('setting defaults to karma, protractor, express', () => {
it('should default to karma, protractor and express', () => __awaiter(this, void 0, void 0, function* () {
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
expect(nx_workspace_2.readJsonInTree(result, 'workspace.json').schematics['@yolkai/nx-schematics:library'].unitTestRunner).toEqual('karma');
expect(nx_workspace_2.readJsonInTree(result, 'workspace.json').schematics['@yolkai/nx-schematics:application'].unitTestRunner).toEqual('karma');
expect(nx_workspace_2.readJsonInTree(result, 'workspace.json').schematics['@yolkai/nx-schematics:application'].e2eTestRunner).toEqual('protractor');
expect(nx_workspace_2.readJsonInTree(result, 'workspace.json').schematics['@yolkai/nx-schematics:node-application'].framework).toEqual('express');
}));
});
describe('NgRx Migration', () => {
it('should update ngrx to 7.1.0', () => __awaiter(this, void 0, void 0, function* () {
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
const json = nx_workspace_2.readJsonInTree(result, 'package.json');
expect(json.dependencies['@ngrx/effects']).toEqual('7.2.0');
expect(json.dependencies['@ngrx/router-store']).toEqual('7.2.0');
expect(json.dependencies['@ngrx/store']).toEqual('7.2.0');
expect(json.devDependencies['@ngrx/schematics']).toEqual('7.2.0');
expect(json.devDependencies['@ngrx/store-devtools']).toEqual('7.2.0');
}));
it('should convert ofType code', () => __awaiter(this, void 0, void 0, function* () {
initialTree.create('user.effects.ts', effectContents);
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
const contents = result.readContent('user.effects.ts');
expect(contents).toContain("import { Effect, Actions, ofType } from '@ngrx/effects';");
expect(literals_1.stripIndents `${contents}`).toContain(literals_1.stripIndents `
effect$ = this.actions$.pipe(
ofType(LoadUser),
mapTo(UserLoaded)
);`);
expect(literals_1.stripIndents `${contents}`).toContain(literals_1.stripIndents `
effect2$ = this.actions$.pipe(
ofType<UserLoaded>(LoadUser),
mapTo(UserLoaded)
);`);
expect(literals_1.stripIndents `${contents}`).toContain(literals_1.stripIndents `
effect3$ = this.actions$.pipe(
ofType<UserLoaded>(LoadUser),
withLatestFrom(this.store.pipe(select(selector))),
mapTo(UserLoaded)
);`);
}));
it('should convert select code', () => __awaiter(this, void 0, void 0, function* () {
initialTree.create('app.component.ts', selectorContents);
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
const contents = result.readContent('app.component.ts');
expect(contents).toContain("import { Store, select } from '@ngrx/store';");
expect(literals_1.stripIndents `${contents}`).toContain(literals_1.stripIndents `
slice$ = this.store.pipe(
select(selector),
map(a => a)
);`);
expect(contents).toContain('this.slice2$ = this.store.pipe(select(selector2))');
expect(contents).toContain('this.store.pipe(select(selector3)).subscribe(console.log);');
expect(literals_1.stripIndents `${contents}`).toContain(literals_1.stripIndents `
slice3$ = Observable.from([]).pipe(
withLatestFrom(this.store.pipe(select(selector5)))
);`);
}));
});
describe('Update Angular CLI', () => {
it('should update @angular-devkit/build-angular', () => __awaiter(this, void 0, void 0, function* () {
const result = yield schematicRunner
.runSchematicAsync('update-7.6.0', {}, initialTree)
.toPromise();
expect(nx_workspace_2.readJsonInTree(result, 'package.json').devDependencies['@angular-devkit/build-angular']).toEqual('~0.13.1');
}));
});
});