import { MergeStrategy, SchematicContext, Tree, VirtualTree } from '@angular-devkit/schematics'; import { isObservable, of } from 'rxjs'; import { tap } from 'rxjs/operators'; import { getFileContent } from '../test'; import { Schema } from './schema'; import { updateWchtoolsOptions } from './update.wchtoolsoptions'; describe('update.wchtoolsoptions', () => { const context: SchematicContext = ({ engine: null, debug: false, strategy: MergeStrategy.Default } as {}) as SchematicContext; it('create the missing file', () => { const pkg = {}; const options: Schema = { url: 'http:/example.org' }; const tree = new VirtualTree(); tree.create('package.json', JSON.stringify(pkg)); const res = updateWchtoolsOptions(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 const data = JSON.parse( getFileContent(tree, '/data/.wchtoolsoptions.json') ); expect(data['x-ibm-dx-tenant-base-url']).toEqual(options.url); } }) ) .toPromise(); }); it('should migrate an existing options file', () => { const pkg = { config: { data: './data-test' } }; const oldOps = { old: true, ['x-ibm-dx-tenant-base-url']: 'http://old.url' }; const options: Schema = { url: 'http:/example.org' }; const tree = new VirtualTree(); tree.create('package.json', JSON.stringify(pkg)); tree.create('/data-test/.wchtoolsoptions', JSON.stringify(oldOps)); const res = updateWchtoolsOptions(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 const data = JSON.parse( getFileContent(tree, '/data-test/.wchtoolsoptions.json') ); expect(data['x-ibm-dx-tenant-base-url']).toEqual(options.url); // validate that the old file does not exist expect(tree.exists('/data-test/.wchtoolsoptions')).toBeFalsy(); // check that the files has been migrated expect(data.old).toBeTruthy(); } }) ) .toPromise(); }); it('should create a new options file', () => { const pkg = { config: { data: './data-test' } }; const options: Schema = { url: 'http:/example.org' }; const tree = new VirtualTree(); tree.create('package.json', JSON.stringify(pkg)); const res = updateWchtoolsOptions(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) { const data = JSON.parse( getFileContent(tree, '/data-test/.wchtoolsoptions.json') ); expect(data['x-ibm-dx-tenant-base-url']).toEqual(options.url); } }) ) .toPromise(); }); });