Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Merged

Iss1471 #1516

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h3>All Transactions</h3>

<div class="side-button">
<div class="button-item">
<button type="button" class="primary" (click)="submitTransaction()">
<button [disabled]="!hasTransactions" type="button" class="primary" (click)="submitTransaction()">
<span>Submit Transaction</span>
</button>
</div>
Expand Down
68 changes: 65 additions & 3 deletions packages/composer-playground/src/app/test/test.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import * as sinon from 'sinon';

import * as chai from 'chai';
import { BusinessNetworkConnection } from 'composer-client';
import { Introspector,
BusinessNetworkDefinition,
TransactionDeclaration } from 'composer-common';

let should = chai.should();

Expand Down Expand Up @@ -48,8 +51,16 @@ describe('TestComponent', () => {
let mockAlertService;
let mockTransactionService;
let mockModal;

let mockIntrospector;
let mockBusinessNetwork;
let mockBusinessNetworkConnection;
let mockTransaction;

class MockModelClass {
isAbstract(): boolean {
return true;
}
}

let sandbox;

Expand All @@ -61,6 +72,12 @@ describe('TestComponent', () => {
mockAlertService = sinon.createStubInstance(AlertService);
mockModal = sinon.createStubInstance(NgbModal);
mockBusinessNetworkConnection = sinon.createStubInstance(BusinessNetworkConnection);
mockBusinessNetwork = sinon.createStubInstance(BusinessNetworkDefinition);
mockIntrospector = sinon.createStubInstance(Introspector);
mockTransaction = sinon.createStubInstance(TransactionDeclaration);

mockClientService.getBusinessNetwork.returns(mockBusinessNetwork);
mockBusinessNetwork.getIntrospector.returns(mockIntrospector);
mockTransactionService = sinon.createStubInstance(TransactionService);
mockBusinessNetworkConnection.listenerCount.returns(0);
mockBusinessNetworkConnection.on = sinon.stub();
Expand All @@ -87,21 +104,62 @@ describe('TestComponent', () => {
});

describe('ngOnInit', () => {

beforeEach(() => {
mockIntrospector.getClassDeclarations.returns([mockTransaction]);
mockAlertService.errorStatus$ = {next: sinon.stub()};
});

it('should create', () => {
component.should.be.ok;
});

it('should load all the registries', fakeAsync(() => {
it('should load all the registries and hasTransactions should be true', fakeAsync(() => {
mockInitializationService.initialize.returns(Promise.resolve());

mockBusinessNetworkConnection.getAllAssetRegistries.returns(Promise.resolve([{id: 'asset.fred'}, {id: 'asset.bob'}]));
mockBusinessNetworkConnection.getAllParticipantRegistries.returns(Promise.resolve([{id: 'participant.fred'}, {id: 'participant.bob'}]));
mockBusinessNetworkConnection.getTransactionRegistry.returns(Promise.resolve('transactionRegistry'));
mockClientService.getBusinessNetworkConnection.returns(mockBusinessNetworkConnection);

component.ngOnInit();
tick();

mockClientService.getBusinessNetworkConnection.should.have.been.called;
mockBusinessNetworkConnection.getAllAssetRegistries.should.have.been.called;

component['assetRegistries'].length.should.equal(2);

component['assetRegistries'][0].should.deep.equal({id: 'asset.bob', displayName: 'bob'});
component['assetRegistries'][1].should.deep.equal({id: 'asset.fred', displayName: 'fred'});

mockBusinessNetworkConnection.getAllParticipantRegistries.should.have.been.called;

component['participantRegistries'].length.should.equal(2);

component['participantRegistries'][0].should.deep.equal({id: 'participant.bob', displayName: 'bob'});
component['participantRegistries'][1].should.deep.equal({id: 'participant.fred', displayName: 'fred'});

mockBusinessNetworkConnection.getTransactionRegistry.should.have.been.called;

component['transactionRegistry'].should.equal('transactionRegistry');

component['chosenRegistry'].should.deep.equal({id: 'participant.bob', displayName: 'bob'});

mockClientService.getBusinessNetwork.should.have.been.called;
mockBusinessNetwork.getIntrospector.should.have.been.called;
mockIntrospector.getClassDeclarations.should.have.been.called;
component.hasTransactions.should.be.true;
}));

it('should load all the registries and hasTransactions should be false', fakeAsync(() => {
mockInitializationService.initialize.returns(Promise.resolve());

mockBusinessNetworkConnection.getAllAssetRegistries.returns(Promise.resolve([{id: 'asset.fred'}, {id: 'asset.bob'}]));
mockBusinessNetworkConnection.getAllParticipantRegistries.returns(Promise.resolve([{id: 'participant.fred'}, {id: 'participant.bob'}]));
mockBusinessNetworkConnection.getTransactionRegistry.returns(Promise.resolve('transactionRegistry'));
mockClientService.getBusinessNetworkConnection.returns(mockBusinessNetworkConnection);
mockIntrospector.getClassDeclarations.returns([new MockModelClass()]);

component.ngOnInit();

Expand All @@ -127,6 +185,11 @@ describe('TestComponent', () => {
component['transactionRegistry'].should.equal('transactionRegistry');

component['chosenRegistry'].should.deep.equal({id: 'participant.bob', displayName: 'bob'});

mockClientService.getBusinessNetwork.should.have.been.called;
mockBusinessNetwork.getIntrospector.should.have.been.called;
mockIntrospector.getClassDeclarations.should.have.been.called;
component.hasTransactions.should.be.false;
}));

it('should set chosen registry to first asset one if no participant registries', fakeAsync(() => {
Expand Down Expand Up @@ -218,7 +281,6 @@ describe('TestComponent', () => {
});

describe('submitTransaction', () => {
let mockTransaction;
beforeEach(() => {
mockTransaction = sinon.createStubInstance(Resource);
mockTransaction.getIdentifier.returns(1);
Expand Down
21 changes: 18 additions & 3 deletions packages/composer-playground/src/app/test/test.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClientService } from '../services/client.service';
import { InitializationService } from '../services/initialization.service';
import { AlertService } from '../basic-modals/alert.service';
import { TransactionComponent } from './transaction/transaction.component';
import { TransactionDeclaration } from 'composer-common';
import { TransactionService } from '../services/transaction.service';

@Component({
Expand All @@ -17,6 +18,7 @@ import { TransactionService } from '../services/transaction.service';

export class TestComponent implements OnInit, OnDestroy {

hasTransactions = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really pedantic, but could we have this as private like those beneath?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nklincoln Hey Nick, yeah I don't really like this either but I had to make it visible so that it could be examined for correctness in the unit test. Hope that's ok with you.

private assetRegistries = [];
private participantRegistries = [];
private transactionRegistry = null;
Expand All @@ -34,9 +36,18 @@ export class TestComponent implements OnInit, OnDestroy {
ngOnInit(): Promise<any> {
this.initializeEventListener();
return this.initializationService.initialize()
.then(() => {
return this.clientService.getBusinessNetworkConnection().getAllAssetRegistries();
})
.then(() => {

let introspector = this.clientService.getBusinessNetwork().getIntrospector();
let modelClassDeclarations = introspector.getClassDeclarations();
modelClassDeclarations.forEach((modelClassDeclaration) => {
// Generate list of all known (non-abstract) transaction types
if (!modelClassDeclaration.isAbstract() && modelClassDeclaration instanceof TransactionDeclaration) {
this.hasTransactions = true;
}
});

return this.clientService.getBusinessNetworkConnection().getAllAssetRegistries()
.then((assetRegistries) => {
assetRegistries.forEach((assetRegistry) => {
let index = assetRegistry.id.lastIndexOf('.');
Expand Down Expand Up @@ -78,6 +89,10 @@ export class TestComponent implements OnInit, OnDestroy {
.catch((error) => {
this.alertService.errorStatus$.next(error);
});
})
.catch((error) => {
this.alertService.errorStatus$.next(error);
});
}

ngOnDestroy() {
Expand Down