Skip to content

Commit

Permalink
Fix exiting unit tests + StorageService tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitgouthaman committed Sep 17, 2017
1 parent 369e14b commit 718e110
Show file tree
Hide file tree
Showing 17 changed files with 639 additions and 70 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 121 additions & 1 deletion src/app/add-stop/add-stop.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,130 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import {
MdSelectModule,
MdSnackBar,
MdSnackBarConfig,
} from '@angular/material';

import { BootstrapTableComponent } from '../bootstrap-table/bootstrap-table.component';
import { AddStopComponent } from './add-stop.component';
import { GeoService } from '../geo/geo.service';
import { ObaService, Region, Stop } from '../oba/oba.service';
import { StorageService } from '../storage/storage.service';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

describe('AddStopComponent', () => {
let component: AddStopComponent;
let fixture: ComponentFixture<AddStopComponent>;

const regions: Array<Region> = [
{
obaBaseUrl: 'reg1url',
regionName: 'reg1',
},
{
obaBaseUrl: 'reg2url',
regionName: 'reg2',
},
];
const stops: Array<Stop> = [
{
code: 'code1',
direction: 'N',
formattedDirection: 'North bound',
id: 'id1',
name: 'stop1',
region: regions[0],
},
{
code: 'code2',
direction: 'S',
formattedDirection: 'South bound',
id: 'id2',
name: 'stop2',
region: regions[0],
},
];

beforeEach(async(() => {
const geoServiceStub = {
getLocation() {
return new Promise<Coordinates>(resolve => {
const coords: Coordinates = {
accuracy: 1,
altitude: null,
altitudeAccuracy: null,
heading: null,
latitude: 101,
longitude: 102,
speed: null,
};
resolve(coords);
});
}
};

const obaServiceStub = {
getRegions() {
return new Promise<Array<Region>>(resolve => {
resolve(this.regions);
});
},
getDefaultRegion() {
return new Promise<Region>(resolve => {
resolve(this.regions[0]);
});
},
getNearbyStops(region: Region, coords: Coordinates) {
return new Promise<Array<Stop>>(resolve => {
resolve(this.stops);
});
},
getSpecificStop(region: Region, coords: Coordinates, stopNumber: number) {
return new Promise<Stop>(resolve => {
resolve(this.stops[0]);
});
},
};

const storageServiceStub = {
addStop(stop: Stop) {
return new Promise<boolean>(resolve => resolve(true));
},
};

const mdSnackBarStub = {
open(message: string, action?: string, config?: MdSnackBarConfig) {},
};

TestBed.configureTestingModule({
declarations: [ AddStopComponent ]
imports: [
FormsModule,
MdSelectModule,
NoopAnimationsModule,
],
declarations: [
AddStopComponent,
BootstrapTableComponent,
],
providers: [
{
provide: GeoService,
useValue: geoServiceStub
},
{
provide: ObaService,
useValue: obaServiceStub
},
{
provide: StorageService,
useValue: storageServiceStub
},
{
provide: MdSnackBar,
useValue: mdSnackBarStub
},
]
})
.compileComponents();
}));
Expand All @@ -17,6 +133,10 @@ describe('AddStopComponent', () => {
fixture = TestBed.createComponent(AddStopComponent);
component = fixture.componentInstance;
fixture.detectChanges();

this.geoService = TestBed.get(GeoService);
this.obaService = TestBed.get(ObaService);
this.storageService = TestBed.get(StorageService);
});

it('should be created', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/app/add-stop/add-stop.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {

import { MdSnackBar } from '@angular/material';

import { Region, Stop } from '../oba/oba';
import { ObaService } from '../oba/oba.service';
import { Region, Stop, ObaService } from '../oba/oba.service';
import { GeoService } from '../geo/geo.service';
import { StorageService } from '../storage/storage.service';

Expand Down
2 changes: 1 addition & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div fxLayout="column" fxLayoutGap="10px">
<md-toolbar color="primary">
<span>One Bus Away</span>
<span id="obatitle">One Bus Away</span>
<span class="md-spacer"></span>
<button *ngIf="!addStopsMode" (click)="toggleAddStopsMode()" md-button>Add stop</button>
<button *ngIf="addStopsMode" (click)="toggleAddStopsMode()" md-button>Done</button>
Expand Down
70 changes: 61 additions & 9 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,71 @@
import { TestBed, async } from '@angular/core/testing';

import { FormsModule } from '@angular/forms';
import {
MdToolbarModule,
MdSelectModule,
} from '@angular/material';
import { AppComponent } from './app.component';
import { AddStopComponent } from './add-stop/add-stop.component';
import { BootstrapTableComponent } from './bootstrap-table/bootstrap-table.component';
import { StopCollectionComponent } from './stop-collection/stop-collection.component';
import { StopDetailsComponent } from './stop-collection/stop-details/stop-details.component';
import { StorageService } from './storage/storage.service';
import { Region, Stop } from './oba/oba.service';

describe('AppComponent', () => {
const region: Region = {
obaBaseUrl: 'reg1url',
regionName: 'reg1',
};
const stops: Array<Stop> = [
{
code: 'code1',
direction: 'N',
formattedDirection: 'North bound',
id: 'id1',
name: 'stop1',
region: region,
},
{
code: 'code2',
direction: 'S',
formattedDirection: 'South bound',
id: 'id2',
name: 'stop2',
region: region,
},
];

beforeEach(async(() => {
const storageServiceStub = {
getAllStops(): Promise<Array<Stop>> {
return new Promise<Array<Stop>>(resolve => {
resolve(stops);
});
},
subscribeToAdd(onAdd: (stop: Stop) => void) { },
subscribeToRemove(onRemove: (stop: Stop) => void) { }
};

TestBed.configureTestingModule({
imports: [
FormsModule,
MdToolbarModule,
MdSelectModule,
],
declarations: [
AppComponent
AddStopComponent,
BootstrapTableComponent,
StopCollectionComponent,
StopDetailsComponent,
AppComponent,
],
providers: [
{
provide: StorageService,
useValue: storageServiceStub
},
]
}).compileComponents();
}));

Expand All @@ -17,16 +75,10 @@ describe('AppComponent', () => {
expect(app).toBeTruthy();
}));

it(`should have as title 'app'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app');
}));

it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app!');
expect(compiled.querySelector('#obatitle').textContent).toContain('One Bus Away');
}));
});
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { AddStopComponent } from './add-stop/add-stop.component';
import { ObaService } from './oba/oba.service';
import { GeoService } from './geo/geo.service';
import { StorageService } from './storage/storage.service';
import { SyncService } from './storage/sync.service';
import { StopDetailsComponent } from './stop-collection/stop-details/stop-details.component';
import { StopCollectionComponent } from './stop-collection/stop-collection.component';
import { BootstrapTableComponent } from './bootstrap-table/bootstrap-table.component';
Expand Down Expand Up @@ -57,6 +58,7 @@ import { BootstrapTableComponent } from './bootstrap-table/bootstrap-table.compo
ObaService,
GeoService,
StorageService,
SyncService,
],
bootstrap: [AppComponent]
})
Expand Down
23 changes: 0 additions & 23 deletions src/app/oba/oba.d.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/app/oba/oba.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { TestBed, inject } from '@angular/core/testing';

import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { ObaService } from './oba.service';

describe('ObaServiceService', () => {
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [ObaService]
});
httpMock = TestBed.get(HttpTestingController);
});

it('should be created', inject([ObaService], (service: ObaService) => {
Expand Down
Loading

0 comments on commit 718e110

Please sign in to comment.