1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix onBeforeRun iteration logic

Signed-off-by: Juho Heikka <juho.heikka@gmail.com>
This commit is contained in:
Juho Heikka 2021-10-07 18:58:57 +03:00
parent 912f239abb
commit 3e500abf3f
3 changed files with 16 additions and 16 deletions

View File

@ -22,23 +22,23 @@
import type { CatalogEntity } from "../catalog"; import type { CatalogEntity } from "../catalog";
export class CatalogRunEvent { export class CatalogRunEvent {
private _defaultPrevented: boolean; #defaultPrevented: boolean;
private _target: CatalogEntity; #target: CatalogEntity;
get defaultPrevented() { get defaultPrevented() {
return this._defaultPrevented; return this.#defaultPrevented;
} }
get target() { get target() {
return this._target; return this.#target;
} }
constructor({ target }: { target: CatalogEntity }) { constructor({ target }: { target: CatalogEntity }) {
this._defaultPrevented = false; this.#defaultPrevented = false;
this._target = target; this.#target = target;
} }
preventDefault() { preventDefault() {
this._defaultPrevented = true; this.#defaultPrevented = true;
} }
} }

View File

@ -206,11 +206,13 @@ export class CatalogEntityRegistry {
} catch (error) { } catch (error) {
logger.warn(`[CATALOG-ENTITY-REGISTRY]: entity ${entity.getId()} onBeforeRun threw an error`, error); logger.warn(`[CATALOG-ENTITY-REGISTRY]: entity ${entity.getId()} onBeforeRun threw an error`, error);
} }
if (runEvent.defaultPrevented) {
return false;
}
} }
const shouldContinue = !runEvent.defaultPrevented; return true;
return shouldContinue;
} }
/** /**

View File

@ -158,8 +158,6 @@ describe("<Catalog />", () => {
expect(onRun).toHaveBeenCalled(); expect(onRun).toHaveBeenCalled();
done(); done();
}, 500); }, 500);
return true;
} }
); );
@ -213,7 +211,7 @@ describe("<Catalog />", () => {
userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon"));
}); });
it("addOnBeforeRun throw an exception => onRun wont be triggered", (done) => { it("addOnBeforeRun throw an exception => onRun will be triggered", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -232,7 +230,7 @@ describe("<Catalog />", () => {
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
() => { () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).toHaveBeenCalled();
done(); done();
}, 500); }, 500);
@ -327,7 +325,7 @@ 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) => { it("addOnRunHook return a promise and reject => onRun will be triggered", (done) => {
const catalogCategoryRegistry = new CatalogCategoryRegistry(); const catalogCategoryRegistry = new CatalogCategoryRegistry();
const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry);
@ -346,7 +344,7 @@ describe("<Catalog />", () => {
catalogEntityRegistry.addOnBeforeRun( catalogEntityRegistry.addOnBeforeRun(
async () => { async () => {
setTimeout(() => { setTimeout(() => {
expect(onRun).not.toHaveBeenCalled(); expect(onRun).toHaveBeenCalled();
done(); done();
}, 500); }, 500);