Skip to content

Commit d1ab20f

Browse files
committed
feat(components): simple cmp test with dep on service
1 parent 9ffed32 commit d1ab20f

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ All the scenarios are listed here below and nicely linked to the source file.
3434
_Test dynamic CSS styles using custom Jasmine matchers._
3535
* [mocking nested components](./src/app/components/nested.component.spec.ts)
3636
_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_
3939
1. [**Testing Services**](./src/app/services)
4040
* [Simple stateless function](./src/app/services/greeting.service.spec.ts)
4141
_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.
4545
_Learn how to mock external dependencies, such as use the `MockBackend` provided by Angular to respond to http calls._
4646
1. [**Testing Pipes**](./src/app/pipes)
4747
* [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_
4850
1. [**Custom Matchers and Utilities**](./src/app/utils)
4951
* [Create your own custom Jasmine matchers](./src/app/utils/custom-matchers.ts)
5052

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

0 commit comments

Comments
 (0)