forked from hyperledger-archives/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Github components unit tests (hyperledger-archives#585)
* Added unit tests for add file component * Finished add-file component test and edit import to clear stubs * Bumped up about.service code coverage * Fixed minor linting issues * Removed fdescribe * Increase code coverage of add-file to 100% * Export AssetRegistry and ParticipantRegistry * Registry unit tests * First trasaction component test * Transaction component unit tests * Remove fdiscribe * Added resource unit tests * Github components unit tests * Added test file
- Loading branch information
Liam Grace
authored
Mar 30, 2017
1 parent
2b0b595
commit df0b91e
Showing
2 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
packages/composer-playground/src/app/github/github.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { TestBed, async, inject, fakeAsync, tick } from '@angular/core/testing'; | ||
import { | ||
HttpModule, | ||
Http, | ||
Response, | ||
ResponseOptions, | ||
XHRBackend, | ||
ConnectionBackend | ||
} from '@angular/http'; | ||
import { ActivatedRoute, Router } from '@angular/router'; | ||
import { MockBackend } from '@angular/http/testing'; | ||
import { SampleBusinessNetworkService } from './../services/samplebusinessnetwork.service'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { GithubComponent } from './github.component'; | ||
|
||
import * as sinon from 'sinon'; | ||
let should = chai.should(); | ||
|
||
class MockActivatedRoute { | ||
queryParams = Observable.of({code: 'code'}); | ||
} | ||
|
||
class MockRouter { | ||
navigate = sinon.stub(); | ||
} | ||
|
||
describe('GithubComponent', () => { | ||
let sandbox; | ||
let fixture; | ||
let component; | ||
let mockSampleBusinessNetwork; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
|
||
mockSampleBusinessNetwork = sinon.createStubInstance(SampleBusinessNetworkService); | ||
mockSampleBusinessNetwork.setUpGithub = sandbox.stub(); | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [HttpModule], | ||
declarations: [ | ||
GithubComponent | ||
], | ||
providers: [ | ||
{ provide: ActivatedRoute, useClass: MockActivatedRoute }, | ||
{ provide: Router, useClass: MockRouter}, | ||
{ provide: ConnectionBackend, useClass: MockBackend }, | ||
{ provide: SampleBusinessNetworkService, useValue: mockSampleBusinessNetwork }, | ||
Http | ||
] | ||
}); | ||
|
||
fixture = TestBed.createComponent(GithubComponent); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
describe('#ngOnInit', () => { | ||
it('should setup the component', fakeAsync(() => { | ||
sandbox.stub(component, 'exchangeCodeForAccessToken') | ||
.returns(Promise.resolve({access_token: 'token'})); | ||
|
||
component.ngOnInit(); | ||
tick(); | ||
mockSampleBusinessNetwork.setUpGithub.should.be.called; | ||
mockSampleBusinessNetwork.setUpGithub.should.be.calledWith('token'); | ||
component.router.navigate.should.be.calledWith(['/editor']); | ||
})); | ||
}); | ||
|
||
describe('#exchangeCodeForAccessToken', () => { | ||
|
||
it('should get a token back from the http request', | ||
async(inject([ConnectionBackend], (mockBackend) => { | ||
|
||
const mockResponse = { | ||
access_token: 'token' | ||
}; | ||
|
||
mockBackend.connections.subscribe((connection) => { | ||
connection.mockRespond(new Response(new ResponseOptions({ | ||
body: JSON.stringify(mockResponse) | ||
}))); | ||
}); | ||
|
||
component.exchangeCodeForAccessToken('code') | ||
.then((response) => { | ||
response.should.deep.equal(mockResponse); | ||
}); | ||
}))); | ||
|
||
it('should give an error back for a http request', | ||
async(inject([ConnectionBackend], (mockBackend) => { | ||
mockBackend.connections.subscribe( | ||
(connection) => { | ||
connection.mockError(new Error('error')); | ||
} | ||
); | ||
|
||
component.exchangeCodeForAccessToken('code') | ||
.then((response) => { | ||
should.not.exist(response); | ||
}) | ||
.catch(error => { | ||
should.exist(error); | ||
}); | ||
}))); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters