File tree 4 files changed +83
-2
lines changed
4 files changed +83
-2
lines changed Original file line number Diff line number Diff line change @@ -34,8 +34,8 @@ All the scenarios are listed here below and nicely linked to the source file.
34
34
_ Test dynamic CSS styles using custom Jasmine matchers._
35
35
* [ mocking nested components] ( ./src/app/components/nested.component.spec.ts )
36
36
_ Learn how to mock out nested components which you don't want to necessarily test_
37
- * [ async pipes within templates ] ( ./src/app/components/async-stream .component.spec.ts )
38
- _ Shows how to correctly resolve async pipes and then verify they properly render in the HTML _
37
+ * [ testing component with service deps ] ( ./src/app/components/component-mock-external .component.spec.ts )
38
+ _ Simple test of component logic by manually instantiating the component _
39
39
1 . [ ** Testing Services** ] ( ./src/app/services )
40
40
* [ Simple stateless function] ( ./src/app/services/greeting.service.spec.ts )
41
41
_ Learn about different ways of injecting a service into a test case as well as how to test service methods._
@@ -45,6 +45,8 @@ All the scenarios are listed here below and nicely linked to the source file.
45
45
_ Learn how to mock external dependencies, such as use the ` MockBackend ` provided by Angular to respond to http calls._
46
46
1 . [ ** Testing Pipes** ] ( ./src/app/pipes )
47
47
* [ custom filter pipe] ( ./src/app/pipes/filter.pipe.spec.ts )
48
+ * [ async pipes within templates] ( ./src/app/components/async-stream.component.spec.ts )
49
+ _ Shows how to correctly resolve async pipes and then verify they properly render in the HTML_
48
50
1 . [ ** Custom Matchers and Utilities** ] ( ./src/app/utils )
49
51
* [ Create your own custom Jasmine matchers] ( ./src/app/utils/custom-matchers.ts )
50
52
Original file line number Diff line number Diff line change
1
+ /* tslint:disable:no-unused-variable */
2
+ import { Component } from '@angular/core' ;
3
+ import { ComponentFixture , TestBed , async } from '@angular/core/testing' ;
4
+ import { By } from '@angular/platform-browser' ;
5
+ import { WithExternalServiceComponent } from './component-mock-external.component' ;
6
+ import { LanguageService , LanguageEnum } from './language.service' ;
7
+
8
+ describe ( 'ContentProjectionComponent' , ( ) => {
9
+ let component : WithExternalServiceComponent ;
10
+ let langService : LanguageService ;
11
+
12
+ beforeEach ( ( ) => {
13
+ langService = new LanguageService ( ) ;
14
+ component = new WithExternalServiceComponent ( langService ) ;
15
+ } ) ;
16
+
17
+ afterEach ( ( ) => {
18
+ langService = null ;
19
+ component = null ;
20
+ } ) ;
21
+
22
+ it ( 'should greet in italian when the lang is IT' , ( ) => {
23
+ langService . setCurrentLanguage ( LanguageEnum . IT ) ;
24
+ expect ( component . getGreeting ( ) ) . toBe ( 'Ciao' ) ;
25
+ } ) ;
26
+
27
+ it ( 'should greet in german when the lang is DE' , ( ) => {
28
+ spyOnProperty ( langService , 'currentLang' , 'get' ) . and . returnValue (
29
+ LanguageEnum . DE
30
+ ) ;
31
+ expect ( component . getGreeting ( ) ) . toBe ( 'Hallo' ) ;
32
+ } ) ;
33
+
34
+ it ( 'should greet in english by default' , ( ) => {
35
+ expect ( component . getGreeting ( ) ) . toBe ( 'Hi' ) ;
36
+ } ) ;
37
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { LanguageService , LanguageEnum } from './language.service' ;
2
+ import { Component } from '@angular/core' ;
3
+
4
+ @Component ( {
5
+ selector : 'app-greeting-component' ,
6
+ template : `
7
+ {{ getGreeting() }}!
8
+ `
9
+ } )
10
+ export class WithExternalServiceComponent {
11
+ constructor ( private langService : LanguageService ) { }
12
+
13
+ getGreeting ( ) {
14
+ if ( this . langService . currentLang === LanguageEnum . DE ) {
15
+ return 'Hallo' ;
16
+ } else if ( this . langService . currentLang === LanguageEnum . IT ) {
17
+ return 'Ciao' ;
18
+ } else {
19
+ return 'Hi' ;
20
+ }
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ import { Injectable } from '@angular/core' ;
2
+
3
+ export enum LanguageEnum {
4
+ DE = 'de' ,
5
+ IT = 'it' ,
6
+ EN = 'en'
7
+ }
8
+
9
+ @Injectable ( )
10
+ export class LanguageService {
11
+ private _currentLang = LanguageEnum . EN ;
12
+
13
+ get currentLang ( ) {
14
+ return this . _currentLang ;
15
+ }
16
+
17
+ setCurrentLanguage ( lang : LanguageEnum ) {
18
+ this . _currentLang = lang ;
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments