|
| 1 | +import {CdkContextMenuTrigger} from './context-menu-trigger'; |
1 | 2 | import {Component, ViewChildren, QueryList, ElementRef, ViewChild, Type} from '@angular/core';
|
2 | 3 | import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
|
3 | 4 | import {By} from '@angular/platform-browser';
|
@@ -469,6 +470,59 @@ describe('MenuTrigger', () => {
|
469 | 470 |
|
470 | 471 | expect(document.querySelector('.test-menu')?.textContent).toBe('Hello!');
|
471 | 472 | });
|
| 473 | + |
| 474 | + describe('null triggerFor', () => { |
| 475 | + let fixture: ComponentFixture<TriggerWithNullValue>; |
| 476 | + |
| 477 | + let nativeTrigger: HTMLElement; |
| 478 | + let contextNativeTrigger: HTMLElement; |
| 479 | + |
| 480 | + const grabElementsForTesting = () => { |
| 481 | + nativeTrigger = fixture.componentInstance.nativeTrigger.nativeElement; |
| 482 | + }; |
| 483 | + |
| 484 | + /** run change detection and, extract and set the rendered elements */ |
| 485 | + const detectChanges = () => { |
| 486 | + fixture.detectChanges(); |
| 487 | + grabElementsForTesting(); |
| 488 | + }; |
| 489 | + |
| 490 | + beforeEach(waitForAsync(() => { |
| 491 | + TestBed.configureTestingModule({ |
| 492 | + imports: [CdkMenuModule], |
| 493 | + declarations: [TriggerWithNullValue], |
| 494 | + }).compileComponents(); |
| 495 | + })); |
| 496 | + |
| 497 | + beforeEach(() => { |
| 498 | + fixture = TestBed.createComponent(TriggerWithNullValue); |
| 499 | + detectChanges(); |
| 500 | + }); |
| 501 | + |
| 502 | + it('should not set aria-haspopup', () => { |
| 503 | + expect(nativeTrigger.hasAttribute('aria-haspopup')).toBeFalse(); |
| 504 | + }); |
| 505 | + |
| 506 | + it('should not set aria-controls', () => { |
| 507 | + expect(nativeTrigger.hasAttribute('aria-controls')).toBeFalse(); |
| 508 | + }); |
| 509 | + |
| 510 | + it('should not toggle the menu on trigger', () => { |
| 511 | + expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); |
| 512 | + |
| 513 | + nativeTrigger.click(); |
| 514 | + detectChanges(); |
| 515 | + expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); |
| 516 | + }); |
| 517 | + |
| 518 | + it('should not toggle the menu on keyboard events', () => { |
| 519 | + expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); |
| 520 | + |
| 521 | + dispatchKeyboardEvent(nativeTrigger, 'keydown', SPACE); |
| 522 | + detectChanges(); |
| 523 | + expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); |
| 524 | + }); |
| 525 | + }); |
472 | 526 | });
|
473 | 527 |
|
474 | 528 | @Component({
|
@@ -602,3 +656,20 @@ class StandaloneTriggerWithInlineMenu {
|
602 | 656 | class TriggerWithData {
|
603 | 657 | menuData: unknown;
|
604 | 658 | }
|
| 659 | + |
| 660 | +@Component({ |
| 661 | + template: ` |
| 662 | + <button [cdkMenuTriggerFor]="null">First</button> |
| 663 | + <button [cdkContextMenuTriggerFor]="null">First</button> |
| 664 | + `, |
| 665 | +}) |
| 666 | +class TriggerWithNullValue { |
| 667 | + @ViewChild(CdkMenuTrigger) |
| 668 | + trigger: CdkMenuTrigger; |
| 669 | + |
| 670 | + @ViewChild(CdkMenuTrigger, {read: ElementRef}) |
| 671 | + nativeTrigger: ElementRef<HTMLElement>; |
| 672 | + |
| 673 | + @ViewChild(CdkContextMenuTrigger, {read: ElementRef}) |
| 674 | + contextMenuNativeTrigger: ElementRef<HTMLElement>; |
| 675 | +} |
0 commit comments