Skip to content

Commit

Permalink
Refactor playground to work primarily with identity cards, rather than (
Browse files Browse the repository at this point in the history
hyperledger-archives#1780)

connection profiles and identities

Contributes to #902

Signed-off-by: James Taylor <[email protected]>
  • Loading branch information
jt-nti authored and Caroline Church committed Aug 18, 2017
1 parent 62fdf9b commit ce92f6b
Show file tree
Hide file tree
Showing 29 changed files with 626 additions and 1,240 deletions.
195 changes: 13 additions & 182 deletions packages/composer-playground/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ import { Directive, Input, Injectable } from '@angular/core';
import { AppComponent } from './app.component';
import { ClientService } from './services/client.service';
import { InitializationService } from './services/initialization.service';
import { ConnectionProfileService } from './services/connectionprofile.service';
import { IdentityService } from './services/identity.service';
import { IdentityCardService } from './services/identity-card.service';
import { LocalStorageService } from 'angular-2-local-storage';
import { AlertService } from './basic-modals/alert.service';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ActivatedRoute, Router, NavigationEnd, NavigationStart } from '@angular/router';
import { BusinessNetworkConnection } from 'composer-client';
import { AdminService } from './services/admin.service';
import { WalletService } from './services/wallet.service';
import { AboutService } from './services/about.service';

import { FileWallet } from 'composer-common';
import { FileWallet, IdCard } from 'composer-common';

import * as sinon from 'sinon';

