1
+ /* eslint-disable testing-library/no-container */
2
+ /* eslint-disable testing-library/render-result-naming-convention */
1
3
import { Directive , HostListener , ElementRef , Input , Output , EventEmitter , Component } from '@angular/core' ;
2
4
3
5
import { render , fireEvent } from '../src/public_api' ;
4
6
5
7
@Directive ( {
8
+ // eslint-disable-next-line @angular-eslint/directive-selector
6
9
selector : '[onOff]' ,
7
10
} )
8
11
export class OnOffDirective {
@@ -21,6 +24,7 @@ export class OnOffDirective {
21
24
}
22
25
23
26
@Directive ( {
27
+ // eslint-disable-next-line @angular-eslint/directive-selector
24
28
selector : '[update]' ,
25
29
} )
26
30
export class UpdateInputDirective {
@@ -32,27 +36,53 @@ export class UpdateInputDirective {
32
36
constructor ( private el : ElementRef ) { }
33
37
}
34
38
39
+ @Component ( {
40
+ // eslint-disable-next-line @angular-eslint/component-selector
41
+ selector : 'greeting' ,
42
+ template : 'Hello {{ name }}!' ,
43
+ } )
44
+ export class GreetingComponent {
45
+ @Input ( ) name = 'World' ;
46
+ }
47
+
35
48
test ( 'the directive renders' , async ( ) => {
36
- const component = await render ( OnOffDirective , {
37
- template : '<div onOff></div>' ,
49
+ const component = await render ( '<div onOff></div>' , {
50
+ declarations : [ OnOffDirective ] ,
38
51
} ) ;
39
52
40
53
expect ( component . container . querySelector ( '[onoff]' ) ) . toBeInTheDocument ( ) ;
41
54
} ) ;
42
55
43
- test ( 'uses the default props' , async ( ) => {
56
+ test ( 'the component renders' , async ( ) => {
57
+ const component = await render ( '<greeting name="Angular"></greeting>' , {
58
+ declarations : [ GreetingComponent ] ,
59
+ } ) ;
60
+
61
+ expect ( component . container . querySelector ( 'greeting' ) ) . toBeInTheDocument ( ) ;
62
+ expect ( component . getByText ( 'Hello Angular!' ) ) ;
63
+ } ) ;
64
+
65
+ test ( 'the directive renders (compatibility with the deprecated signature)' , async ( ) => {
44
66
const component = await render ( OnOffDirective , {
45
67
template : '<div onOff></div>' ,
46
68
} ) ;
47
69
70
+ expect ( component . container . querySelector ( '[onoff]' ) ) . toBeInTheDocument ( ) ;
71
+ } ) ;
72
+
73
+ test . only ( 'uses the default props' , async ( ) => {
74
+ const component = await render ( '<div onOff></div>' , {
75
+ declarations : [ OnOffDirective ] ,
76
+ } ) ;
77
+
48
78
fireEvent . click ( component . getByText ( 'init' ) ) ;
49
79
fireEvent . click ( component . getByText ( 'on' ) ) ;
50
80
fireEvent . click ( component . getByText ( 'off' ) ) ;
51
81
} ) ;
52
82
53
83
test ( 'overrides input properties' , async ( ) => {
54
- const component = await render ( OnOffDirective , {
55
- template : '<div onOff on="hello"></div>' ,
84
+ const component = await render ( '<div onOff on="hello"></div>' , {
85
+ declarations : [ OnOffDirective ] ,
56
86
} ) ;
57
87
58
88
fireEvent . click ( component . getByText ( 'init' ) ) ;
@@ -62,8 +92,8 @@ test('overrides input properties', async () => {
62
92
63
93
test ( 'overrides input properties via a wrapper' , async ( ) => {
64
94
// `bar` will be set as a property on the wrapper component, the property will be used to pass to the directive
65
- const component = await render ( OnOffDirective , {
66
- template : '<div onOff [on]="bar"></div>' ,
95
+ const component = await render ( '<div onOff [on]="bar"></div>' , {
96
+ declarations : [ OnOffDirective ] ,
67
97
componentProperties : {
68
98
bar : 'hello' ,
69
99
} ,
@@ -77,8 +107,8 @@ test('overrides input properties via a wrapper', async () => {
77
107
test ( 'overrides output properties' , async ( ) => {
78
108
const clicked = jest . fn ( ) ;
79
109
80
- const component = await render ( OnOffDirective , {
81
- template : '<div onOff (clicked)="clicked($event)"></div>' ,
110
+ const component = await render ( '<div onOff (clicked)="clicked($event)"></div>' , {
111
+ declarations : [ OnOffDirective ] ,
82
112
componentProperties : {
83
113
clicked,
84
114
} ,
@@ -93,8 +123,8 @@ test('overrides output properties', async () => {
93
123
94
124
describe ( 'removeAngularAttributes' , ( ) => {
95
125
test ( 'should remove angular attributes' , async ( ) => {
96
- await render ( OnOffDirective , {
97
- template : '<div onOff (clicked)="clicked($event)"></div>' ,
126
+ await render ( '<div onOff (clicked)="clicked($event)"></div>' , {
127
+ declarations : [ OnOffDirective ] ,
98
128
removeAngularAttributes : true ,
99
129
} ) ;
100
130
@@ -103,8 +133,8 @@ describe('removeAngularAttributes', () => {
103
133
} ) ;
104
134
105
135
test ( 'is disabled by default' , async ( ) => {
106
- await render ( OnOffDirective , {
107
- template : '<div onOff (clicked)="clicked($event)"></div>' ,
136
+ await render ( '<div onOff (clicked)="clicked($event)"></div>' , {
137
+ declarations : [ OnOffDirective ] ,
108
138
} ) ;
109
139
110
140
expect ( document . querySelector ( '[ng-version]' ) ) . not . toBeNull ( ) ;
@@ -113,8 +143,8 @@ describe('removeAngularAttributes', () => {
113
143
} ) ;
114
144
115
145
test ( 'updates properties and invokes change detection' , async ( ) => {
116
- const component = await render ( UpdateInputDirective , {
117
- template : '<div [update]="value" ></div>' ,
146
+ const component = await render ( '<div [update]="value" ></div>' , {
147
+ declarations : [ UpdateInputDirective ] ,
118
148
componentProperties : {
119
149
value : 'value1' ,
120
150
} ,
0 commit comments