Skip to content

Commit

Permalink
iss2568 fix Playground when no NPM connection (hyperledger-archives#2616
Browse files Browse the repository at this point in the history
)

* issue 2568 - fix Playground when no connection to NPM is available

Signed-off-by: Sam Smith <[email protected]>

* issue 2568 - fix Playground when no connection to NPM is available
add in fix for original issue to other fix for when no valid samples are available

Signed-off-by: Sam Smith <[email protected]>
  • Loading branch information
samjsmith authored and nklincoln committed Nov 15, 2017
1 parent a84592e commit 0cfc6ba
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ <h3 class="sub-title">Choose a Business Network Definition to start with:</h3>
</div>
</div>
<div *ngIf="!npmInProgress" class="sample-network-list-container">
<div class="sample-network-list"
[ngClass]="{'selected-network' : sampleNetworks[1].name === chosenNetwork.name}" id="base-samples">
<div class="sample-network-list-item"
<div class="sample-network-list" [ngClass]="{'selected-network' : sampleNetworks[0].name === chosenNetwork.name}" id="base-samples">
<div class="sample-network-list-item" *ngIf="sampleNetworks.length>1"
[ngClass]="{'selected-network' : sampleNetworks[1].name === chosenNetwork.name}"
(click)="selectNetwork(sampleNetworks[1])">
<img *ngIf="sampleNetworks[1].networkImage" src="{{sampleNetworks[1].networkImage}}">
Expand Down Expand Up @@ -99,7 +98,7 @@ <h3 class="sub-title">Choose a Business Network Definition to start with:</h3>

<h3>Samples on npm</h3>

<div class="sample-network-list" id="npm-samples">
<div class="sample-network-list" id="npm-samples" *ngIf="sampleNetworks.length>1">
<div class="sample-network-list-item"
[ngClass]="{'selected-network' : sampleNetwork.name === chosenNetwork.name}"
*ngFor=" let sampleNetwork of sampleNetworks | slice:2; let networkIndex=index"
Expand All @@ -111,6 +110,9 @@ <h3>Samples on npm</h3>
<div class="sample-network-name">{{sampleNetwork.name}}</div>
</div>
</div>
<div *ngIf="sampleNetworks.length==1">
<p class="error-message">Error: could not get any sample networks.</p>
</div>
</div>
</div>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,16 @@ describe('DeployComponent', () => {
});

