mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Catch the cases where onRunHook throws or reject
Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
parent
2ba9315348
commit
24f5f9950a
@ -33,6 +33,8 @@ import { toJS } from "mobx";
|
|||||||
|
|
||||||
const css = makeCss(styles);
|
const css = makeCss(styles);
|
||||||
|
|
||||||
|
const isPromise = (obj: any): obj is Promise<any> => (obj?.then && typeof obj?.then === "function") ? true: false;
|
||||||
|
|
||||||
export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
|
export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
|
||||||
constructor(public entity: T) {}
|
constructor(public entity: T) {}
|
||||||
|
|
||||||
@ -112,11 +114,23 @@ export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof onRunHook === "function") {
|
if (typeof onRunHook === "function") {
|
||||||
// if onRunHook() returns a Promise, we wait for it to resolve
|
let shouldRun;
|
||||||
// if not, just take whatever it returns
|
|
||||||
Promise.resolve(onRunHook(toJS(this.entity))).then((shouldRun) => {
|
try {
|
||||||
if (shouldRun) this.entity.onRun(ctx);
|
shouldRun = onRunHook(toJS(this.entity));
|
||||||
});
|
} catch (error) {
|
||||||
|
if (process?.env?.NODE_ENV !== "test") console.warn(`[CATALOG-ENTITY-ITEM] onRunHook of entity.metadata.uid ${this.entity.metadata.uid} throw an exception, stop before onRun`, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPromise(shouldRun)) {
|
||||||
|
Promise.resolve(shouldRun).then((shouldRun) => {
|
||||||
|
if (shouldRun) this.entity.onRun(ctx);
|
||||||
|
}).catch((error) => {
|
||||||
|
if (process?.env?.NODE_ENV !== "test") console.warn(`[CATALOG-ENTITY-ITEM] onRunHook of entity.metadata.uid ${this.entity.metadata.uid} rejects, stop before onRun`, error);
|
||||||
|
});
|
||||||
|
} else if (shouldRun) {
|
||||||
|
this.entity.onRun(ctx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -229,6 +229,50 @@ describe("<Catalog />", () => {
|
|||||||
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
|
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("addOnRunHook throw an exception => onRun wont be triggered", (done) => {
|
||||||
|
const onRun = jest.fn();
|
||||||
|
const catalogEntityItem = new CatalogEntityItem({
|
||||||
|
...catalogEntity,
|
||||||
|
...catalogEntityItemMethods,
|
||||||
|
onRun,
|
||||||
|
});
|
||||||
|
|
||||||
|
const catalogCategoryRegistry = new CatalogCategoryRegistry();
|
||||||
|
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
|
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
|
||||||
|
|
||||||
|
// mock as if there is a selected item > the detail panel opens
|
||||||
|
jest
|
||||||
|
.spyOn(catalogEntityStore, "selectedItem", "get")
|
||||||
|
.mockImplementation(() => {
|
||||||
|
return catalogEntityItem;
|
||||||
|
});
|
||||||
|
|
||||||
|
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||||
|
catalogEntityUid,
|
||||||
|
() => {
|
||||||
|
onRunDisposer?.();
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(onRun).not.toHaveBeenCalled();
|
||||||
|
done();
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
throw new Error("error!");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
render(
|
||||||
|
<Catalog
|
||||||
|
history={history}
|
||||||
|
location={mockLocation}
|
||||||
|
match={mockMatch}
|
||||||
|
catalogEntityStore={catalogEntityStore}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
|
||||||
|
});
|
||||||
|
|
||||||
it("addOnRunHook return a promise and resolve true => onRun()", (done) => {
|
it("addOnRunHook return a promise and resolve true => onRun()", (done) => {
|
||||||
let onRunTriggered = false;
|
let onRunTriggered = false;
|
||||||
const onRun = () => {
|
const onRun = () => {
|
||||||
@ -322,4 +366,48 @@ describe("<Catalog />", () => {
|
|||||||
|
|
||||||
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
|
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("addOnRunHook return a promise and reject => onRun wont be triggered", (done) => {
|
||||||
|
const onRun = jest.fn();
|
||||||
|
const catalogEntityItem = new CatalogEntityItem({
|
||||||
|
...catalogEntity,
|
||||||
|
...catalogEntityItemMethods,
|
||||||
|
onRun,
|
||||||
|
});
|
||||||
|
|
||||||
|
const catalogCategoryRegistry = new CatalogCategoryRegistry();
|
||||||
|
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
|
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
|
||||||
|
|
||||||
|
// mock as if there is a selected item > the detail panel opens
|
||||||
|
jest
|
||||||
|
.spyOn(catalogEntityStore, "selectedItem", "get")
|
||||||
|
.mockImplementation(() => {
|
||||||
|
return catalogEntityItem;
|
||||||
|
});
|
||||||
|
|
||||||
|
const onRunDisposer = catalogEntityRegistry.addOnRunHook(
|
||||||
|
catalogEntityUid,
|
||||||
|
async () => {
|
||||||
|
onRunDisposer?.();
|
||||||
|
setTimeout(() => {
|
||||||
|
expect(onRun).not.toHaveBeenCalled();
|
||||||
|
done();
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
throw new Error("rejection!");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
render(
|
||||||
|
<Catalog
|
||||||
|
history={history}
|
||||||
|
location={mockLocation}
|
||||||
|
match={mockMatch}
|
||||||
|
catalogEntityStore={catalogEntityStore}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user