import { MergeStrategy, SchematicContext, Tree, VirtualTree } from '@angular-devkit/schematics'; import { isObservable, of } from 'rxjs'; import { tap } from 'rxjs/operators'; import { getFileContent } from '../test'; import { BUILD_COMMAND, CLI_IMPORT, SDK_IMPORT } from './constants'; import { Schema } from './schema'; import { BUILD_PRODUCTION, DEPLOY, DEPLOY_PUSH, PACKAGE_PATH, PACKAGE_PRODUCTION, updatePackage } from './update.package'; describe('update.package', () => { const context: SchematicContext = ({ engine: null, debug: false, strategy: MergeStrategy.Default } as {}) as SchematicContext; it('should update an existing package json', () => { const tree = new VirtualTree(); const options: Schema = {}; const pkg: any = { dependencies: { [SDK_IMPORT]: '^6.0.0' }, devDependencies: { [CLI_IMPORT]: '^6.0.0' }, scripts: { [BUILD_PRODUCTION]: BUILD_PRODUCTION, [PACKAGE_PRODUCTION]: PACKAGE_PRODUCTION, [DEPLOY_PUSH]: DEPLOY_PUSH, [DEPLOY]: DEPLOY } }; tree.create(PACKAGE_PATH, JSON.stringify(pkg)); const res = updatePackage(options)(tree, context); const rxRes = isObservable(res) ? res : of(res as Tree); return rxRes .pipe( tap(tree => { // verify the url expect(tree).toBeTruthy(); if (tree) { // check that we have the file expect(tree.exists(PACKAGE_PATH)).toBeTruthy(); const pkg = JSON.parse(getFileContent(tree, PACKAGE_PATH)); expect(pkg.dependencies[SDK_IMPORT]).toBeTruthy(); expect(pkg.devDependencies[CLI_IMPORT]).toBeTruthy(); expect(pkg.scripts[BUILD_COMMAND]).toBeTruthy(); } }) ) .toPromise(); }); it('should update the package json', () => { const tree = new VirtualTree(); const options: Schema = {}; const res = updatePackage(options)(tree, context); const rxRes = isObservable(res) ? res : of(res as Tree); return rxRes .pipe( tap(tree => { // verify the url expect(tree).toBeTruthy(); if (tree) { // check that we have the file expect(tree.exists(PACKAGE_PATH)).toBeTruthy(); const pkg = JSON.parse(getFileContent(tree, PACKAGE_PATH)); expect(pkg.dependencies[SDK_IMPORT]).toBeTruthy(); expect(pkg.devDependencies[CLI_IMPORT]).toBeTruthy(); expect(pkg.scripts[BUILD_COMMAND]).toBeTruthy(); } }) ) .toPromise(); }); });