From 45a2a41fd719213775d4eb4da72b521029ddb61a Mon Sep 17 00:00:00 2001 From: nklincoln Date: Wed, 7 Jun 2017 14:34:00 +0100 Subject: [PATCH 1/2] enable addition of README file via AddFile --- .../src/app/add-file/add-file.component.html | 1 + .../app/add-file/add-file.component.spec.ts | 32 +++- .../src/app/add-file/add-file.component.ts | 14 +- .../replace-confirm.component.html | 2 +- .../replace-confirm.component.spec.ts | 14 +- .../replace-confirm.component.ts | 16 +- .../src/app/editor/editor.component.spec.ts | 141 +++++++++++++++++- .../src/app/editor/editor.component.ts | 31 +++- .../src/app/import/import.component.spec.ts | 2 + .../src/app/import/import.component.ts | 5 +- .../src/app/services/client.service.spec.ts | 23 +++ .../src/app/services/client.service.ts | 16 +- .../src/assets/svg/other/MD_File.svg | 1 + 13 files changed, 264 insertions(+), 34 deletions(-) create mode 100644 packages/composer-playground/src/assets/svg/other/MD_File.svg diff --git a/packages/composer-playground/src/app/add-file/add-file.component.html b/packages/composer-playground/src/app/add-file/add-file.component.html index b6c52c6d45..aec9039ef1 100644 --- a/packages/composer-playground/src/app/add-file/add-file.component.html +++ b/packages/composer-playground/src/app/add-file/add-file.component.html @@ -20,6 +20,7 @@

Add a file

{{currentFileName}}
diff --git a/packages/composer-playground/src/app/add-file/add-file.component.spec.ts b/packages/composer-playground/src/app/add-file/add-file.component.spec.ts index 9a47107f95..75297acfc0 100644 --- a/packages/composer-playground/src/app/add-file/add-file.component.spec.ts +++ b/packages/composer-playground/src/app/add-file/add-file.component.spec.ts @@ -117,7 +117,7 @@ describe('AddFileComponent', () => { }); describe('#fileAccepted', () => { - it('should call this.createModel', fakeAsync(() => { + it('should call this.createModel if model file detected', fakeAsync(() => { let b = new Blob(['/**CTO File*/'], {type: 'text/plain'}); let file = new File([b], 'newfile.cto'); @@ -130,7 +130,7 @@ describe('AddFileComponent', () => { createMock.should.have.been.called; })); - it('should call this.createScript', fakeAsync(() => { + it('should call this.createScript if script file detected', fakeAsync(() => { let b = new Blob(['/**JS File*/'], {type: 'text/plain'}); let file = new File([b], 'newfile.js'); @@ -144,6 +144,20 @@ describe('AddFileComponent', () => { createMock.should.have.been.called; })); + it('should call this.createReadme if readme file detected', fakeAsync(() => { + + let b = new Blob(['/**README File*/'], {type: 'text/plain'}); + let file = new File([b], 'README.md'); + + let createMock = sandbox.stub(component, 'createReadme'); + let dataBufferMock = sandbox.stub(component, 'getDataBuffer') + .returns(Promise.resolve('some data')); + + component.fileAccepted(file); + tick(); + createMock.should.have.been.called; + })); + it('should call this.fileRejected when there is an error reading the file', fakeAsync(() => { let b = new Blob(['/**CTO File*/'], {type: 'text/plain'}); @@ -255,6 +269,20 @@ describe('AddFileComponent', () => { })); }); + describe('#createModel', () => { + it('should establish a readme file', async(() => { + component.businessNetwork = mockBusinessNetwork; + let dataBuffer = new Buffer('/**README File**/ read all the things'); + + component.createReadme(dataBuffer); + + component.fileType.should.equal('md'); + component.currentFileName.should.equal('README.md'); + component.currentFile.should.equal(dataBuffer.toString()); + })); + + }); + describe('#changeCurrentFileType', () => { it('should set current file to a script file, created by calling createScript with correct parameters', async(() => { let mockScript = sinon.createStubInstance(Script); diff --git a/packages/composer-playground/src/app/add-file/add-file.component.ts b/packages/composer-playground/src/app/add-file/add-file.component.ts index 48b4b2dafe..db522cb56e 100644 --- a/packages/composer-playground/src/app/add-file/add-file.component.ts +++ b/packages/composer-playground/src/app/add-file/add-file.component.ts @@ -21,7 +21,7 @@ export class AddFileComponent { expandInput: boolean = false; maxFileSize: number = 5242880; - supportedFileTypes: string[] = ['.js', '.cto']; + supportedFileTypes: string[] = ['.js', '.cto', '.md']; addModelNamespace: string = 'org.acme.model'; addModelFileName: string = 'models/org.acme.model'; @@ -64,6 +64,10 @@ export class AddFileComponent { this.expandInput = true; this.createModel(file, data); break; + case 'md': + this.expandInput = true; + this.createReadme(data); + break; default: throw new Error('Unexpected File Type'); } @@ -92,7 +96,7 @@ export class AddFileComponent { this.fileType = 'js'; let scriptManager = this.businessNetwork.getScriptManager(); let filename = file.name ? 'lib/' + file.name : this.addScriptFileName; - this.currentFile = scriptManager.createScript('lib/' + file.name || this.addScriptFileName, 'JS', dataBuffer.toString()); + this.currentFile = scriptManager.createScript(filename, 'JS', dataBuffer.toString()); this.currentFileName = this.currentFile.getIdentifier(); } @@ -104,6 +108,12 @@ export class AddFileComponent { this.currentFileName = this.currentFile.getFileName(); } + createReadme(dataBuffer) { + this.fileType = 'md'; + this.currentFile = dataBuffer.toString(); + this.currentFileName = 'README.md'; + } + fileRejected(reason: string) { this.alertService.errorStatus$.next(reason); } diff --git a/packages/composer-playground/src/app/basic-modals/replace-confirm/replace-confirm.component.html b/packages/composer-playground/src/app/basic-modals/replace-confirm/replace-confirm.component.html index b7401f76b9..c6e3be931d 100644 --- a/packages/composer-playground/src/app/basic-modals/replace-confirm/replace-confirm.component.html +++ b/packages/composer-playground/src/app/basic-modals/replace-confirm/replace-confirm.component.html @@ -3,7 +3,7 @@ -

{{headerMessage}}

+

Current definition will be replaced