-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathcounter.component.spectator.spec.ts
66 lines (52 loc) · 1.94 KB
/
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
import { byTestId, createComponentFactory, Spectator } from '@ngneat/spectator';
import { take, toArray } from 'rxjs/operators';
import { CounterComponent } from './counter.component';
const startCount = 123;
const newCount = 456;
describe('CounterComponent with spectator', () => {
let spectator: Spectator<CounterComponent>;
function expectCount(count: number): void {
expect(spectator.query(byTestId('count'))).toHaveText(String(count));
}
const createComponent = createComponentFactory({
component: CounterComponent,
shallow: true,
});
beforeEach(() => {
spectator = createComponent({ props: { startCount } });
});
it('shows the start count', () => {
expectCount(startCount);
});
it('increments the count', () => {
spectator.click(byTestId('increment-button'));
expectCount(startCount + 1);
});
it('decrements the count', () => {
spectator.click(byTestId('decrement-button'));
expectCount(startCount - 1);
});
it('resets the count', () => {
spectator.click(byTestId('decrement-button'));
spectator.typeInElement(String(newCount), byTestId('reset-input'));
spectator.click(byTestId('reset-button'));
expectCount(newCount);
});
it('does not reset if the value is not a number', () => {
const value = 'not a number';
spectator.typeInElement(String(value), byTestId('reset-input'));
spectator.click(byTestId('reset-button'));
expectCount(startCount);
});
it('emits countChange events', () => {
let actualCounts: number[] | undefined;
spectator.component.countChange.pipe(take(3), toArray()).subscribe((counts) => {
actualCounts = counts;
});
spectator.click(byTestId('increment-button'));
spectator.click(byTestId('decrement-button'));
spectator.typeInElement(String(newCount), byTestId('reset-input'));
spectator.click(byTestId('reset-button'));
expect(actualCounts).toEqual([startCount + 1, startCount, newCount]);
});
});