Skip to content

Commit

Permalink
fix(core/modal): fires 2 events on closing (#1489)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Leroux <[email protected]>
  • Loading branch information
matthiashader and danielleroux authored Sep 23, 2024
1 parent 24dbdee commit 6041b3d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/breezy-hotels-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siemens/ix': patch
---

fix(core/modal): duplicate event firing
9 changes: 9 additions & 0 deletions packages/core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export class Modal {
*/
@Method()
async dismissModal<T = any>(reason?: T) {
if (!this.modalVisible) {
return;
}

let allowDismiss = true;

if (this.beforeDismiss !== undefined) {
Expand Down Expand Up @@ -213,7 +217,12 @@ export class Modal {
*/
@Method()
async closeModal<T = any>(reason: T) {
if (!this.modalVisible) {
return;
}

this.slideOutModal(() => {
this.modalVisible = false;
this.dialog.close(
JSON.stringify(
{
Expand Down
56 changes: 55 additions & 1 deletion packages/core/src/components/modal/test/modal.ct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
*/
import { expect } from '@playwright/test';
import { test } from '@utils/test';
import { ModalInstance, showModal } from './../../utils/modal';

declare global {
interface Window {
showModal: any;
showModal: typeof showModal;
__counter: number;
}
}

Expand Down Expand Up @@ -149,3 +151,55 @@ test.describe('closeOnBackdropClick = true', () => {
await expect(page.locator('ix-modal dialog')).toBeVisible();
});
});

test('emits one event on close', async ({ mount, page }) => {
await mount(``);

await page.evaluate(() => {
return new Promise<void>((resolve) => {
const script = document.createElement('script');
script.type = 'module';
script.innerHTML = `
import * as ix from 'http://127.0.0.1:8080/www/build/index.esm.js';
window.showModal = ix.showModal;
`;
document.body.appendChild(script);
resolve();
});
});

await page.waitForTimeout(1000);

await page.evaluate(() => {
const elm = document.createElement('ix-modal');
elm.innerHTML = `
<ix-modal-header>Title</ix-modal-header>
<ix-modal-content>Content</ix-modal-header>
`;

window
.showModal({
content: elm,
// Disable animation to get the direct animation end callback
animation: false,
})
.then((instance: ModalInstance<unknown>) => {
instance.onDismiss.on(() => {
const counter = window.__counter;
if (counter) {
window.__counter = counter + 1;
} else {
window.__counter = 1;
}
});
});
});
const dialog = page.locator('ix-modal dialog');
await expect(dialog).toBeVisible();
const iconButton = page.locator('ix-icon-button');

await iconButton.click();
await expect(dialog).not.toBeVisible();

expect(await page.evaluate(() => window.__counter)).toBe(1);
});

0 comments on commit 6041b3d

Please sign in to comment.