206 lines
7.6 KiB
JavaScript
206 lines
7.6 KiB
JavaScript
import { EventGroup } from './EventGroup';
|
|
describe('EventGroup', function () {
|
|
it('can observe an HTML element event', function () {
|
|
var timesCalled = 0;
|
|
var sourceButton = document.createElement('button');
|
|
var parent = {
|
|
cb: function () {
|
|
timesCalled++;
|
|
},
|
|
};
|
|
var eg = new EventGroup(parent);
|
|
var ev = document.createEvent('HTMLEvents');
|
|
eg.on(sourceButton, 'click', parent.cb);
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
ev.initEvent('click', true, true);
|
|
sourceButton.dispatchEvent(ev);
|
|
expect(timesCalled).toEqual(1);
|
|
sourceButton.dispatchEvent(ev);
|
|
expect(timesCalled).toEqual(2);
|
|
eg.dispose();
|
|
sourceButton.dispatchEvent(ev);
|
|
expect(timesCalled).toEqual(2);
|
|
});
|
|
it('can observe an object event', function () {
|
|
var timesCalled = 0;
|
|
var sourceObject = {};
|
|
var parent = {
|
|
cb: function () {
|
|
timesCalled++;
|
|
},
|
|
};
|
|
var parentEvents = new EventGroup(parent);
|
|
var sourceEvents = new EventGroup(sourceObject);
|
|
sourceEvents.declare(['foo', 'bar']);
|
|
expect(EventGroup.isDeclared(sourceObject, 'foo')).toEqual(true);
|
|
expect(EventGroup.isDeclared(sourceObject, 'bar')).toEqual(true);
|
|
expect(EventGroup.isDeclared(sourceObject, 'baz')).toEqual(false);
|
|
parentEvents.on(sourceObject, 'foo, bar', parent.cb);
|
|
expect(EventGroup.isObserved(sourceObject, 'foo')).toEqual(true);
|
|
expect(EventGroup.isObserved(sourceObject, 'bar')).toEqual(true);
|
|
expect(EventGroup.isObserved(sourceObject, 'baz')).toEqual(false);
|
|
sourceEvents.raise('foo');
|
|
expect(timesCalled).toEqual(1);
|
|
sourceEvents.raise('bar');
|
|
expect(timesCalled).toEqual(2);
|
|
parentEvents.dispose();
|
|
sourceEvents.raise('thing');
|
|
expect(timesCalled).toEqual(2);
|
|
});
|
|
it('can bubble object events', function () {
|
|
var rootCalled = 0;
|
|
var childCalled = 0;
|
|
var grandChildCalled = 0;
|
|
var childResponse = true;
|
|
var root = {
|
|
cb: function () {
|
|
rootCalled++;
|
|
},
|
|
};
|
|
var child = {
|
|
parent: root,
|
|
cb: function () {
|
|
childCalled++;
|
|
return childResponse;
|
|
},
|
|
};
|
|
var grandChild = {
|
|
parent: child,
|
|
cb: function () {
|
|
grandChildCalled++;
|
|
},
|
|
};
|
|
var rootEvents = new EventGroup(root);
|
|
var childEvents = new EventGroup(child);
|
|
var grandChildEvents = new EventGroup(grandChild);
|
|
rootEvents.on(root, 'foo', root.cb);
|
|
childEvents.on(child, 'foo', child.cb);
|
|
grandChildEvents.on(grandChild, 'foo', grandChild.cb);
|
|
// bubble up to the root.
|
|
grandChildEvents.raise('foo', null, true);
|
|
expect(rootCalled).toEqual(1);
|
|
expect(childCalled).toEqual(1);
|
|
expect(grandChildCalled).toEqual(1);
|
|
// cancel at the child.
|
|
childResponse = false;
|
|
grandChildEvents.raise('foo', null, true);
|
|
expect(rootCalled).toEqual(1);
|
|
expect(childCalled).toEqual(2);
|
|
expect(grandChildCalled).toEqual(2);
|
|
// dispose all.
|
|
rootEvents.dispose();
|
|
childEvents.dispose();
|
|
grandChildEvents.dispose();
|
|
grandChildEvents.raise('foo', null, true);
|
|
expect(rootCalled).toEqual(1);
|
|
expect(childCalled).toEqual(2);
|
|
expect(grandChildCalled).toEqual(2);
|
|
});
|
|
it('can cancelBubble/preventDefault if false is returned on an element event callback', function () {
|
|
var rootCalled = 0;
|
|
var childCalled = 0;
|
|
var childResponse = true;
|
|
var rootDiv = document.createElement('div');
|
|
var childDiv = document.createElement('div');
|
|
var grandChildButton = document.createElement('button');
|
|
var parent = {
|
|
onRootClick: function () {
|
|
rootCalled++;
|
|
},
|
|
onChildClick: function () {
|
|
childCalled++;
|
|
return childResponse;
|
|
},
|
|
};
|
|
var parentEvents = new EventGroup(parent);
|
|
parentEvents.on(childDiv, 'click', parent.onChildClick);
|
|
parentEvents.on(rootDiv, 'click', parent.onRootClick);
|
|
document.body.appendChild(rootDiv).appendChild(childDiv).appendChild(grandChildButton);
|
|
try {
|
|
var ev = document.createEvent('HTMLEvents');
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
ev.initEvent('click', true, true);
|
|
grandChildButton.dispatchEvent(ev);
|
|
// verify we bubble.
|
|
expect(childCalled).toEqual(1);
|
|
expect(rootCalled).toEqual(1);
|
|
// now return false at the child, shouldn't hit root.
|
|
childResponse = false;
|
|
grandChildButton.dispatchEvent(ev);
|
|
expect(childCalled).toEqual(2);
|
|
expect(rootCalled).toEqual(1);
|
|
parentEvents.dispose();
|
|
grandChildButton.dispatchEvent(ev);
|
|
expect(childCalled).toEqual(2);
|
|
expect(rootCalled).toEqual(1);
|
|
}
|
|
finally {
|
|
document.body.removeChild(rootDiv);
|
|
}
|
|
});
|
|
it('can selectively remove event handlers', function () {
|
|
var cb1Called = 0;
|
|
var cb2Called = 0;
|
|
var sourceObject = {};
|
|
var parent = {
|
|
cb1: function () {
|
|
cb1Called++;
|
|
},
|
|
cb2: function () {
|
|
cb2Called++;
|
|
},
|
|
};
|
|
var parentEvents = new EventGroup(parent);
|
|
var sourceEvents = new EventGroup(sourceObject);
|
|
parentEvents.on(sourceObject, 'foo', parent.cb1);
|
|
parentEvents.on(sourceObject, 'foo', parent.cb2);
|
|
sourceEvents.raise('foo');
|
|
expect(cb1Called).toEqual(1);
|
|
expect(cb1Called).toEqual(1);
|
|
// remove one.
|
|
parentEvents.off(sourceObject, 'foo', parent.cb1);
|
|
sourceEvents.raise('foo');
|
|
expect(cb1Called).toEqual(1);
|
|
expect(cb2Called).toEqual(2);
|
|
// attach it again.
|
|
parentEvents.on(sourceObject, 'foo', parent.cb1);
|
|
sourceEvents.raise('foo');
|
|
expect(cb1Called).toEqual(2);
|
|
expect(cb2Called).toEqual(3);
|
|
// detatch both based on event name.
|
|
parentEvents.off(sourceObject, 'foo');
|
|
sourceEvents.raise('foo');
|
|
expect(cb1Called).toEqual(2);
|
|
expect(cb2Called).toEqual(3);
|
|
// attach it again.
|
|
parentEvents.on(sourceObject, 'foo', parent.cb1);
|
|
parentEvents.on(sourceObject, 'foo', parent.cb2);
|
|
sourceEvents.raise('foo');
|
|
expect(cb1Called).toEqual(3);
|
|
expect(cb2Called).toEqual(4);
|
|
// detach based on object.
|
|
parentEvents.off(sourceObject);
|
|
sourceEvents.raise('foo');
|
|
expect(cb1Called).toEqual(3);
|
|
expect(cb2Called).toEqual(4);
|
|
});
|
|
it('can raise custom html events', function () {
|
|
var timesCalled = 0;
|
|
var sourceButton = document.createElement('button');
|
|
var parent = {
|
|
cb: function () {
|
|
timesCalled++;
|
|
},
|
|
};
|
|
var eg = new EventGroup(parent);
|
|
eg.on(sourceButton, 'foobar', parent.cb);
|
|
EventGroup.raise(sourceButton, 'foobar');
|
|
expect(timesCalled).toEqual(1);
|
|
EventGroup.raise(sourceButton, 'foobar');
|
|
expect(timesCalled).toEqual(2);
|
|
eg.dispose();
|
|
EventGroup.raise(sourceButton, 'foobar');
|
|
expect(timesCalled).toEqual(2);
|
|
});
|
|
});
|
|
//# sourceMappingURL=EventGroup.test.js.map
|