-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(cdk/menu): Allow setting cdkMenuTriggerFor null #25818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b3dc759
450095e
4a77921
0ccdc02
3930736
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import {CdkContextMenuTrigger} from './context-menu-trigger'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the linter is complaining that this import is unused |
||
import {Component, ViewChildren, QueryList, ElementRef, ViewChild, Type} from '@angular/core'; | ||
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; | ||
import {By} from '@angular/platform-browser'; | ||
|
@@ -47,12 +48,36 @@ describe('MenuTrigger', () => { | |
expect(menuItemElement.getAttribute('aria-disabled')).toBe('true'); | ||
}); | ||
|
||
it('should set aria-haspopup to menu', () => { | ||
it('should set aria-haspopup based on whether a menu is assigned', () => { | ||
expect(menuItemElement.getAttribute('aria-haspopup')).toEqual('menu'); | ||
|
||
fixture.componentInstance.trigger.menuTemplateRef = null; | ||
fixture.detectChanges(); | ||
|
||
expect(menuItemElement.hasAttribute('aria-haspopup')).toBe(false); | ||
}); | ||
|
||
it('should have a menu', () => { | ||
it('should have a menu based on whether a menu is assigned', () => { | ||
expect(menuItem.hasMenu).toBeTrue(); | ||
|
||
fixture.componentInstance.trigger.menuTemplateRef = null; | ||
fixture.detectChanges(); | ||
|
||
expect(menuItem.hasMenu).toBeFalse(); | ||
}); | ||
|
||
it('should set aria-controls based on whether a menu is assigned', () => { | ||
expect(menuItemElement.hasAttribute('aria-controls')).toBeFalse(); | ||
}); | ||
|
||
it('should set aria-expanded based on whether a menu is assigned', () => { | ||
expect(menuItemElement.hasAttribute('aria-expanded')).toBeTrue(); | ||
expect(menuItemElement.getAttribute('aria-expanded')).toBe('false'); | ||
|
||
fixture.componentInstance.trigger.menuTemplateRef = null; | ||
fixture.detectChanges(); | ||
|
||
expect(menuItemElement.hasAttribute('aria-expanded')).toBeFalse(); | ||
}); | ||
}); | ||
|
||
|
@@ -469,6 +494,50 @@ describe('MenuTrigger', () => { | |
|
||
expect(document.querySelector('.test-menu')?.textContent).toBe('Hello!'); | ||
}); | ||
|
||
xdescribe('null triggerFor', () => { | ||
let fixture: ComponentFixture<TriggerWithNullValue>; | ||
|
||
let nativeTrigger: HTMLElement; | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [CdkMenuModule], | ||
declarations: [TriggerWithNullValue], | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TriggerWithNullValue); | ||
nativeTrigger = fixture.componentInstance.nativeTrigger.nativeElement | ||
}); | ||
|
||
it('should not set aria-haspopup', () => { | ||
expect(nativeTrigger.hasAttribute('aria-haspopup')).toBeFalse(); | ||
}); | ||
|
||
it('should not set aria-controls', () => { | ||
expect(nativeTrigger.hasAttribute('aria-controls')).toBeFalse(); | ||
}); | ||
|
||
it('should not toggle the menu on trigger', () => { | ||
expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); | ||
|
||
nativeTrigger.click(); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); | ||
}); | ||
|
||
it('should not toggle the menu on keyboard events', () => { | ||
expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); | ||
|
||
dispatchKeyboardEvent(nativeTrigger, 'keydown', SPACE); | ||
fixture.detectChanges(); | ||
|
||
expect(fixture.componentInstance.trigger.isOpen()).toBeFalse(); | ||
}); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
|
@@ -477,7 +546,10 @@ describe('MenuTrigger', () => { | |
<ng-template #noop><div cdkMenu></div></ng-template> | ||
`, | ||
}) | ||
class TriggerForEmptyMenu {} | ||
class TriggerForEmptyMenu { | ||
@ViewChild(CdkMenuTrigger) trigger: CdkMenuTrigger; | ||
@ViewChild(CdkMenuTrigger, { read: ElementRef }) nativeTrigger: ElementRef; | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
|
@@ -602,3 +674,20 @@ class StandaloneTriggerWithInlineMenu { | |
class TriggerWithData { | ||
menuData: unknown; | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<button [cdkMenuTriggerFor]="null">First</button> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that we need an entirely separate fixture where it is set to null. Instead we should adapt an existing fixture so we can also test that the DOM is updated on dynamic changes. See how it's done for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's good idea, I updated tests. |
||
<button [cdkContextMenuTriggerFor]="null">First</button> | ||
`, | ||
}) | ||
class TriggerWithNullValue { | ||
@ViewChild(CdkMenuTrigger) | ||
trigger: CdkMenuTrigger; | ||
|
||
@ViewChild(CdkMenuTrigger, {read: ElementRef}) | ||
nativeTrigger: ElementRef<HTMLElement>; | ||
|
||
@ViewChild(CdkContextMenuTrigger, {read: ElementRef}) | ||
contextMenuNativeTrigger: ElementRef<HTMLElement>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this can be simplified to
return this._menuTrigger?.menuTemplateRef != null
.