Skip to content

Commit

Permalink
Simplify code to get playground version in about service (hyperledger…
Browse files Browse the repository at this point in the history
…-archives#2941) (hyperledger-archives#3062)

Contributes to #2936

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti authored and Dave Kelsey committed Dec 22, 2017
1 parent 2af9bb6 commit 12c94df
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 124 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ packages/composer-website/linkresults.txt
packages/composer-website/jsondata
packages/composer-website/jekylldocs/api-doc-inline
packages/composer-website/jekylldocs/jsdoc
packages/composer-playground/src/assets/npmlist.json

packages/composer-systests/systestv1/tls/ca/fabric-ca-server.db
packages/composer-runtime-hlfv1/vendor/github.com/
Expand Down
4 changes: 0 additions & 4 deletions packages/composer-playground/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,3 @@ npm-debug.log
# IDE #
.idea/
*.swp

# Used to get package versions, created on npm install
/src/assets/npmlist.json

1 change: 0 additions & 1 deletion packages/composer-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"postversion": "git push && git push --tags",
"pretest": "npm run lint",
"posttest": "istanbul check-coverage",
"postinstall": "npm list -json --depth=0 2>&1 | grep -iv ERR > ./src/assets/npmlist.json",
"prebuild:dev": "npm run clean:dist",
"prebuild:prod": "npm run clean:dist",
"preclean:install": "npm run clean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('FooterComponent', () => {
fixture.detectChanges();
tick();
fixture.detectChanges();
component['playgroundVersion'].should.equal('1');
component['playgroundVersion'].should.match(/[0-9]+.[0-9]+.[0-9]+/);
component['config'].should.deep.equal(myConfig);

fixture.detectChanges();
Expand Down
92 changes: 10 additions & 82 deletions packages/composer-playground/src/app/services/about.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,30 @@
/* tslint:disable:max-classes-per-file */
import { TestBed, async, inject, fakeAsync, tick } from '@angular/core/testing';
import { AboutService } from './about.service';
import {
HttpModule,
Response,
ResponseOptions,
XHRBackend
} from '@angular/http';
import { MockBackend } from '@angular/http/testing';

import * as sinon from 'sinon';

const mockResponse = {
name: 'composer-playground',
version: '1',
dependencies: {
'composer-admin': {
version: '2'
},
'composer-client': {
version: '3'
},
'composer-common': {
version: '4'
}
}
};

const expectedResponse = {
playground: {
name: 'playground',
version: '1'
},
common: {
name: 'composer-common',
version: '2'
},
client: {
name: 'composer-client',
version: '3'
},
admin: {
name: 'composer-admin',
version: '4'
}
};

describe('AboutService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
imports: [],
providers: [
AboutService,
{provide: XHRBackend, useClass: MockBackend}
AboutService
]
});
});

it('it should make an http call to retrieve the json that shows the versions',
async(inject([AboutService, XHRBackend], (aboutService, mockBackend) => {
// setup a mocked response
mockBackend.connections.subscribe((connection) => {
connection.mockRespond(new Response(new ResponseOptions({
body: JSON.stringify(mockResponse)
})));
});

// make the call to the service which was injected
return aboutService.getVersions()
.then((versions) => {
versions.playground.name.should.equal('playground');
versions.playground.version.should.equal('1');
versions.admin.name.should.equal('composer-admin');
versions.admin.version.should.equal('2');
versions.client.name.should.equal('composer-client');
versions.client.version.should.equal('3');
versions.common.name.should.equal('composer-common');
versions.common.version.should.equal('4');
});
})));

it('should enter catch block', async(inject([AboutService, XHRBackend], (aboutService, mockBackend) => {
mockBackend.connections.subscribe(
(connection) => {
connection.mockError(new Error('error'));
}
);
it('it should make an http call to retrieve the json that shows the versions', fakeAsync(inject([AboutService], (aboutService) => {
// make the call to the service which was injected
aboutService.getVersions().then((versions) => {
versions.playground.name.should.equal('playground');
versions.playground.version.should.match(/[0-9]+.[0-9]+.[0-9]+/);
});

return aboutService.getVersions()
.then(() => {
// Ignore this
})
.catch((error) => {
error.message.should.equal('error');
});
tick();
})));

it('should return the version if already set', fakeAsync(inject([AboutService, XHRBackend], (aboutService, mockBackend) => {
it('should return the version if already set', fakeAsync(inject([AboutService], (aboutService) => {
aboutService['versions'] = {version: 'myVersion'};

let getModulesStub = sinon.stub(aboutService, 'getModules');
Expand Down
43 changes: 8 additions & 35 deletions packages/composer-playground/src/app/services/about.service.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { BehaviorSubject, Observable } from 'rxjs/Rx';
import * as packageObject from '../../../package.json';

@Injectable()
export class AboutService {
versions = null;
constructor(private http: Http) {
}

getVersions(): Promise<any> {
if (!this.versions) {
return this.getModules()
.then((modules) => {
this.versions = {
playground: {
name: 'playground',
version: modules.version
},
common: {
name: 'composer-common',
version: modules.dependencies['composer-common'].version
},
client: {
name: 'composer-client',
version: modules.dependencies['composer-client'].version,
},
admin: {
name: 'composer-admin',
version: modules.dependencies['composer-admin'].version
}
};
return this.versions;
});
} else {
return Promise.resolve(this.versions);
this.versions = {
playground: {
name: 'playground',
version: packageObject.version
}
};
}
}

private getModules(): Promise<any> {
return this.http.get('assets/npmlist.json')
.map((res) => res.json())
.toPromise();
return Promise.resolve(this.versions);
}
}

0 comments on commit 12c94df

Please sign in to comment.