Skip to content

Commit

Permalink
fix(controller)!: fix label activation utility on slotted elements
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 511523410
  • Loading branch information
material-web-copybara authored and copybara-github committed Feb 22, 2023
1 parent c63a1d9 commit 8b58f98
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions controller/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export function dispatchActivationClick(element: HTMLElement) {
*/
export function isActivationClick(event: Event) {
// Event must start at the event target.
if (event.currentTarget !== event.target) {
return false;
}
// Event must not be retargeted from shadowRoot.
if (event.composedPath()[0] !== event.target) {
return false;
}
Expand Down
27 changes: 23 additions & 4 deletions controller/events_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('events', () => {

beforeEach(() => {
instance = document.createElement('div');
instance.attachShadow({mode: 'open'});
instance.attachShadow({mode: 'open'})
.append(document.createElement('slot'));
// To have event.target set correctly, the EventTarget instance must be
// attached to the DOM.
document.body.appendChild(instance);
Expand Down Expand Up @@ -113,20 +114,38 @@ describe('events', () => {
});

describe('isActivationClick()', () => {
it('should return true only if the event originated from target', () => {
it('returns true for click on listener', () => {
const listener = jasmine.createSpy('listener', isActivationClick);
listener.and.callThrough();
instance.addEventListener('click', listener);
instance.dispatchEvent(
new MouseEvent('click', {bubbles: true, composed: true}));
expect(listener).toHaveBeenCalledTimes(1);
expect(listener.calls.mostRecent().returnValue).toBe(true);
});

it('returns false for click on element listener shadowRoot', () => {
const listener = jasmine.createSpy('listener', isActivationClick);
listener.and.callThrough();
instance.addEventListener('click', listener);
const innerEl = document.createElement('div');
instance.shadowRoot!.append(innerEl);

innerEl.dispatchEvent(
new MouseEvent('click', {bubbles: true, composed: true}));
expect(listener).toHaveBeenCalledTimes(2);
expect(listener).toHaveBeenCalledTimes(1);
expect(listener.calls.mostRecent().returnValue).toBe(false);
});

it('returns false for click on element listener child', () => {
const listener = jasmine.createSpy('listener', isActivationClick);
listener.and.callThrough();
instance.addEventListener('click', listener);
const slottedEl = document.createElement('div');
instance.append(slottedEl);

slottedEl.dispatchEvent(
new MouseEvent('click', {bubbles: true, composed: true}));
expect(listener).toHaveBeenCalledTimes(1);
expect(listener.calls.mostRecent().returnValue).toBe(false);
});
});
Expand Down

0 comments on commit 8b58f98

Please sign in to comment.