/
directives.spec.ts
79 lines (73 loc) · 2 KB
/
directives.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
import { Directive } from '@angular/core';
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MockDirective } from 'ng-mocks';
import coreReflectDirective from 'ng-mocks/dist/lib/common/core.reflect.directive';
@Directive({
selector: 'directive',
})
export class DirectiveDefault {}
@Directive({
providers: [
{
multi: true,
provide: NG_VALUE_ACCESSOR,
useExisting: DirectiveValueAccessor,
},
],
selector: 'directive',
})
export class DirectiveValueAccessor {}
@Directive({
providers: [
{
multi: true,
provide: NG_VALIDATORS,
useExisting: DirectiveValidator,
},
],
selector: 'directive',
})
export class DirectiveValidator {}
// providers should be added to directives only in case if they were specified in the original directive.
describe('issue-145:directives', () => {
it('does not add NG_VALUE_ACCESSOR to directives', () => {
const mock = MockDirective(DirectiveDefault);
const { providers } = coreReflectDirective().resolve(mock);
expect(providers).toEqual([
{
provide: DirectiveDefault,
useExisting: jasmine.anything(),
},
]);
});
it('adds NG_VALUE_ACCESSOR to directives that provide it', () => {
const mock = MockDirective(DirectiveValueAccessor);
const { providers } = coreReflectDirective().resolve(mock);
expect(providers).toEqual([
{
provide: DirectiveValueAccessor,
useExisting: jasmine.anything(),
},
{
multi: true,
provide: NG_VALUE_ACCESSOR,
useFactory: jasmine.anything(),
},
]);
});
it('respects NG_VALIDATORS too', () => {
const mock = MockDirective(DirectiveValidator);
const { providers } = coreReflectDirective().resolve(mock);
expect(providers).toEqual([
{
provide: DirectiveValidator,
useExisting: jasmine.anything(),
},
{
multi: true,
provide: NG_VALIDATORS,
useFactory: jasmine.anything(),
},
]);
});
});