Expand Down Expand Up @@ -150,8 +149,9 @@ describe('AppComponent', () => {
let mockAdminService;
let mockConnectionProfileService;
let mockBusinessNetworkConnection;
let mockWalletService;
let mockIdCard;
let mockIdentityService;
let mockIdentityCardService;
let mockLocalStorageService;
let mockAboutService;
let mockAdminConnection;
Expand All @@ -170,9 +170,11 @@ describe('AppComponent', () => {
mockModal = sinon.createStubInstance(NgbModal);
mockBusinessNetworkConnection = sinon.createStubInstance(BusinessNetworkConnection);
mockAdminService = sinon.createStubInstance(AdminService);
mockConnectionProfileService = sinon.createStubInstance(ConnectionProfileService);
mockWalletService = sinon.createStubInstance(WalletService);
mockIdCard = sinon.createStubInstance(IdCard);
mockIdCard.getConnectionProfile.returns({name: '$default', type: 'web'});
mockIdentityService = sinon.createStubInstance(IdentityService);
mockIdentityCardService = sinon.createStubInstance(IdentityCardService);
mockIdentityCardService.getCurrentIdentityCard.returns(mockIdCard);
mockLocalStorageService = sinon.createStubInstance(LocalStorageService);
mockAboutService = sinon.createStubInstance(AboutService);
mockAdminConnection = sinon.createStubInstance(AdminConnection);
Expand All @@ -198,9 +200,8 @@ describe('AppComponent', () => {
{provide: ActivatedRoute, useValue: activatedRoute},
{provide: Router, useValue: routerStub},
{provide: AdminService, useValue: mockAdminService},
{provide: ConnectionProfileService, useValue: mockConnectionProfileService},
{provide: WalletService, useValue: mockWalletService},
{provide: IdentityService, useValue: mockIdentityService},
{provide: IdentityCardService, useValue: mockIdentityCardService},
{provide: LocalStorageService, useValue: mockLocalStorageService},
{provide: AboutService, useValue: mockAboutService}
]
Expand Down Expand Up @@ -307,7 +308,6 @@ describe('AppComponent', () => {
let openVersionModalStub = sinon.stub(component, 'openVersionModal');
mockClientService.ensureConnected.returns(Promise.resolve());
mockClientService.getBusinessNetworkName.returns('bob');
mockConnectionProfileService.getCurrentConnectionProfile.returns('$default');

routerStub.eventParams = {url: '/bob', nav: 'end'};

Expand All @@ -325,7 +325,6 @@ describe('AppComponent', () => {
let openVersionModalStub = sinon.stub(component, 'openVersionModal');
mockClientService.ensureConnected.returns(Promise.resolve());
mockClientService.getBusinessNetworkName.returns('bob');
mockConnectionProfileService.getCurrentConnectionProfile.returns('$default');

routerStub.eventParams = {url: '/bob', nav: 'end'};

Expand Down Expand Up @@ -357,7 +356,6 @@ describe('AppComponent', () => {
routerStub.eventParams = {url: '/editor', nav: 'end'};
mockClientService.ensureConnected.returns(Promise.resolve());
mockClientService.getBusinessNetworkName.returns('bob');
mockConnectionProfileService.getCurrentConnectionProfile.returns('$default');

updateComponent();

Expand Down Expand Up @@ -509,147 +507,6 @@ describe('AppComponent', () => {
mockOnTransactionEvent = sinon.stub(component, 'onTransactionEvent');
errorStatusSpy = sinon.spy(mockAlertService.errorStatus$, 'next');
}));

it('should load the connection profiles when local and logged in', fakeAsync(() => {

component['showHeaderLinks'] = true;
component['connectionProfiles'] = [{name: 'test_name'}];
mockIdentityService.getCurrentIdentity.returns(Promise.resolve('bob'));
mockInitializationService.isWebOnly.returns(Promise.resolve(false));
let updateConnectionDataMock = sinon.stub(component, 'updateConnectionData').returns(Promise.resolve());

activatedRoute.testParams = {};

updateComponent();

tick();

// update now got info back about if local or not
updateComponent();

mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called;
updateConnectionDataMock.should.have.been.calledTwice;

mockInitializationService.initialize.should.have.been.called;

component['usingLocally'].should.equal(true);

links.length.should.equal(3);
links[0].linkParams.should.deep.equal(['editor']);
links[1].linkParams.should.deep.equal(['test']);
links[2].linkParams.should.deep.equal(['identity']);
}));

it('should load the connection profiles when web only and logged in', fakeAsync(() => {

component['showHeaderLinks'] = true;
component['connectionProfiles'] = [{name: 'test_name'}];
mockIdentityService.getCurrentIdentity.returns(Promise.resolve('bob'));
mockInitializationService.isWebOnly.returns(Promise.resolve(true));
let updateConnectionDataMock = sinon.stub(component, 'updateConnectionData').returns(Promise.resolve());

activatedRoute.testParams = {};

updateComponent();

tick();

// update now got info back about if local or not
updateComponent();

mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called;
updateConnectionDataMock.should.have.been.calledTwice;

mockInitializationService.initialize.should.have.been.called;

component['usingLocally'].should.equal(false);

links.length.should.equal(3);
links[0].linkParams.should.deep.equal(['editor']);
links[1].linkParams.should.deep.equal(['test']);
links[2].linkParams.should.deep.equal(['identity']);
}));

it('should load the connection profiles when local but not logged in', fakeAsync(() => {
mockIdentityService.getCurrentIdentity.returns(Promise.resolve('bob'));
mockInitializationService.isWebOnly.returns(Promise.resolve(false));
let updateConnectionDataMock = sinon.stub(component, 'updateConnectionData').returns(Promise.resolve());

activatedRoute.testParams = {};

updateComponent();

tick();

// update now got info back about if local or not
updateComponent();

mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called;
updateConnectionDataMock.should.have.been.calledTwice;

mockInitializationService.initialize.should.have.been.called;

component['usingLocally'].should.equal(true);

links.length.should.equal(0);
}));

it('should load the connection profiles when web only but not logged in', fakeAsync(() => {
mockIdentityService.getCurrentIdentity.returns(Promise.resolve('bob'));
mockInitializationService.isWebOnly.returns(Promise.resolve(true));
let updateConnectionDataMock = sinon.stub(component, 'updateConnectionData').returns(Promise.resolve());

activatedRoute.testParams = {};

updateComponent();

tick();

// update now got info back about if local or not
updateComponent();

mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called;
updateConnectionDataMock.should.have.been.calledTwice;

mockInitializationService.initialize.should.have.been.called;

component['usingLocally'].should.equal(false);

links.length.should.equal(0);
}));
});

describe('updateConnectionData', () => {
let mockOnBusy;
let mockOnError;
let mockOnTransactionEvent;
let mockQueryParamsUpdated;

beforeEach(async(() => {
mockOnBusy = sinon.stub(component, 'onBusyStatus');
mockOnError = sinon.stub(component, 'onErrorStatus');
mockOnTransactionEvent = sinon.stub(component, 'onTransactionEvent');
mockQueryParamsUpdated = sinon.stub(component, 'queryParamsUpdated');
}));

it('should update the connection profile data', fakeAsync(() => {
mockAdminConnection.getAllProfiles.returns(Promise.resolve(Promise.resolve({bob: {type: 'web'}})));
mockAdminService.getAdminConnection.returns(mockAdminConnection);
mockIdentityService.getCurrentIdentities.returns(Promise.resolve(['bob', 'fred']));

activatedRoute.testParams = {};

updateComponent();

component.updateConnectionData();

tick();

component['connectionProfiles'].length.should.equal(1);
component['connectionProfiles'].should.deep.equal([{default: false, name: 'bob', profile: {type: 'web'}}]);

component['identities'].should.deep.equal(['bob', 'fred']);
}));
});

describe('onBusyStatus', () => {
Expand All @@ -667,8 +524,6 @@ describe('AppComponent', () => {
}));

it('should not show if in web mode', () => {
mockConnectionProfileService.getCurrentConnectionProfile.returns('$default');

activatedRoute.testParams = {};

updateComponent();
Expand All @@ -683,6 +538,7 @@ describe('AppComponent', () => {
});

it('should open the modal', () => {
mockIdCard.getConnectionProfile.returns({name: 'notWebMode'});
activatedRoute.testParams = {};

updateComponent();
Expand All @@ -699,6 +555,7 @@ describe('AppComponent', () => {
});

it('should open the modal and update the status', () => {
mockIdCard.getConnectionProfile.returns({name: 'notWebMode'});
activatedRoute.testParams = {};

updateComponent();
Expand All @@ -719,6 +576,7 @@ describe('AppComponent', () => {
});

it('should open the modal and close it', () => {
mockIdCard.getConnectionProfile.returns({name: 'notWebMode'});
activatedRoute.testParams = {};

updateComponent();
Expand All @@ -739,6 +597,7 @@ describe('AppComponent', () => {
});

it('should not open the modal if no message', () => {
mockIdCard.getConnectionProfile.returns({name: 'notWebMode'});
activatedRoute.testParams = {};

updateComponent();
Expand Down Expand Up @@ -920,32 +779,6 @@ describe('AppComponent', () => {
}));
});

describe('openVersionModal', () => {
let mockOnBusy;
let mockOnError;
let mockOnTransactionEvent;
let mockQueryParamsUpdated;

beforeEach(async(() => {
mockOnBusy = sinon.stub(component, 'onBusyStatus');
mockOnError = sinon.stub(component, 'onErrorStatus');
mockOnTransactionEvent = sinon.stub(component, 'onTransactionEvent');
mockQueryParamsUpdated = sinon.stub(component, 'queryParamsUpdated');
mockModal.open.returns({componentInstance: {}});

}));

it('should open version modal', () => {
activatedRoute.testParams = {};

updateComponent();

component['openVersionModal']();

mockModal.open.should.have.been.called;
});
});

describe('openVersionModal', () => {
let mockOnBusy;
let mockOnError;
Expand Down Expand Up @@ -1102,8 +935,6 @@ describe('AppComponent', () => {
tick();

mockClientService.disconnect.should.have.been.called;
mockIdentityService.setCurrentIdentity.should.have.been.calledWith(null);
mockConnectionProfileService.setCurrentConnectionProfile.should.have.been.calledWith(null);
mockIdentityService.setLoggedIn.should.have.been.calledWith(false);
routerStub.navigate.should.have.been.calledWith(['/login']);
}));
Expand Down
Loading

0 comments on commit ce92f6b

Please sign in to comment.