From 27562e92db4472c624334f9e475e4fff4d0897ff Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Wed, 20 Nov 2019 19:55:43 +0200 Subject: [PATCH] fix(docs-infra): fix StackBlitz and zipped `http` examples (#33941) Previously, the generated StackBlitz examples as well as the corresponding downloadable zips for the `http` guide examples were not correct and thus trying to run the app and/or tests would fail. This commit fixes the examples: - Replace `TestBed.inject()` (which was [introduced in v9][1]) with `TestBed.get()` (which is available in v8 used in the examples). (NOTE: The examples will soon be updated to v9 (as part of [FW-1609][2] and switched back to `TestBed.inject()` then.) - Include `src/app/heroes/hero.ts` in the zip, because it is referenced by some of the other files and the compilation fails without it. - Ensure `src/main-specs.ts` is not included in the zip that does not include the tests. Including the file broke the app, because there is logic in our zip-builder that renamed `main-*.ts` files to `main.ts` and thus `main-specs.ts` ended up overwriting the actual `main.ts`. [1]: https://next.angular.io/guide/deprecations#angularcoretesting [2]: https://angular-team.atlassian.net/browse/FW-1609 Fixes #33874 Fixes #33945 PR Close #33941 --- aio/content/examples/http/specs.stackblitz.json | 1 + .../examples/http/src/app/heroes/heroes.service.spec.ts | 8 ++++---- aio/content/examples/http/src/main.ts | 7 +++++++ aio/content/examples/http/src/testing/http-client.spec.ts | 5 +++-- aio/content/examples/http/stackblitz.json | 3 ++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/aio/content/examples/http/specs.stackblitz.json b/aio/content/examples/http/specs.stackblitz.json index 1442c98f3a..68c0874f18 100644 --- a/aio/content/examples/http/specs.stackblitz.json +++ b/aio/content/examples/http/specs.stackblitz.json @@ -1,6 +1,7 @@ { "description": "Http Guide Testing", "files":[ + "src/app/heroes/hero.ts", "src/app/heroes/heroes.service.ts", "src/app/heroes/heroes.service.spec.ts", diff --git a/aio/content/examples/http/src/app/heroes/heroes.service.spec.ts b/aio/content/examples/http/src/app/heroes/heroes.service.spec.ts index c3bf9bc7e0..bfc204b4ad 100644 --- a/aio/content/examples/http/src/app/heroes/heroes.service.spec.ts +++ b/aio/content/examples/http/src/app/heroes/heroes.service.spec.ts @@ -28,9 +28,9 @@ describe('HeroesService', () => { // Inject the http, test controller, and service-under-test // as they will be referenced by each test. - httpClient = TestBed.inject(HttpClient); - httpTestingController = TestBed.inject(HttpTestingController); - heroService = TestBed.inject(HeroesService); + httpClient = TestBed.get(HttpClient); + httpTestingController = TestBed.get(HttpTestingController); + heroService = TestBed.get(HeroesService); }); afterEach(() => { @@ -44,7 +44,7 @@ describe('HeroesService', () => { let expectedHeroes: Hero[]; beforeEach(() => { - heroService = TestBed.inject(HeroesService); + heroService = TestBed.get(HeroesService); expectedHeroes = [ { id: 1, name: 'A' }, { id: 2, name: 'B' }, diff --git a/aio/content/examples/http/src/main.ts b/aio/content/examples/http/src/main.ts index 6b6532d428..0740658908 100644 --- a/aio/content/examples/http/src/main.ts +++ b/aio/content/examples/http/src/main.ts @@ -1,5 +1,12 @@ // #docregion +import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; + import { AppModule } from './app/app.module'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} platformBrowserDynamic().bootstrapModule(AppModule); diff --git a/aio/content/examples/http/src/testing/http-client.spec.ts b/aio/content/examples/http/src/testing/http-client.spec.ts index d098397e79..f13c15a18b 100644 --- a/aio/content/examples/http/src/testing/http-client.spec.ts +++ b/aio/content/examples/http/src/testing/http-client.spec.ts @@ -27,8 +27,8 @@ describe('HttpClient testing', () => { }); // Inject the http service and test controller for each test - httpClient = TestBed.inject(HttpClient); - httpTestingController = TestBed.inject(HttpTestingController); + httpClient = TestBed.get(HttpClient); + httpTestingController = TestBed.get(HttpTestingController); }); // #enddocregion setup // #docregion afterEach @@ -67,6 +67,7 @@ describe('HttpClient testing', () => { httpTestingController.verify(); }); // #enddocregion get-test + it('can test HttpClient.get with matching header', () => { const testData: Data = {name: 'Test Data'}; diff --git a/aio/content/examples/http/stackblitz.json b/aio/content/examples/http/stackblitz.json index 6ef625c1a0..5d24738b29 100644 --- a/aio/content/examples/http/stackblitz.json +++ b/aio/content/examples/http/stackblitz.json @@ -5,7 +5,8 @@ "!**/*.js", "!src/testing/*.*", - "!src/index-specs.html" + "!src/index-specs.html", + "!src/main-specs.ts" ], "tags": ["http"] }