Skip to content

Commit

Permalink
Generator fixes (hyperledger-archives#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeeyturner authored May 4, 2017
1 parent a897d86 commit 91ad5c7
Show file tree
Hide file tree
Showing 19 changed files with 466 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ describe('EditorComponent', () => {
beforeEach(() => {
sinon.stub(component, 'ngOnInit');
});

it('should toggle editing', () => {
component['editActive'] = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 { ClientService } from '../services/client.service';
import { InitializationService } from '../services/initialization.service';

import {
Expand Down Expand Up @@ -187,14 +187,14 @@ describe('ResourceComponent', () => {
mockClassDeclaration.getModelFile.returns(mockModelFile);
mockClassDeclaration.getName.returns('class.declaration');
mockClassDeclaration.getIdentifierFieldName.returns('resourceId');

});

it('should generate a valid resource', () => {

mockSerializer.toJSON.returns({'$class': 'com.org'});
mockSerializer.fromJSON.returns(mockResource);
mockResource.validate = sandbox.stub();
mockSerializer.toJSON.returns({'$class': 'com.org'});
mockSerializer.fromJSON.returns(mockResource);
mockResource.validate = sandbox.stub();
component['resourceDeclaration'] = mockClassDeclaration;

// should start clean
Expand All @@ -216,15 +216,15 @@ describe('ResourceComponent', () => {

it('should set definitionError on serializer fail', () => {
component['resourceDeclaration'] = mockClassDeclaration;

// Set serializer to throw
mockSerializer.toJSON = () => {
throw new Error('error');
};

// should start clean
should.not.exist(component['definitionError']);

// Run method
component['generateResource']();

Expand All @@ -233,8 +233,8 @@ describe('ResourceComponent', () => {
});

it('should set definitionError on validation fail', () => {
mockSerializer.toJSON.returns({'$class': 'com.org'});
mockSerializer.fromJSON.returns(mockResource);
mockSerializer.toJSON.returns({'$class': 'com.org'});
mockSerializer.fromJSON.returns(mockResource);
component['resourceDeclaration'] = mockClassDeclaration;

// Set validate to throw
Expand Down Expand Up @@ -338,9 +338,9 @@ describe('ResourceComponent', () => {

// Insert definition error
component['definitionError'] = 'Error: forced error content';

// Check that the UI is showing the error
fixture.detectChanges();
fixture.detectChanges();
element = fixture.debugElement.query(By.css('.resource-error-text')).nativeElement;
element.textContent.should.contain('Error: forced error content');

Expand All @@ -351,10 +351,10 @@ describe('ResourceComponent', () => {

// Insert definition error
component['definitionError'] = 'Error: forced error content';

// Check that the transaction submission button is disabled in UI
fixture.detectChanges();
element = fixture.debugElement.query(By.css('#createResourceButton')).nativeElement;
element = fixture.debugElement.query(By.css('#createResourceButton')).nativeElement;
(element as HTMLButtonElement).disabled.should.be.true;

});
Expand All @@ -364,18 +364,18 @@ describe('ResourceComponent', () => {

// Insert definition error
component['definitionError'] = 'Error: forced error content';

// Check that the transaction submission button is disabled
fixture.detectChanges();
element = fixture.debugElement.query(By.css('#createResourceButton')).nativeElement;
element = fixture.debugElement.query(By.css('#createResourceButton')).nativeElement;
(element as HTMLButtonElement).disabled.should.be.true;

// Fix the definition error
component['definitionError'] = undefined;

// Check that the transaction submission button is enabled
fixture.detectChanges();
element = fixture.debugElement.query(By.css('#createResourceButton')).nativeElement;
element = fixture.debugElement.query(By.css('#createResourceButton')).nativeElement;
(element as HTMLButtonElement).disabled.should.be.false;

});
Expand Down
166 changes: 91 additions & 75 deletions packages/composer-playground/src/app/services/identity.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,170 +1,186 @@
/* tslint:disable:no-unused-variable */

import {TestBed, inject, fakeAsync, tick} from '@angular/core/testing';
import {IdentityService} from './identity.service';
import {LocalStorageService} from 'angular-2-local-storage';
import {ConnectionProfileService} from './connectionprofile.service';
import {WalletService} from './wallet.service';
import { TestBed, async, inject, fakeAsync, tick } from '@angular/core/testing';
import { IdentityService } from './identity.service';
import { LocalStorageService } from 'angular-2-local-storage';
import { ConnectionProfileService } from './connectionprofile.service';
import { ClientService } from './client.service';
import { WalletService } from './wallet.service';
import * as sinon from 'sinon';

import {FileWallet} from 'composer-common';

describe('IdentityService', () => {

let mockLocalStorageService;
let mockConnectionProfileService;
let mockWalletService;


beforeEach(() => {
mockLocalStorageService = sinon.createStubInstance(LocalStorageService);
mockConnectionProfileService = sinon.createStubInstance(ConnectionProfileService);
mockWalletService = sinon.createStubInstance(WalletService);

TestBed.configureTestingModule({
providers: [IdentityService,
{provide: LocalStorageService, useValue: mockLocalStorageService},
{provide: ConnectionProfileService, useValue: mockConnectionProfileService},
{provide: WalletService, useValue: mockWalletService}]
{provide: LocalStorageService, useValue: mockLocalStorageService},
{provide: ConnectionProfileService, useValue: mockConnectionProfileService},
{provide: WalletService, useValue:mockWalletService}]
});
});

describe('getCurrentIdentities', () => {
describe('getCurrentIdentities', () =>{
it('should get current identities', fakeAsync(inject([IdentityService], (service: IdentityService) => {
mockConnectionProfileService.getCurrentConnectionProfile.returns('bob');
let stubGetIdentities = sinon.stub(service, 'getIdentities');

service.getCurrentIdentities();
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}")
let stubGetIdentities = sinon.stub(service,'getIdentities');
stubGetIdentities.returns(Promise.resolve(['identity1','identity2']))
service.getCurrentIdentities().then((currentIdentities) => {
currentIdentities.should.deep.equal(['identity1','identity2']);
})

tick();

mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called;
stubGetIdentities.should.have.been.calledWith('bob');
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}")

})));
});
})

describe('getIdentities', () => {
describe('getIdentities', () =>{
it('should get identities', fakeAsync(inject([IdentityService], (service: IdentityService) => {

let stubFileWallet = {list: sinon.stub().returns(Promise.resolve(['identity2', 'identity1']))};

let stubFileWallet = sinon.createStubInstance(FileWallet)
stubFileWallet.list.returns(Promise.resolve(['identity2','identity1']));
mockWalletService.getWallet.returns(stubFileWallet);

service.getIdentities("{'name':'profile','type': 'hlf'}");
service.getIdentities("{'name':'profile','type': 'hlf'}").then((identities) => {
tick();
identities.should.deep.equal(['identity1','identity2']);
mockWalletService.getWallet.should.be.calledWith("{'name':'profile','type': 'hlf'}")
stubFileWallet.list.should.be.called;
})

tick();

mockWalletService.getWallet.should.have.been.calledWith("{'name':'profile','type': 'hlf'}");

stubFileWallet.list.should.have.been.called;

})));
});
})

describe('getCurrentIdentity', () => {
it('should get current identity', fakeAsync(inject([IdentityService], (service: IdentityService) => {
let stubGetIdentity = sinon.stub(service, 'getIdentity');
stubGetIdentity.returns(Promise.resolve('identity1'));
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}");
let stubGetIdentity = sinon.stub(service,'getIdentity');
stubGetIdentity.returns(Promise.resolve('identity1'))
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}")

service.getCurrentIdentity().then((currentIdentity) => {
stubGetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}");
currentIdentity.should.equal('identity1');
});
})

tick();

stubGetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}");

})));
});
})


describe('getIdentity', () => {
it('should get an identity if it exists', fakeAsync(inject([IdentityService], (service: IdentityService) => {
mockLocalStorageService.get.returns('identity1');
let stubGetIdentities = sinon.stub(service, 'getIdentities');
stubGetIdentities.returns(Promise.resolve(['identity1', 'identity2']));
let stubGetIdentities = sinon.stub(service,'getIdentities');
stubGetIdentities.returns(Promise.resolve(['identity1','identity2']));

service.getIdentity("{'name':'profile','type': 'hlf'}").then((identity) => {
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}");
});
service.getIdentity("{'name':'profile','type': 'hlf'}");

tick();

})));
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}");

})))
it('should return another identity if the wanted identity doesnt exist', fakeAsync(inject([IdentityService], (service: IdentityService) => {
mockLocalStorageService.get.returns('identity3');
let stubGetIdentities = sinon.stub(service, 'getIdentities');
stubGetIdentities.returns(Promise.resolve(['identity1', 'identity2']));
let stubGetIdentities = sinon.stub(service,'getIdentities');
stubGetIdentities.returns(Promise.resolve(['identity1','identity2']));

service.getIdentity("{'name':'profile','type': 'hlf'}").then((identity) => {
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}");
});
service.getIdentity("{'name':'profile','type': 'hlf'}");

tick();

})));
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}");

})));
it('should return null if no identites exist', fakeAsync(inject([IdentityService], (service: IdentityService) => {
mockLocalStorageService.get.returns('identity3');
let stubGetIdentities = sinon.stub(service, 'getIdentities');
let stubGetIdentities = sinon.stub(service,'getIdentities');
stubGetIdentities.returns(Promise.resolve([]));

service.getIdentity("{'name':'profile','type': 'hlf'}").then((identity) => {
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}");
});
service.getIdentity("{'name':'profile','type': 'hlf'}");

tick();

})));
});
stubGetIdentities.should.be.calledWith("{'name':'profile','type': 'hlf'}");

})))
})

