Skip to content

Commit

Permalink
feat(events): check returned event flags in DEBUG mode
Browse files Browse the repository at this point in the history
  • Loading branch information
localvoid committed May 23, 2018
1 parent f83f779 commit 2fc17db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/ivi-events/src/__tests__/dispatch.spec.ts
Expand Up @@ -186,3 +186,16 @@ describe("event flow", () => {
expect(order).toEqual([2]);
});
});

test(`returning invalid EventFlags should raise an exception`, () => {
const t1 = document.createElement("div");
const h1 = onClick(() => (10));

expect(() => {
dispatchEvent(
[{ target: t1, handlers: h1 }],
new SyntheticNativeEvent<MouseEvent>(0, t1, 0, new MouseEvent("click")),
true,
);
}).toThrowError(`Invalid`);
});
7 changes: 7 additions & 0 deletions packages/ivi-events/src/dispatch.ts
Expand Up @@ -22,6 +22,13 @@ function _dispatch(
event: SyntheticEvent,
): EventFlags {
const flags = (dispatch === void 0) ? handler.handler(event) : dispatch(handler, event);
if (DEBUG) {
if (flags !== void 0) {
if (flags & ~(EventFlags.PreventDefault | EventFlags.StopPropagation)) {
throw new Error(`Invalid event flags: ${flags}`);
}
}
}
return (flags === void 0) ? 0 : flags;
}

Expand Down

0 comments on commit 2fc17db

Please sign in to comment.