2016-08-04 17:49:32 +02:00
|
|
|
/* eslint-env qunit */
|
2015-05-04 01:12:38 +02:00
|
|
|
import * as Events from '../../src/js/utils/events.js';
|
2015-03-26 06:43:41 +02:00
|
|
|
import document from 'global/document';
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.module('Events');
|
2015-03-11 03:01:11 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should add and remove an event listener to an element', function() {
|
|
|
|
QUnit.expect(1);
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
|
|
|
const listener = function() {
|
|
|
|
QUnit.ok(true, 'Click Triggered');
|
2013-01-11 00:06:12 +03:00
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.on(el, 'click', listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// 1 click
|
|
|
|
Events.trigger(el, 'click');
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.off(el, 'click', listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// No click should happen.
|
|
|
|
Events.trigger(el, 'click');
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should add and remove multiple event listeners to an element with a single call', function() {
|
|
|
|
QUnit.expect(6);
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
|
|
|
const listener = function() {
|
|
|
|
QUnit.ok(true, 'Callback triggered');
|
2014-07-08 21:51:47 +03:00
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.on(el, ['click', 'event1', 'event2'], listener);
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'click');
|
|
|
|
Events.trigger(el, 'click');
|
|
|
|
Events.off(el, 'click', listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// No click should happen.
|
|
|
|
Events.trigger(el, 'click');
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'event1');
|
|
|
|
Events.trigger(el, 'event1');
|
|
|
|
Events.off(el, 'event1', listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// No event1 should happen.
|
|
|
|
Events.trigger(el, 'event1');
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'event2');
|
|
|
|
Events.trigger(el, 'event2');
|
|
|
|
Events.off(el, 'event2', listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// No event2 should happen.
|
|
|
|
Events.trigger(el, 'event2');
|
2014-07-08 21:51:47 +03:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should be possible to pass data when you trigger an event', function() {
|
|
|
|
QUnit.expect(6);
|
|
|
|
const el = document.createElement('div');
|
|
|
|
const fakeData1 = 'Fake Data 1';
|
|
|
|
const fakeData2 = {txt: 'Fake Data 2'};
|
2015-06-05 19:36:59 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const listener = function(evt, hash) {
|
|
|
|
QUnit.ok(true, 'Callback triggered');
|
2015-06-05 19:36:59 +02:00
|
|
|
deepEqual(fakeData1, hash.d1, 'Shoulbe be passed to the handler');
|
|
|
|
deepEqual(fakeData2, hash.d2, 'Shoulbe be passed to the handler');
|
|
|
|
};
|
|
|
|
|
|
|
|
Events.on(el, ['event1', 'event2'], listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.trigger(el, 'event1', { d1: fakeData1, d2: fakeData2});
|
|
|
|
Events.trigger(el, 'event2', { d1: fakeData1, d2: fakeData2});
|
2015-06-05 19:36:59 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should remove all listeners of a type', function() {
|
|
|
|
const el = document.createElement('div');
|
|
|
|
let clicks = 0;
|
|
|
|
const listener = function() {
|
2013-01-11 00:06:12 +03:00
|
|
|
clicks++;
|
|
|
|
};
|
2016-08-04 17:49:32 +02:00
|
|
|
const listener2 = function() {
|
2013-01-11 00:06:12 +03:00
|
|
|
clicks++;
|
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.on(el, 'click', listener);
|
|
|
|
Events.on(el, 'click', listener2);
|
2016-08-04 17:49:32 +02:00
|
|
|
// 2 clicks
|
|
|
|
Events.trigger(el, 'click');
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.ok(clicks === 2, 'both click listeners fired');
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.off(el, 'click');
|
2016-08-04 17:49:32 +02:00
|
|
|
// No click should happen.
|
|
|
|
Events.trigger(el, 'click');
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.ok(clicks === 2, 'no click listeners fired');
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should remove all listeners of an array of types', function() {
|
|
|
|
const el = document.createElement('div');
|
|
|
|
let calls = 0;
|
|
|
|
const listener = function() {
|
2014-07-08 21:51:47 +03:00
|
|
|
calls++;
|
|
|
|
};
|
2016-08-04 17:49:32 +02:00
|
|
|
const listener2 = function() {
|
2014-07-08 21:51:47 +03:00
|
|
|
calls++;
|
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.on(el, ['click', 'event1'], listener);
|
|
|
|
Events.on(el, ['click', 'event1'], listener2);
|
2016-08-04 17:49:32 +02:00
|
|
|
// 2 calls
|
|
|
|
Events.trigger(el, 'click');
|
|
|
|
// 2 calls
|
|
|
|
Events.trigger(el, 'event1');
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.ok(calls === 4, 'both click listeners fired');
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.off(el, ['click', 'event1']);
|
2016-08-04 17:49:32 +02:00
|
|
|
// No click should happen.
|
|
|
|
Events.trigger(el, 'click');
|
|
|
|
// No event1 should happen.
|
|
|
|
Events.trigger(el, 'event1');
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.ok(calls === 4, 'no event listeners fired');
|
2014-07-08 21:51:47 +03:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should remove all listeners from an element', function() {
|
|
|
|
QUnit.expect(2);
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
|
|
|
const listener = function() {
|
|
|
|
QUnit.ok(true, 'Fake1 Triggered');
|
2013-01-11 00:06:12 +03:00
|
|
|
};
|
2016-08-04 17:49:32 +02:00
|
|
|
const listener2 = function() {
|
|
|
|
QUnit.ok(true, 'Fake2 Triggered');
|
2013-01-11 00:06:12 +03:00
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.on(el, 'fake1', listener);
|
|
|
|
Events.on(el, 'fake2', listener2);
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'fake1');
|
|
|
|
Events.trigger(el, 'fake2');
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.off(el);
|
2013-01-11 00:06:12 +03:00
|
|
|
|
|
|
|
// No listener should happen.
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'fake1');
|
|
|
|
Events.trigger(el, 'fake2');
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should listen only once', function() {
|
|
|
|
QUnit.expect(1);
|
2013-01-11 00:06:12 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
|
|
|
const listener = function() {
|
|
|
|
QUnit.ok(true, 'Click Triggered');
|
2013-01-11 00:06:12 +03:00
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.one(el, 'click', listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// 1 click
|
|
|
|
Events.trigger(el, 'click');
|
|
|
|
// No click should happen.
|
|
|
|
Events.trigger(el, 'click');
|
2013-01-11 00:06:12 +03:00
|
|
|
});
|
2013-01-18 04:33:53 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should listen only once in multiple events from a single call', function() {
|
|
|
|
QUnit.expect(3);
|
2014-07-08 21:51:47 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
|
|
|
const listener = function() {
|
|
|
|
QUnit.ok(true, 'Callback Triggered');
|
2014-07-08 21:51:47 +03:00
|
|
|
};
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.one(el, ['click', 'event1', 'event2'], listener);
|
2016-08-04 17:49:32 +02:00
|
|
|
// 1 click
|
|
|
|
Events.trigger(el, 'click');
|
|
|
|
// No click should happen.
|
|
|
|
Events.trigger(el, 'click');
|
|
|
|
// event1 must be handled.
|
|
|
|
Events.trigger(el, 'event1');
|
|
|
|
// No event1 should be handled.
|
|
|
|
Events.trigger(el, 'event1');
|
|
|
|
// event2 must be handled.
|
|
|
|
Events.trigger(el, 'event2');
|
|
|
|
// No event2 should be handled.
|
|
|
|
Events.trigger(el, 'event2');
|
2014-07-08 21:51:47 +03:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should stop immediate propagtion', function() {
|
|
|
|
QUnit.expect(1);
|
2013-01-18 04:33:53 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
2013-01-18 04:33:53 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(el, 'test', function(e) {
|
|
|
|
QUnit.ok(true, 'First listener fired');
|
2013-01-18 04:33:53 +03:00
|
|
|
e.stopImmediatePropagation();
|
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(el, 'test', function(e) {
|
|
|
|
QUnit.ok(false, 'Second listener fired');
|
2013-01-18 04:33:53 +03:00
|
|
|
});
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'test');
|
2013-01-18 04:33:53 +03:00
|
|
|
});
|
2013-09-04 23:33:27 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should bubble up DOM unless bubbles == false', function() {
|
|
|
|
QUnit.expect(3);
|
2013-09-04 23:33:27 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const outer = document.createElement('div');
|
|
|
|
const inner = outer.appendChild(document.createElement('div'));
|
2013-09-04 23:33:27 +03:00
|
|
|
|
|
|
|
// Verify that if bubbles === true, event bubbles up dom.
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(inner, 'bubbles', function(e) {
|
|
|
|
QUnit.ok(true, 'Inner listener fired');
|
2013-09-04 23:33:27 +03:00
|
|
|
});
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(outer, 'bubbles', function(e) {
|
|
|
|
QUnit.ok(true, 'Outer listener fired');
|
2013-09-04 23:33:27 +03:00
|
|
|
});
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.trigger(inner, { type: 'bubbles', target: inner, bubbles: true });
|
2013-09-04 23:33:27 +03:00
|
|
|
|
|
|
|
// Only change 'bubbles' to false, and verify only inner handler is called.
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(inner, 'nobub', function(e) {
|
|
|
|
QUnit.ok(true, 'Inner listener fired');
|
2013-09-04 23:33:27 +03:00
|
|
|
});
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(outer, 'nobub', function(e) {
|
|
|
|
QUnit.ok(false, 'Outer listener fired');
|
2013-09-04 23:33:27 +03:00
|
|
|
});
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.trigger(inner, { type: 'nobub', target: inner, bubbles: false });
|
2013-09-04 23:33:27 +03:00
|
|
|
});
|
2014-03-25 00:56:02 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should have a defaultPrevented property on an event that was prevent from doing default action', function() {
|
|
|
|
QUnit.expect(2);
|
2014-03-25 00:56:02 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el = document.createElement('div');
|
2014-03-25 00:56:02 +03:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(el, 'test', function(e) {
|
|
|
|
QUnit.ok(true, 'First listener fired');
|
2014-03-25 00:56:02 +03:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(el, 'test', function(e) {
|
|
|
|
QUnit.ok(e.defaultPrevented, 'Should have `defaultPrevented` to signify preventDefault being called');
|
2014-03-25 00:56:02 +03:00
|
|
|
});
|
|
|
|
|
2015-03-11 03:01:11 +02:00
|
|
|
Events.trigger(el, 'test');
|
2014-03-25 00:56:02 +03:00
|
|
|
});
|
2015-04-09 12:03:29 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.test('should have relatedTarget correctly set on the event', function() {
|
|
|
|
QUnit.expect(2);
|
2015-04-09 12:03:29 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
const el1 = document.createElement('div');
|
|
|
|
const el2 = document.createElement('div');
|
|
|
|
const relatedEl = document.createElement('div');
|
2015-04-09 12:03:29 +02:00
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.on(el1, 'click', function(e) {
|
|
|
|
QUnit.equal(e.relatedTarget, relatedEl, 'relatedTarget is set for all browsers when related element is set on the event');
|
2015-04-09 12:03:29 +02:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.trigger(el1, { type: 'click', relatedTarget: relatedEl });
|
2015-04-09 12:03:29 +02:00
|
|
|
|
|
|
|
Events.on(el2, 'click', function(e) {
|
2016-08-04 17:49:32 +02:00
|
|
|
QUnit.equal(e.relatedTarget, null, 'relatedTarget is null when none is provided');
|
2015-04-09 12:03:29 +02:00
|
|
|
});
|
|
|
|
|
2016-08-04 17:49:32 +02:00
|
|
|
Events.trigger(el2, { type: 'click', relatedTarget: undefined });
|
2015-04-09 12:03:29 +02:00
|
|
|
});
|