describe('setCurrentIdentity', () => {
it('should set current identity', inject([IdentityService], (service: IdentityService) => {
let stubSetIdentity = sinon.stub(service, 'setIdentity');
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}");
it('should set current identity', fakeAsync(inject([IdentityService], (service: IdentityService) => {
let stubSetIdentity = sinon.stub(service,'setIdentity');
mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}")

service.setCurrentIdentity('identity1');
stubSetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}", 'identity1');
}));
});

tick();

stubSetIdentity.should.be.calledWith("{'name':'profile','type': 'hlf'}",'identity1');

})))
})

describe('setIdentity', () => {
it('should set identity', inject([IdentityService], (service: IdentityService) => {
it('should set identity', fakeAsync(inject([IdentityService], (service: IdentityService) => {

service.setIdentity("{'name':'profile','type': 'hlf'}", 'identity1');
mockLocalStorageService.set.should.be.calledWith("currentIdentity:{'name':'profile','type': 'hlf'}", 'identity1')
service.setIdentity("{'name':'profile','type': 'hlf'}",'identity1');
tick();
mockLocalStorageService.set.should.be.calledWith("currentIdentity:{'name':'profile','type': 'hlf'}",'identity1')

}));
});
})))
})

describe('getUserID', () => {
it('should get user id', fakeAsync(inject([IdentityService], (service: IdentityService) => {
let stubGetCurrentIdentity = sinon.stub(service, 'getCurrentIdentity');
stubGetCurrentIdentity.returns(Promise.resolve('currentIdentity'));
stubGetCurrentIdentity.returns(Promise.resolve('currentIdentity'))
service.getUserID().then((userID) => {
stubGetCurrentIdentity.should.be.called;
userID.should.equal('currentIdentity');
});
})

tick();

})));
});
stubGetCurrentIdentity.should.be.called;

})))
})

