Skip to content

Commit 76470da

Browse files
author
Weston Pace
committed
Created an example using mock services in mock configuration
1 parent 17ddcab commit 76470da

15 files changed

+135
-61
lines changed

angular.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
"extractLicenses": true,
4545
"vendorChunk": false,
4646
"buildOptimizer": true
47+
},
48+
"mock": {
49+
"fileReplacements": [
50+
{
51+
"replace": "src/environments/environment.ts",
52+
"with": "src/environments/environment.mock.ts"
53+
}
54+
]
4755
}
4856
}
4957
},
@@ -55,6 +63,9 @@
5563
"configurations": {
5664
"production": {
5765
"browserTarget": "servicesim:build:production"
66+
},
67+
"mock": {
68+
"browserTarget": "servicesim:build:mock"
5869
}
5970
}
6071
},
@@ -103,11 +114,11 @@
103114
"builder": "@angular-devkit/build-angular:protractor",
104115
"options": {
105116
"protractorConfig": "e2e/protractor.conf.js",
106-
"devServerTarget": "servicesim:serve"
117+
"devServerTarget": "servicesim:serve:mock"
107118
},
108119
"configurations": {
109120
"production": {
110-
"devServerTarget": "servicesim:serve:production"
121+
"devServerTarget": "servicesim:serve:mock"
111122
}
112123
}
113124
},

e2e/src/app.e2e-spec.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AppPage } from './app.po';
2+
import { browser } from 'protractor';
23

34
describe('workspace-project App', () => {
45
let page: AppPage;
@@ -7,8 +8,10 @@ describe('workspace-project App', () => {
78
page = new AppPage();
89
});
910

10-
it('should display welcome message', () => {
11-
page.navigateTo();
12-
expect(page.getParagraphText()).toEqual('Welcome to servicesim!');
11+
it('should have a simulated stock price', async () => {
12+
await page.navigateTo();
13+
await browser.sleep(2000);
14+
const stockPrice = await page.getStockText();
15+
expect(['125', '150', '117.3'].indexOf(stockPrice)).toBeGreaterThanOrEqual(0);
1316
});
1417
});

e2e/src/app.po.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class AppPage {
55
return browser.get('/');
66
}
77

8-
getParagraphText() {
9-
return element(by.css('app-root h1')).getText();
8+
getStockText() {
9+
return element(by.css('span')).getText();
1010
}
1111
}

src/app/app.component.html

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
1-
<!--The content below is only a placeholder and can be replaced.-->
2-
<div style="text-align:center">
3-
<h1>
4-
Welcome to {{ title }}!
5-
</h1>
6-
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
7-
</div>
8-
<h2>Here are some links to help you start: </h2>
9-
<ul>
10-
<li>
11-
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
12-
</li>
13-
<li>
14-
<h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a></h2>
15-
</li>
16-
<li>
17-
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
18-
</li>
19-
</ul>
20-
1+
<p>Apple Stock Price: <span id="stock">{{appleStockPrice}}</span></p>
2+
<p>Last Update Time: {{lastUpdateTime}}</p>

src/app/app.component.spec.ts

-27
This file was deleted.

src/app/app.component.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit, Inject, NgZone } from '@angular/core';
2+
import { StockService, STOCK_SERVICE } from './stock.service';
3+
import { interval } from 'rxjs';
4+
import { startWith, switchMap } from 'rxjs/operators';
25

36
@Component({
47
selector: 'app-root',
58
templateUrl: './app.component.html',
69
styleUrls: ['./app.component.css']
710
})
8-
export class AppComponent {
9-
title = 'servicesim';
11+
export class AppComponent implements OnInit {
12+
appleStockPrice = -1;
13+
lastUpdateTime = 'Not yet updated';
14+
15+
constructor(@Inject(STOCK_SERVICE) private stockService: StockService, private ngZone: NgZone) {
16+
17+
}
18+
19+
ngOnInit() {
20+
this.ngZone.runOutsideAngular(() => {
21+
interval(1000).pipe(startWith(0), switchMap(_ => this.stockService.getAppleStockPrice())).subscribe(stockPrice => {
22+
this.ngZone.run(() => {
23+
this.appleStockPrice = stockPrice;
24+
this.lastUpdateTime = new Date().toISOString();
25+
});
26+
});
27+
});
28+
}
29+
1030
}

src/app/app.module.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
3+
import { HttpClientModule } from '@angular/common/http';
34

45
import { AppComponent } from './app.component';
6+
import { environment } from '../environments/environment';
57

68
@NgModule({
79
declarations: [
810
AppComponent
911
],
1012
imports: [
11-
BrowserModule
13+
BrowserModule,
14+
HttpClientModule,
15+
environment.possiblyMockModule
16+
],
17+
providers: [
1218
],
13-
providers: [],
1419
bootstrap: [AppComponent]
1520
})
1621
export class AppModule { }

src/app/mock.module.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NgModule } from '@angular/core';
2+
import { STOCK_SERVICE } from './stock.service';
3+
import { MockStockService } from './stock.mock.service';
4+
5+
@NgModule({
6+
providers: [
7+
{ provide: STOCK_SERVICE, useClass: MockStockService }
8+
]
9+
})
10+
export class MockModule {
11+
12+
}

src/app/real.module.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NgModule } from '@angular/core';
2+
import { STOCK_SERVICE } from './stock.service';
3+
import { RealStockService } from './stock.real.service';
4+
5+
@NgModule({
6+
providers: [
7+
{ provide: STOCK_SERVICE, useClass: RealStockService }
8+
]
9+
})
10+
export class RealModule {
11+
12+
}

src/app/stock.mock.service.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { of } from 'rxjs';
2+
import { StockService } from './stock.service';
3+
import { Injectable } from '@angular/core';
4+
5+
@Injectable()
6+
export class MockStockService implements StockService {
7+
8+
private index = 0;
9+
private prices = [125, 150, 117.3];
10+
11+
getAppleStockPrice() {
12+
const priceToReturn = this.prices[this.index++];
13+
if (this.index === this.prices.length) {
14+
this.index = 0;
15+
}
16+
return of(priceToReturn);
17+
}
18+
19+
}

src/app/stock.real.service.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
import { Observable } from 'rxjs';
4+
import { StockService } from './stock.service';
5+
6+
@Injectable()
7+
export class RealStockService implements StockService {
8+
9+
constructor(private httpClient: HttpClient) {
10+
11+
}
12+
13+
getAppleStockPrice() {
14+
return this.httpClient.get('https://api.iextrading.com/1.0/stock/aapl/price') as Observable<number>;
15+
}
16+
17+
}

src/app/stock.service.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Observable } from 'rxjs';
2+
3+
export const STOCK_SERVICE = 'STOCK_SERVICE';
4+
export interface StockService {
5+
getAppleStockPrice(): Observable<number>;
6+
}

src/environments/environment.mock.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { MockModule } from '../app/mock.module';
2+
3+
export const environment = {
4+
production: false,
5+
possiblyMockModule: MockModule
6+
};
7+

src/environments/environment.prod.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { RealModule } from '../app/real.module';
2+
13
export const environment = {
2-
production: true
4+
production: true,
5+
possiblyMockModule: RealModule
36
};
7+

src/environments/environment.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { RealModule } from '../app/real.module';
2+
13
// This file can be replaced during build by using the `fileReplacements` array.
24
// `ng build ---prod` replaces `environment.ts` with `environment.prod.ts`.
35
// The list of file replacements can be found in `angular.json`.
46

57
export const environment = {
6-
production: false
8+
production: false,
9+
possiblyMockModule: RealModule
710
};
811

912
/*

0 commit comments

Comments
 (0)