1+ /* eslint-disable testing-library/no-container */
2+ /* eslint-disable testing-library/render-result-naming-convention */
13import { Directive , HostListener , ElementRef , Input , Output , EventEmitter , Component } from '@angular/core' ;
24
35import { render , fireEvent } from '../src/public_api' ;
46
57@Directive ( {
8+ // eslint-disable-next-line @angular-eslint/directive-selector
69 selector : '[onOff]' ,
710} )
811export class OnOffDirective {
@@ -21,6 +24,7 @@ export class OnOffDirective {
2124}
2225
2326@Directive ( {
27+ // eslint-disable-next-line @angular-eslint/directive-selector
2428 selector : '[update]' ,
2529} )
2630export class UpdateInputDirective {
@@ -32,27 +36,53 @@ export class UpdateInputDirective {
3236 constructor ( private el : ElementRef ) { }
3337}
3438
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+
3548test ( '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 ] ,
3851 } ) ;
3952
4053 expect ( component . container . querySelector ( '[onoff]' ) ) . toBeInTheDocument ( ) ;
4154} ) ;
4255
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 ( ) => {
4466 const component = await render ( OnOffDirective , {
4567 template : '<div onOff></div>' ,
4668 } ) ;
4769
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+
4878 fireEvent . click ( component . getByText ( 'init' ) ) ;
4979 fireEvent . click ( component . getByText ( 'on' ) ) ;
5080 fireEvent . click ( component . getByText ( 'off' ) ) ;
5181} ) ;
5282
5383test ( '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 ] ,
5686 } ) ;
5787
5888 fireEvent . click ( component . getByText ( 'init' ) ) ;
@@ -62,8 +92,8 @@ test('overrides input properties', async () => {
6292
6393test ( 'overrides input properties via a wrapper' , async ( ) => {
6494 // `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 ] ,
6797 componentProperties : {
6898 bar : 'hello' ,
6999 } ,
@@ -77,8 +107,8 @@ test('overrides input properties via a wrapper', async () => {
77107test ( 'overrides output properties' , async ( ) => {
78108 const clicked = jest . fn ( ) ;
79109
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 ] ,
82112 componentProperties : {
83113 clicked,
84114 } ,
@@ -93,8 +123,8 @@ test('overrides output properties', async () => {
93123
94124describe ( 'removeAngularAttributes' , ( ) => {
95125 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 ] ,
98128 removeAngularAttributes : true ,
99129 } ) ;
100130
@@ -103,8 +133,8 @@ describe('removeAngularAttributes', () => {
103133 } ) ;
104134
105135 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 ] ,
108138 } ) ;
109139
110140 expect ( document . querySelector ( '[ng-version]' ) ) . not . toBeNull ( ) ;
@@ -113,8 +143,8 @@ describe('removeAngularAttributes', () => {
113143} ) ;
114144
115145test ( '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 ] ,
118148 componentProperties : {
119149 value : 'value1' ,
120150 } ,
0 commit comments