describe('getUserSecret', () => {
it('should get user secret', fakeAsync(inject([IdentityService], (service: IdentityService) => {
let stubGetCurrentIdentity = sinon.stub(service, 'getCurrentIdentity');
stubGetCurrentIdentity.returns(Promise.resolve('currentIdentity'));

mockConnectionProfileService.getCurrentConnectionProfile.returns('bob');

let stubFileWallet = {get: sinon.stub().returns(Promise.resolve('secret2'))};

mockConnectionProfileService.getCurrentConnectionProfile.returns("{'name':'profile','type': 'hlf'}");
let stubFileWallet = sinon.createStubInstance(FileWallet)
stubFileWallet.get.returns(Promise.resolve('secret2'));
mockWalletService.getWallet.returns(stubFileWallet);

service.getUserSecret();
let result = service.getUserSecret().then((result) => {
result.should.equal('secret2');
stubGetCurrentIdentity.should.be.called;
mockConnectionProfileService.getCurrentConnectionProfile.should.be.called;
mockWalletService.getWallet.should.be.calledWith("{'name':'profile','type': 'hlf'}");
})

tick();
mockConnectionProfileService.getCurrentConnectionProfile.should.have.been.called;
mockWalletService.getWallet.should.be.calledWith('bob');

stubFileWallet.get.should.have.been.calledWith('currentIdentity');
})));
});
})))
})

});
Loading

0 comments on commit 91ad5c7

Please sign in to comment.