|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {APP_ID, Component, destroyPlatform, ErrorHandler, PLATFORM_ID} from '@angular/core'; |
| 9 | +import { |
| 10 | + APP_ID, |
| 11 | + Component, |
| 12 | + destroyPlatform, |
| 13 | + Directive, |
| 14 | + ErrorHandler, |
| 15 | + HostListener, |
| 16 | + PLATFORM_ID, |
| 17 | +} from '@angular/core'; |
10 | 18 | import {withEventReplay} from '@angular/platform-browser'; |
11 | 19 |
|
12 | 20 | import {EventPhase} from '@angular/core/primitives/event-dispatch'; |
@@ -183,6 +191,128 @@ describe('event replay', () => { |
183 | 191 | expect(outerOnClickSpy).toHaveBeenCalledBefore(innerOnClickSpy); |
184 | 192 | }); |
185 | 193 |
|
| 194 | + describe('host bindings', () => { |
| 195 | + it('should not error when when binding to document:click on a container', async () => { |
| 196 | + const clickSpy = jasmine.createSpy(); |
| 197 | + @Directive({ |
| 198 | + selector: '[add-listener]', |
| 199 | + }) |
| 200 | + class AddGlobalListener { |
| 201 | + @HostListener('document:click') |
| 202 | + handleClick = clickSpy; |
| 203 | + } |
| 204 | + |
| 205 | + @Component({ |
| 206 | + selector: 'app', |
| 207 | + template: ` |
| 208 | + <ng-container add-listener> |
| 209 | + <button id="click-me">Click me!</button> |
| 210 | + </ng-container>`, |
| 211 | + imports: [AddGlobalListener], |
| 212 | + }) |
| 213 | + class AppComponent {} |
| 214 | + |
| 215 | + const appId = 'custom-app-id'; |
| 216 | + const providers = [{provide: APP_ID, useValue: appId}]; |
| 217 | + const hydrationFeatures = () => [withEventReplay()]; |
| 218 | + |
| 219 | + const html = await ssr(AppComponent, {envProviders: providers, hydrationFeatures}); |
| 220 | + const ssrContents = getAppContents(html); |
| 221 | + const doc = getDocument(); |
| 222 | + |
| 223 | + prepareEnvironment(doc, ssrContents); |
| 224 | + resetTViewsFor(AppComponent); |
| 225 | + const clickMe = doc.getElementById('click-me')!; |
| 226 | + clickMe.click(); |
| 227 | + await hydrate(doc, AppComponent, { |
| 228 | + envProviders: [{provide: PLATFORM_ID, useValue: 'browser'}, ...providers], |
| 229 | + hydrationFeatures, |
| 230 | + }); |
| 231 | + |
| 232 | + expect(clickSpy).not.toHaveBeenCalled(); |
| 233 | + }); |
| 234 | + |
| 235 | + it('should not error when when binding to window:click on a container', async () => { |
| 236 | + const clickSpy = jasmine.createSpy(); |
| 237 | + @Directive({ |
| 238 | + selector: '[add-listener]', |
| 239 | + }) |
| 240 | + class AddGlobalListener { |
| 241 | + @HostListener('window:click') |
| 242 | + handleClick = clickSpy; |
| 243 | + } |
| 244 | + |
| 245 | + @Component({ |
| 246 | + selector: 'app', |
| 247 | + template: ` |
| 248 | + <ng-container add-listener> |
| 249 | + <button id="click-me">Click me!</button> |
| 250 | + </ng-container>`, |
| 251 | + imports: [AddGlobalListener], |
| 252 | + }) |
| 253 | + class AppComponent {} |
| 254 | + |
| 255 | + const appId = 'custom-app-id'; |
| 256 | + const providers = [{provide: APP_ID, useValue: appId}]; |
| 257 | + const hydrationFeatures = () => [withEventReplay()]; |
| 258 | + |
| 259 | + const html = await ssr(AppComponent, {envProviders: providers, hydrationFeatures}); |
| 260 | + const ssrContents = getAppContents(html); |
| 261 | + const doc = getDocument(); |
| 262 | + |
| 263 | + prepareEnvironment(doc, ssrContents); |
| 264 | + resetTViewsFor(AppComponent); |
| 265 | + const clickMe = doc.getElementById('click-me')!; |
| 266 | + clickMe.click(); |
| 267 | + await hydrate(doc, AppComponent, { |
| 268 | + envProviders: [{provide: PLATFORM_ID, useValue: 'browser'}, ...providers], |
| 269 | + hydrationFeatures, |
| 270 | + }); |
| 271 | + |
| 272 | + expect(clickSpy).not.toHaveBeenCalled(); |
| 273 | + }); |
| 274 | + |
| 275 | + it('should not error when when binding to body:click on a container', async () => { |
| 276 | + const clickSpy = jasmine.createSpy(); |
| 277 | + @Directive({ |
| 278 | + selector: '[add-listener]', |
| 279 | + }) |
| 280 | + class AddGlobalListener { |
| 281 | + @HostListener('body:click') |
| 282 | + handleClick = clickSpy; |
| 283 | + } |
| 284 | + |
| 285 | + @Component({ |
| 286 | + selector: 'app', |
| 287 | + template: ` |
| 288 | + <ng-container add-listener> |
| 289 | + <button id="click-me">Click me!</button> |
| 290 | + </ng-container>`, |
| 291 | + imports: [AddGlobalListener], |
| 292 | + }) |
| 293 | + class AppComponent {} |
| 294 | + |
| 295 | + const appId = 'custom-app-id'; |
| 296 | + const providers = [{provide: APP_ID, useValue: appId}]; |
| 297 | + const hydrationFeatures = () => [withEventReplay()]; |
| 298 | + |
| 299 | + const html = await ssr(AppComponent, {envProviders: providers, hydrationFeatures}); |
| 300 | + const ssrContents = getAppContents(html); |
| 301 | + const doc = getDocument(); |
| 302 | + |
| 303 | + prepareEnvironment(doc, ssrContents); |
| 304 | + resetTViewsFor(AppComponent); |
| 305 | + const clickMe = doc.getElementById('click-me')!; |
| 306 | + clickMe.click(); |
| 307 | + await hydrate(doc, AppComponent, { |
| 308 | + envProviders: [{provide: PLATFORM_ID, useValue: 'browser'}, ...providers], |
| 309 | + hydrationFeatures, |
| 310 | + }); |
| 311 | + |
| 312 | + expect(clickSpy).not.toHaveBeenCalled(); |
| 313 | + }); |
| 314 | + }); |
| 315 | + |
186 | 316 | it('should remove jsaction attributes, but continue listening to events.', async () => { |
187 | 317 | @Component({ |
188 | 318 | standalone: true, |
|
0 commit comments