describe('onShow', () => {

let selectNetworkStub;
let addEmptyNetworkOption;
beforeEach(() => {
selectNetworkStub = sinon.stub(component, 'selectNetwork');
});

it('should get the list of sample networks', fakeAsync(() => {
let selectNetworkStub = sinon.stub(component, 'selectNetwork');
let addEmptyNetworkOption = sinon.stub(component, 'addEmptyNetworkOption').returns([{name: 'empty'}, {name: 'modelOne'}, {name: 'modelTwo'}]);
mockBusinessNetworkService.getSampleList.returns(Promise.resolve([{name: 'modelTwo'}, {name: 'modelOne'}]));

addEmptyNetworkOption = sinon.stub(component, 'addEmptyNetworkOption').returns([{name: 'empty'}, {name: 'modelOne'}, {name: 'modelTwo'}]);
component.onShow();
component['npmInProgress'].should.equal(true);
tick();
Expand All @@ -188,15 +193,14 @@ describe('DeployComponent', () => {

it('should handle error', fakeAsync(() => {
mockBusinessNetworkService.getSampleList.returns(Promise.reject({message: 'some error'}));

addEmptyNetworkOption = sinon.stub(component, 'addEmptyNetworkOption').returns([{name: 'empty'}]);
component.onShow();

component['npmInProgress'].should.equal(true);
tick();

addEmptyNetworkOption.should.have.been.calledWith([]);
selectNetworkStub.should.have.been.calledWith({name: 'empty'});
component['npmInProgress'].should.equal(false);

mockAlertService.errorStatus$.next.should.have.been.called;
}));
});

Expand Down Expand Up @@ -287,7 +291,7 @@ describe('DeployComponent', () => {
component.closeSample();

component['sampleDropped'].should.equal(false);
selectStub.should.have.been.calledWith({network: 'two'});
selectStub.should.have.been.calledWith({network: 'one'});
});
}));

Expand Down
25 changes: 14 additions & 11 deletions packages/composer-playground/src/app/import/import.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ export abstract class ImportComponent implements OnInit {
this.npmInProgress = true;
return this.sampleBusinessNetworkService.getSampleList()
.then((sampleNetworkList) => {
this.sampleNetworks = this.addEmptyNetworkOption(sampleNetworkList);
this.selectNetwork(this.sampleNetworks[1]);
this.npmInProgress = false;

this.initNetworkList(sampleNetworkList);
})
.catch((error) => {
this.npmInProgress = false;
this.alertService.errorStatus$.next(error);
this.initNetworkList([]);
});
}

Expand Down Expand Up @@ -161,20 +157,17 @@ rule NetworkAdminSystem {

closeSample() {
this.sampleDropped = false;
this.selectNetwork(this.sampleNetworks[1]);
this.selectNetwork(this.sampleNetworks[0]);
}

addEmptyNetworkOption(networks: any[]): any[] {

let newOrder = [];

// Append new network option to the list.
newOrder.push(this.EMPTY_BIZNET);

console.log('>>', networks);
for (let i = 0; i < networks.length; i++) {
newOrder.push(networks[i]);
}

return newOrder;
}

Expand Down Expand Up @@ -235,4 +228,14 @@ rule NetworkAdminSystem {
this.alertService.errorStatus$.next(reason);
this.expandInput = false;
}

private initNetworkList(sampleNetworkList): void {
this.sampleNetworks = this.addEmptyNetworkOption(sampleNetworkList);
if (this.sampleNetworks.length === 1) {
this.selectNetwork(this.sampleNetworks[0]);
} else {
this.selectNetwork(this.sampleNetworks[1]);
}
this.npmInProgress = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ describe('UpdateComponent', () => {
});

describe('onShow', () => {
it('should get the list of sample networks', fakeAsync(() => {

let selectNetworkStub;
let addEmptyNetworkOption;
beforeEach(() => {
mockClientService.getBusinessNetwork.returns({getName: sinon.stub().returns('my-network')});
let selectNetworkStub = sinon.stub(component, 'selectNetwork');
let addEmptyNetworkOption = sinon.stub(component, 'addEmptyNetworkOption').returns([{name: 'empty'}, {name: 'modelOne'}, {name: 'modelTwo'}]);
mockBusinessNetworkService.getSampleList.returns(Promise.resolve([{name: 'modelTwo'}, {name: 'modelOne'}]));
selectNetworkStub = sinon.stub(component, 'selectNetwork');
});

it('should get the list of sample networks', fakeAsync(() => {
mockBusinessNetworkService.getSampleList.returns(Promise.resolve([{name: 'modelTwo'}, {name: 'modelOne'}]));
addEmptyNetworkOption = sinon.stub(component, 'addEmptyNetworkOption').returns([{name: 'empty'}, {name: 'modelOne'}, {name: 'modelTwo'}]);
component.onShow();
component['npmInProgress'].should.equal(true);
tick();
Expand All @@ -184,17 +189,15 @@ describe('UpdateComponent', () => {
}));

it('should handle error', fakeAsync(() => {
mockClientService.getBusinessNetwork.returns({getName: sinon.stub().returns('my-network')});
mockBusinessNetworkService.getSampleList.returns(Promise.reject({message: 'some error'}));

addEmptyNetworkOption = sinon.stub(component, 'addEmptyNetworkOption').returns([{name: 'empty'}]);
component.onShow();

component['npmInProgress'].should.equal(true);
tick();

addEmptyNetworkOption.should.have.been.calledWith([]);
selectNetworkStub.should.have.been.calledWith({name: 'empty'});
component['npmInProgress'].should.equal(false);

mockAlertService.errorStatus$.next.should.have.been.called;
}));
});

Expand Down Expand Up @@ -285,7 +288,7 @@ describe('UpdateComponent', () => {
component.closeSample();

component['sampleDropped'].should.equal(false);
selectStub.should.have.been.calledWith({network: 'two'});
selectStub.should.have.been.calledWith({network: 'one'});
});
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('SampleBusinessNetworkService', () => {
throw('should not get here');
})
.catch((error) => {
error.message.should.equal('some error');
error.should.match(/Error connecting to/);
});

tick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ export class SampleBusinessNetworkService {
}

public getSampleList(): Promise<any> {
return this.http.get(PLAYGROUND_API + '/api/getSampleList')
const URL = PLAYGROUND_API + '/api/getSampleList';
return this.http.get(URL)
.toPromise()
.then((response) => {
return response.json();
})
.catch((error) => {
throw(error);
throw('Error connecting to: ' + URL);
});
}

Expand Down

0 comments on commit 0cfc6ba

Please sign in to comment.