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(flow): catch error when beforeBack promise is rejected #7601

Merged
merged 4 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
46 changes: 46 additions & 0 deletions packages/calcite-components/src/components/flow/flow.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ describe("calcite-flow", () => {

await page.setContent(`<calcite-flow><calcite-flow-item></calcite-flow-item></calcite-flow>`);

expect(await page.findAll("calcite-flow-item")).toHaveLength(1);

await page.$eval(
"calcite-flow-item",
(elm: HTMLCalciteFlowItemElement) =>
Expand All @@ -114,6 +116,50 @@ describe("calcite-flow", () => {

expect(backValue).toBeDefined();
expect(mockCallBack).toHaveBeenCalledTimes(1);
expect(await page.findAll("calcite-flow-item")).toHaveLength(0);
});

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

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

await page.setContent(`<calcite-flow><calcite-flow-item></calcite-flow-item></calcite-flow>`);

await page.$eval(
"calcite-flow-item",
(elm: HTMLCalciteFlowItemElement) =>
(elm.beforeBack = (window as typeof window & Pick<typeof elm, "beforeBack">).beforeBack)
);

const flow = await page.find("calcite-flow");

await flow.callMethod("back");

expect(mockCallBack).toHaveBeenCalledTimes(1);
});

it("should not remove flow-item on rejected 'beforeBack' promise'", async () => {
const page = await newE2EPage();

await page.exposeFunction("beforeBack", () => Promise.reject());

await page.setContent(`<calcite-flow><calcite-flow-item></calcite-flow-item></calcite-flow>`);

expect(await page.findAll("calcite-flow-item")).toHaveLength(1);

await page.$eval(
"calcite-flow-item",
(elm: HTMLCalciteFlowItemElement) =>
(elm.beforeBack = (window as typeof window & Pick<typeof elm, "beforeBack">).beforeBack)
);

const flow = await page.find("calcite-flow");

await flow.callMethod("back");

expect(await page.findAll("calcite-flow-item")).toHaveLength(1);
});

it("frame advancing should add animation class", async () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/calcite-components/src/components/flow/flow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ export class Flow implements LoadableComponent {
? lastItem.beforeBack
: (): Promise<void> => Promise.resolve();

await beforeBack.call(lastItem);
try {
await beforeBack.call(lastItem);
} catch (_error) {
// back prevented
return;
}

lastItem.remove();

Expand Down