Skip to content
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

fix(modal): catch error when beforeClose promise is rejected #7600

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/calcite-components/src/components/modal/modal.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,48 @@ it("calls the beforeClose method prior to closing via attribute", async () => {
expect(await modal.getProperty("opened")).toBe(false);
});

it("should handle rejected 'beforeClose' promise'", async () => {
const page = await newE2EPage();

const mockCallBack = jest.fn().mockReturnValue(() => Promise.reject());
await page.exposeFunction("beforeClose", mockCallBack);

await page.setContent(`<calcite-modal open></calcite-modal>`);

await page.$eval(
"calcite-modal",
(elm: HTMLCalciteModalElement) =>
(elm.beforeClose = (window as typeof window & Pick<typeof elm, "beforeClose">).beforeClose)
);

const modal = await page.find("calcite-modal");
modal.setProperty("open", false);
await page.waitForChanges();

expect(mockCallBack).toHaveBeenCalledTimes(1);
});
Copy link
Member

@jcfranco jcfranco Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an assertion here to catch that open remains true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

open would actually still be true here but opened would be false.

Should we set open to false in the catch? @jcfranco

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would just update the state and not affect transitions, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think restoring open makes sense since we're blocking changes to it.


it("should remain open with rejected 'beforeClose' promise'", async () => {
const page = await newE2EPage();

await page.exposeFunction("beforeClose", () => Promise.reject());
await page.setContent(`<calcite-modal open></calcite-modal>`);

await page.$eval(
"calcite-modal",
(elm: HTMLCalciteModalElement) =>
(elm.beforeClose = (window as typeof window & Pick<typeof elm, "beforeClose">).beforeClose)
);

const modal = await page.find("calcite-modal");
modal.setProperty("open", false);
await page.waitForChanges();

expect(await modal.getProperty("open")).toBe(true);
expect(await modal.getProperty("opened")).toBe(true);
expect(modal.getAttribute("open")).toBe(""); // Makes sure attribute is added back
});

describe("opening and closing behavior", () => {
it("opens and closes", async () => {
const page = await newE2EPage();
Expand Down
16 changes: 15 additions & 1 deletion packages/calcite-components/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ export class Modal

@Watch("open")
async toggleModal(value: boolean): Promise<void> {
if (this.ignoreOpenChange) {
return;
}

onToggleOpenCloseComponent(this);
if (value) {
this.transitionEl?.classList.add(CSS.openingIdle);
Expand Down Expand Up @@ -557,7 +561,17 @@ export class Modal
}

if (this.beforeClose) {
await this.beforeClose(this.el);
try {
await this.beforeClose(this.el);
} catch (_error) {
// close prevented
requestAnimationFrame(() => {
this.ignoreOpenChange = true;
this.open = true;
this.ignoreOpenChange = false;
});
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a // close prevented or similar one-liner comment to the catch block about this intentionally returning because closing was canceled?

}
}

this.ignoreOpenChange = true;
Expand Down