Skip to content

Commit

Permalink
Update Playground Version Check (hyperledger-archives#2441)
Browse files Browse the repository at this point in the history
Made the playground version check happen sooner so that you won't get error messages, you will only get the message to clear local storage

contributes to hyperledger/composer#2208

Signed-off-by: Caroline Church <[email protected]>
  • Loading branch information
cazfletch authored Oct 24, 2017
1 parent 741ec5e commit e8e9c17
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 142 deletions.
30 changes: 14 additions & 16 deletions packages/composer-playground/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ describe('AppComponent', () => {
let mockAlertService: MockAlertService;
let mockModal;
let mockAdminService;
let mockConnectionProfileService;
let mockBusinessNetworkConnection;
let mockIdCard;
let mockIdentityService;
Expand All @@ -165,6 +164,8 @@ describe('AppComponent', () => {
let activatedRoute: ActivatedRouteStub;
let routerStub: RouterStub;

let checkVersionStub;

beforeEach(async(() => {
mockClientService = sinon.createStubInstance(ClientService);
mockInitializationService = sinon.createStubInstance(InitializationService);
Expand Down Expand Up @@ -217,9 +218,12 @@ describe('AppComponent', () => {
beforeEach(async(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
checkVersionStub = sinon.stub(component, 'checkVersion');
}));

function updateComponent() {
function updateComponent(checkVersion = true) {
checkVersionStub.returns(Promise.resolve(checkVersion));

// trigger initial data binding
fixture.detectChanges();

Expand Down Expand Up @@ -306,14 +310,13 @@ describe('AppComponent', () => {
});

it('should check version and open version modal', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(false));
let openVersionModalStub = sinon.stub(component, 'openVersionModal');
mockClientService.ensureConnected.returns(Promise.resolve());
mockClientService.getBusinessNetworkName.returns('bob');

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

updateComponent();
updateComponent(false);

tick();

Expand All @@ -323,7 +326,6 @@ describe('AppComponent', () => {
}));

it('should check version and not open version modal', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(true));
let openVersionModalStub = sinon.stub(component, 'openVersionModal');
mockClientService.ensureConnected.returns(Promise.resolve());
mockClientService.getBusinessNetworkName.returns('bob');
Expand All @@ -340,7 +342,6 @@ describe('AppComponent', () => {
}));

it('should not do anything on non navigation end events', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion');
let welcomeModalStub = sinon.stub(component, 'openWelcomeModal');

routerStub.eventParams = {url: '/', nav: 'start'};
Expand All @@ -349,12 +350,10 @@ describe('AppComponent', () => {

tick();

checkVersionStub.should.not.have.been.called;
welcomeModalStub.should.not.have.been.called;
}));

it('should show header links if logged in', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(true));
routerStub.eventParams = {url: '/editor', nav: 'end'};
mockClientService.ensureConnected.returns(Promise.resolve());
mockClientService.getBusinessNetworkName.returns('bob');
Expand All @@ -369,7 +368,6 @@ describe('AppComponent', () => {
}));

it('should not show header links if not logged in', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(true));
routerStub.eventParams = {url: '/login', nav: 'end'};

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

it('should not show header links if redirected to login', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(true));
routerStub.eventParams = {url: '/editor', nav: 'end', urlAfterRedirects: '/login'};

updateComponent();
Expand Down Expand Up @@ -826,8 +823,6 @@ describe('AppComponent', () => {
}));

it('should open the welcome modal', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(true));

activatedRoute.testParams = {};

updateComponent();
Expand All @@ -842,11 +837,9 @@ describe('AppComponent', () => {
}));

it('should open the version modal', fakeAsync(() => {
let checkVersionStub = sinon.stub(component, 'checkVersion').returns(Promise.resolve(false));

activatedRoute.testParams = {};

updateComponent();
updateComponent(false);

component['openWelcomeModal']();

Expand Down Expand Up @@ -895,7 +888,6 @@ describe('AppComponent', () => {
mockOnError = sinon.stub(component, 'onErrorStatus');
mockOnTransactionEvent = sinon.stub(component, 'onTransactionEvent');
mockQueryParamsUpdated = sinon.stub(component, 'queryParamsUpdated');

}));

it('should check the version return true', fakeAsync(() => {
Expand All @@ -906,6 +898,8 @@ describe('AppComponent', () => {

updateComponent();

checkVersionStub.restore();

component['checkVersion']().then((result) => {
result.should.equal(true);
});
Expand All @@ -926,6 +920,8 @@ describe('AppComponent', () => {

updateComponent();

checkVersionStub.restore();

component['checkVersion']().then((result) => {
result.should.equal(true);
});
Expand All @@ -947,6 +943,8 @@ describe('AppComponent', () => {

updateComponent();

checkVersionStub.restore();

component['checkVersion']().then((result) => {
result.should.equal(false);
});
Expand Down
14 changes: 7 additions & 7 deletions packages/composer-playground/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class AppComponent implements OnInit, OnDestroy {
private configService: ConfigService) {
}

ngOnInit() {
ngOnInit(): Promise<void> {
this.subs = [
this.alertService.busyStatus$.subscribe((busyStatus) => {
this.onBusyStatus(busyStatus);
Expand All @@ -80,6 +80,12 @@ export class AppComponent implements OnInit, OnDestroy {
this.processRouteEvent(e);
})
];

return this.checkVersion().then((success) => {
if (!success) {
this.openVersionModal();
}
});
}

ngOnDestroy() {
Expand All @@ -101,12 +107,6 @@ export class AppComponent implements OnInit, OnDestroy {
let welcomePromise;
if (event['url'] === '/login' && this.showWelcome) {
welcomePromise = this.openWelcomeModal();
} else {
welcomePromise = this.checkVersion().then((success) => {
if (!success) {
this.openVersionModal();
}
});
}

if (event['url'] === '/login' || event['urlAfterRedirects'] === '/login') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class InitializationService {

private config;

constructor(private clientService: ClientService,
private alertService: AlertService,
constructor(private alertService: AlertService,
private identityService: IdentityService,
private identityCardService: IdentityCardService,
private configService: ConfigService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
/* tslint:disable:no-unused-expression */
/* tslint:disable:no-var-requires */
/* tslint:disable:max-classes-per-file */
import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement, Component, Input } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Component, Input } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { TransactionComponent } from './transaction.component';
import { CodemirrorComponent } from 'ng2-codemirror';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ClientService } from '../../services/client.service';
import { InitializationService } from '../../services/initialization.service';

import {
Resource,
Expand All @@ -23,7 +20,6 @@ import {
ParticipantDeclaration,
TransactionDeclaration,
ClassDeclaration,
Property,
Factory,
ModelFile
} from 'composer-common';
Expand All @@ -50,7 +46,6 @@ describe('ResourceComponent', () => {

let mockNgbActiveModal;
let mockClientService;
let mockInitializationService;

let mockBusinessNetworkConnection;
let mockBusinessNetwork;
Expand All @@ -66,11 +61,9 @@ describe('ResourceComponent', () => {

mockNgbActiveModal = sinon.createStubInstance(NgbActiveModal);
mockClientService = sinon.createStubInstance(ClientService);
mockInitializationService = sinon.createStubInstance(InitializationService);

mockNgbActiveModal.open = sandbox.stub();
mockNgbActiveModal.close = sandbox.stub();
mockInitializationService.initialize.returns(Promise.resolve());

mockResource = sinon.createStubInstance(Resource);
mockBusinessNetworkConnection = sinon.createStubInstance(BusinessNetworkConnection);
Expand All @@ -95,8 +88,7 @@ describe('ResourceComponent', () => {
],
providers: [
{provide: NgbActiveModal, useValue: mockNgbActiveModal},
{provide: ClientService, useValue: mockClientService},
{provide: InitializationService, useValue: mockInitializationService}
{provide: ClientService, useValue: mockClientService}
]
});
fixture = TestBed.createComponent(ResourceComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,31 @@ export class ResourceComponent implements OnInit {
};

constructor(public activeModal: NgbActiveModal,
private clientService: ClientService,
private initializationService: InitializationService) {
private clientService: ClientService) {
}

ngOnInit(): Promise<any> {
return this.initializationService.initialize()
.then(() => {

// Determine what resource declaration we are using and stub json decription
let introspector = this.clientService.getBusinessNetwork().getIntrospector();
let modelClassDeclarations = introspector.getClassDeclarations();

modelClassDeclarations.forEach((modelClassDeclaration) => {
if (this.registryId === modelClassDeclaration.getFullyQualifiedName()) {

// Set resource declaration
this.resourceDeclaration = modelClassDeclaration;
this.resourceType = this.retrieveResourceType(modelClassDeclaration);

if (this.editMode()) {
this.resourceAction = 'Update';
let serializer = this.clientService.getBusinessNetwork().getSerializer();
this.resourceDefinition = JSON.stringify(serializer.toJSON(this.resource), null, 2);
} else {
// Stub out json definition
this.resourceAction = 'Create New';
this.generateResource();
}
ngOnInit() {
// Determine what resource declaration we are using and stub json decription
let introspector = this.clientService.getBusinessNetwork().getIntrospector();
let modelClassDeclarations = introspector.getClassDeclarations();

modelClassDeclarations.forEach((modelClassDeclaration) => {
if (this.registryId === modelClassDeclaration.getFullyQualifiedName()) {

// Set resource declaration
this.resourceDeclaration = modelClassDeclaration;
this.resourceType = this.retrieveResourceType(modelClassDeclaration);

if (this.editMode()) {
this.resourceAction = 'Update';
let serializer = this.clientService.getBusinessNetwork().getSerializer();
this.resourceDefinition = JSON.stringify(serializer.toJSON(this.resource), null, 2);
} else {
// Stub out json definition
this.resourceAction = 'Create New';
this.generateResource();
}
});

}
});
}

Expand All @@ -112,7 +106,7 @@ export class ResourceComponent implements OnInit {
/**
* Generate the json description of a resource
*/
private generateResource(withSampleData?: boolean): void {
private generateResource(withSampleData ?: boolean): void {
let businessNetworkDefinition = this.clientService.getBusinessNetwork();
let factory = businessNetworkDefinition.getFactory();
let idx = Math.round(Math.random() * 9999).toString();
Expand Down Expand Up @@ -152,25 +146,25 @@ export class ResourceComponent implements OnInit {
private addOrUpdateResource(): void {
this.actionInProgress = true;
return this.retrieveResourceRegistry(this.resourceType)
.then((registry) => {
let json = JSON.parse(this.resourceDefinition);
let serializer = this.clientService.getBusinessNetwork().getSerializer();
let resource = serializer.fromJSON(json);
resource.validate();
if (this.editMode()) {
return registry.update(resource);
} else {
return registry.add(resource);
}
})
.then(() => {
this.actionInProgress = false;
this.activeModal.close();
})
.catch((error) => {
this.definitionError = error.toString();
this.actionInProgress = false;
});
.then((registry) => {
let json = JSON.parse(this.resourceDefinition);
let serializer = this.clientService.getBusinessNetwork().getSerializer();
let resource = serializer.fromJSON(json);
resource.validate();
if (this.editMode()) {
return registry.update(resource);
} else {
return registry.add(resource);
}
})
.then(() => {
this.actionInProgress = false;
this.activeModal.close();
})
.catch((error) => {
this.definitionError = error.toString();
this.actionInProgress = false;
});
}

/**
Expand Down Expand Up @@ -213,7 +207,5 @@ export class ResourceComponent implements OnInit {
};

return types[type]();

}

}
Loading

0 comments on commit e8e9c17

Please sign in to comment.