/
test.spec.ts
81 lines (71 loc) · 2.24 KB
/
test.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
// tslint:disable strict-type-predicates
import { Component, forwardRef } from '@angular/core';
import {
ControlValueAccessor,
FormControl,
NG_VALUE_ACCESSOR,
ReactiveFormsModule,
} from '@angular/forms';
import {
MockBuilder,
MockInstance,
MockRender,
ngMocks,
} from 'ng-mocks';
@Component({
providers: [
{
multi: true,
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DependencyComponent),
},
],
selector: 'app-child',
template: `dependency`,
})
class DependencyComponent implements ControlValueAccessor {
public registerOnChange = (fn: any): void => fn;
public registerOnTouched = (fn: any): void => fn;
public writeValue = (obj: any): void => obj;
}
@Component({
selector: 'tested',
template: ` <app-child [formControl]="formControl"></app-child> `,
})
class TestedComponent {
public readonly formControl = new FormControl();
}
describe('MockReactiveForms', () => {
beforeEach(() => {
return MockBuilder(TestedComponent)
.mock(DependencyComponent)
.keep(ReactiveFormsModule);
});
it('sends the correct value to the mock form component', () => {
// That is our spy on writeValue calls.
// With auto spy this code is not needed.
const writeValue =
typeof jest === 'undefined'
? jasmine.createSpy('writeValue')
: jest.fn();
// in case of jest
// const writeValue = jest.fn();
// Because of early calls of writeValue, we need to install
// the spy via MockInstance before the render.
MockInstance(DependencyComponent, 'writeValue', writeValue);
const fixture = MockRender(TestedComponent);
const component = fixture.point.componentInstance;
// During initialization it should be called
// with null.
expect(writeValue).toHaveBeenCalledWith(null);
// Let's find the form control element
// and simulate its change, like a user does it.
const mockControlEl = ngMocks.find(DependencyComponent);
ngMocks.change(mockControlEl, 'foo');
expect(component.formControl.value).toBe('foo');
// Let's check that change on existing formControl
// causes calls of `writeValue` on the mock component.
component.formControl.setValue('bar');
expect(writeValue).toHaveBeenCalledWith('bar');
});
});