Skip to content

Commit

Permalink
[fix][menu-item]: Emit current state when checked change event fires (#…
Browse files Browse the repository at this point in the history
…33029)

Co-authored-by: John Kreitlow <[email protected]>
  • Loading branch information
eljefe223 and radium-v authored Oct 14, 2024
1 parent 46cdc69 commit bc620a1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fix: menu-item emit current state when checked change event fires",
"packageName": "@fluentui/web-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion packages/web-components/src/menu-item/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class MenuItem extends FASTElement {
toggleState(this.elementInternals, 'checked', checkableMenuItem ? next : false);

if (this.$fastController.isConnected) {
this.$emit('change');
this.$emit('change', next, { bubbles: true });
}
}

Expand Down
67 changes: 67 additions & 0 deletions packages/web-components/src/menu-list/menu-list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { once } from 'events';
import { test } from '@playwright/test';
import { expect, fixtureURL } from '../helpers.tests.js';
import { MenuItemRole } from '../menu-item/menu-item.options.js';
import { MenuItem } from '../menu-item/menu-item.js';

test.describe('Menu', () => {
test.beforeEach(async ({ page }) => {
Expand Down Expand Up @@ -521,4 +523,69 @@ test.describe('Menu', () => {
await expect(item).toHaveAttribute('data-indent', '2');
}
});

test.describe('`change` event', () => {
test('should emit `change` event when `checked` property changed', async ({ page }) => {
const element = page.locator('fluent-menu-list');
const menuItems = element.locator('fluent-menu-item');

await page.setContent(/* html */ `
<fluent-menu-list>
<fluent-menu-item role="menuitemradio">Menu Item 1</fluent-menu-item>
<fluent-menu-item>Menu item 2</fluent-menu-item>
<fluent-menu-item>Menu item 3</fluent-menu-item>
<fluent-menu-item>Menu item 4</fluent-menu-item>
</fluent-menu-list>
`);

const [wasChanged] = await Promise.all([
menuItems
.nth(0)
.evaluate(
node => new Promise(resolve => node.addEventListener('change', () => resolve(true), { once: true })),
),
menuItems.nth(0).evaluate((node: MenuItem) => {
node.checked = true;
}),
]);

expect(wasChanged).toEqual(true);
});

test('should emit change event when menu-item checked and unchecked', async ({ page }) => {
const element = page.locator('fluent-menu-list');
const menuItems = element.locator('fluent-menu-item');

await page.setContent(/* html */ `
<fluent-menu-list>
<fluent-menu-item role="menuitemradio">Menu Item 1</fluent-menu-item>
<fluent-menu-item checked role="menuitemradio">Menu item 2</fluent-menu-item>
<fluent-menu-item role="menuitemradio">Menu item 3</fluent-menu-item>
<fluent-menu-item role="menuitemradio">Menu item 4</fluent-menu-item>
</fluent-menu-list>
`);

let wasChanged = menuItems.nth(0).evaluate((node: MenuItem) => {
return new Promise(resolve => {
node.addEventListener('change', evt => {
resolve((evt as any).detail);
});
});
});

await menuItems.nth(0).click();
await expect(wasChanged).resolves.toBeTruthy();

wasChanged = menuItems.nth(0).evaluate((node: MenuItem) => {
return new Promise(resolve => {
node.addEventListener('change', evt => {
resolve((evt as any).detail);
});
});
});

await menuItems.nth(1).click();
await expect(wasChanged).resolves.toBeFalsy();
});
});
});

0 comments on commit bc620a1

Please sign in to comment.