Skip to content

Commit

Permalink
Github components unit tests (hyperledger-archives#585)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
109 changes: 109 additions & 0 deletions packages/composer-playground/src/app/github/github.component.spec.ts
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);
});
})));
});
});
10 changes: 5 additions & 5 deletions packages/composer-playground/src/app/github/github.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Http} from '@angular/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Http } from '@angular/http';

import {SampleBusinessNetworkService} from '../services/samplebusinessnetwork.service';
import { SampleBusinessNetworkService } from '../services/samplebusinessnetwork.service';

@Component({
selector: 'github',
Expand All @@ -29,7 +29,7 @@ export class GithubComponent implements OnInit {
this.sampleBusinessNetworkService.setUpGithub(token.access_token);
this.sampleBusinessNetworkService.OPEN_SAMPLE = true;
return this.router.navigate(['/editor']);
})
});
});
}

Expand Down

0 comments on commit df0b91e

Please sign in to comment.