1
0
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:
Hung-Han (Henry) Chen 2021-10-05 09:39:21 +03:00
parent 2ba9315348
commit 24f5f9950a
No known key found for this signature in database
GPG Key ID: 54B44603D251B788
2 changed files with 107 additions and 5 deletions

View File

@ -33,6 +33,8 @@ import { toJS } from "mobx";
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 {
constructor(public entity: T) {}
@ -112,11 +114,23 @@ export class CatalogEntityItem<T extends CatalogEntity> implements ItemObject {
}
if (typeof onRunHook === "function") {
// if onRunHook() returns a Promise, we wait for it to resolve
// if not, just take whatever it returns
Promise.resolve(onRunHook(toJS(this.entity))).then((shouldRun) => {
if (shouldRun) this.entity.onRun(ctx);
});
let shouldRun;
try {
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);
}
}
}

View File

@ -229,6 +229,50 @@ describe("<Catalog />", () => {
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) => {
let onRunTriggered = false;
const onRun = () => {
@ -322,4 +366,48 @@ describe("<Catalog />", () => {
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"));
});
});