-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathservice-counter.component.spectator.spec.ts
108 lines (82 loc) · 3.01 KB
/
service-counter.component.spectator.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { TestBed } from '@angular/core/testing';
import {
byTestId,
createComponentFactory,
mockProvider,
Spectator,
} from '@ngneat/spectator';
import { of } from 'rxjs';
import { CounterService } from '../../services/counter.service';
import { ServiceCounterComponent } from './service-counter.component';
const newCount = 123;
describe('ServiceCounterComponent: integration test with spectator', () => {
let spectator: Spectator<ServiceCounterComponent>;
function expectCount(count: number): void {
expect(spectator.query(byTestId('count'))).toHaveText(String(count));
}
const createComponent = createComponentFactory({
component: ServiceCounterComponent,
providers: [CounterService],
});
beforeEach(() => {
spectator = createComponent();
});
it('shows the start count', () => {
expectCount(0);
});
it('increments the count', () => {
spectator.click(byTestId('increment-button'));
expectCount(1);
});
it('decrements the count', () => {
spectator.click(byTestId('decrement-button'));
expectCount(-1);
});
it('resets the count', () => {
spectator.typeInElement(String(newCount), byTestId('reset-input'));
spectator.click(byTestId('reset-button'));
expectCount(newCount);
});
});
describe('ServiceCounterComponent: unit test with spectator', () => {
let spectator: Spectator<ServiceCounterComponent>;
function expectCount(count: number): void {
expect(spectator.query(byTestId('count'))).toHaveText(String(count));
}
let counterService: Pick<CounterService, keyof CounterService>;
const createComponent = createComponentFactory({
component: ServiceCounterComponent,
providers: [mockProvider(CounterService)],
});
beforeEach(() => {
counterService = TestBed.inject(CounterService);
(counterService as jasmine.SpyObj<CounterService>).getCount.and.returnValue(of(0));
spectator = createComponent();
});
it('shows the start count', () => {
expectCount(0);
expect(counterService.getCount).toHaveBeenCalled();
});
it('increments the count', () => {
spectator.click(byTestId('increment-button'));
// No count expectation here since the fake method does not do anything
expect(counterService.increment).toHaveBeenCalled();
});
it('decrements the count', () => {
spectator.click(byTestId('decrement-button'));
// No count expectation here since the fake method does not do anything
expect(counterService.decrement).toHaveBeenCalled();
});
it('resets the count', () => {
spectator.typeInElement(String(newCount), byTestId('reset-input'));
spectator.click(byTestId('reset-button'));
// No count expectation here since the fake method does not do anything
expect(counterService.reset).toHaveBeenCalled();
});
it('does not reset if the value is not a number', () => {
const value = 'not a number';
spectator.typeInElement(value, byTestId('reset-input'));
spectator.click(byTestId('reset-button'));
expect(counterService.reset).not.toHaveBeenCalled();
});
});