Skip to content

Commit bb3a8e8

Browse files
committed
maint(core events tests): Add jQuery vs native JavaScript events tests.
Add test showing that jQuery catches native events, but native listeners do not catch jQuery events. In the mid-term we want to switch to native-only to get rid of this difference.
1 parent 038bb79 commit bb3a8e8

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/core/events.test.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import events from "./events";
22
import utils from "./utils";
33

44
describe("core.events tests", () => {
5-
describe("add / remove event listener", () => {
5+
describe("1 - add / remove event listener", () => {
66
const _el = {
77
event_list: [],
88
addEventListener(event_type, cb) {
@@ -49,7 +49,7 @@ describe("core.events tests", () => {
4949
});
5050
});
5151

52-
describe("event factories", () => {
52+
describe("2 - event factories", () => {
5353
let catched;
5454
let outer;
5555
let inner;
@@ -102,4 +102,41 @@ describe("core.events tests", () => {
102102
expect(catched).toBe("outer");
103103
});
104104
});
105+
106+
describe("3 - jQuery vs native", () => {
107+
// These tests show an annoying difference between jQuery and native
108+
// JavaScript events. jQuery catches native JavaScript events, which is
109+
// good. But events triggered by jQuery are not compatibel with native
110+
// JavaScript events and are not catched by native JavaScript event
111+
// handlers.
112+
// We want to get rid of jQuery events in the mid-term.
113+
114+
it("jQuery catches native", async () => {
115+
let catched = false;
116+
const $ = (await import("jquery")).default;
117+
const el = document.createElement("input");
118+
el.setAttribute("type", "text");
119+
el.setAttribute("name", "inp");
120+
$(el).on("input", () => {
121+
catched = true;
122+
});
123+
el.dispatchEvent(events.input_event());
124+
await utils.timeout(1);
125+
expect(catched).toBe(true);
126+
});
127+
128+
it("native does not catch jQuery", async () => {
129+
let catched = false;
130+
const $ = (await import("jquery")).default;
131+
const el = document.createElement("input");
132+
el.setAttribute("type", "text");
133+
el.setAttribute("name", "inp");
134+
el.addEventListener("input", () => {
135+
catched = true;
136+
});
137+
$(el).trigger("input");
138+
await utils.timeout(1);
139+
expect(catched).toBe(false);
140+
});
141+
});
105142
});

0 commit comments

Comments
 (0)