Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 621f01c

Browse files
authored
Merge pull request #23 from skatejs/22
#22 - Ensure event handlers aren't duplicated
2 parents e52dae7 + da1b31f commit 621f01c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ const defaults = {
99
function syncEvent(node, eventName, newEventHandler) {
1010
const eventNameLc = eventName.toLowerCase();
1111
const eventStore = node.__events || (node.__events = {});
12-
const oldEventHandler = eventStore[eventName];
12+
const oldEventHandler = eventStore[eventNameLc];
1313

1414
// Remove old listener so they don't double up.
1515
if (oldEventHandler) {
16-
node.removeEventListener(eventName, oldEventHandler);
16+
node.removeEventListener(eventNameLc, oldEventHandler);
1717
}
1818

1919
// Bind new listener.
2020
if (newEventHandler) {
21-
node.addEventListener(eventNameLc, eventStore[eventName] = function handler(e) {
21+
node.addEventListener(eventNameLc, eventStore[eventNameLc] = function handler(e) {
2222
newEventHandler.call(this, e);
2323
});
2424
}

test/unit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './unit/children';
2-
import './unit/custom-events';
2+
import './unit/events';
33
import './unit/display-name';
44
import './unit/errors';
55
import './unit/prop-types';

test/unit/custom-events.js renamed to test/unit/events.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,28 @@ describe('custom events', () => {
4242
done();
4343
}, 1);
4444
});
45+
46+
it('should not duplicate handlers', done => {
47+
let count = 0;
48+
const Comp = reactify(document.registerElement('x-custom-event-3', {
49+
prototype: Object.create(HTMLElement.prototype, {
50+
trigger: {
51+
value() {
52+
this.dispatchEvent(new CustomEvent('test'));
53+
},
54+
},
55+
}),
56+
}), { React, ReactDOM });
57+
58+
const func = () => ++count;
59+
60+
// Using both ontest and onTest (case-sensitive) test case-sensitivity.
61+
const comp = ReactDOM.render(<Comp ontest={func} onTest={func} />, window.fixture);
62+
63+
setTimeout(() => {
64+
ReactDOM.findDOMNode(comp).trigger();
65+
expect(count).to.equal(1);
66+
done();
67+
}, 1);
68+
});
4569
});

0 commit comments

Comments
 (0)