paper.js/test/tests/Emitter.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-04-08 04:42:50 -04:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
2015-04-08 04:42:50 -04:00
* http://scratchdisk.com/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
module('Emitter');
test('on()', function() {
2015-04-08 15:01:38 -04:00
var emitter = new Base(Emitter),
installed;
// fake event type registration
emitter._eventTypes = {mousemove: {install: function(){ installed = true;} } };
equals(function() {
return !emitter.responds('mousemove');
}, true);
emitter.on('mousemove', function() {});
equals(function() {
return emitter.responds('mousemove')
2015-04-08 04:42:50 -04:00
}, true);
equals(function() { return installed; }, true);
// one time installation only
installed = false;
emitter.on('mousemove', function() {});
equals(function() { return !installed; }, true);
2015-04-08 04:42:50 -04:00
emitter.on('customUnregistered', function() {});
equals(function() {
2015-04-08 04:42:50 -04:00
return emitter.responds('customUnregistered')
}, true);
});
test('off()', function() {
var emitter = new Base(Emitter),
uninstalled, called = 0,
handler = function () {called++},
handler2 = function () {};
emitter._eventTypes = {mousemove: {uninstall: function(){ uninstalled = true;} } };
2015-04-08 04:42:50 -04:00
emitter.on('mousemove', handler);
emitter.on('mousemove', handler2);
emitter.on('custom', handler);
emitter.emit('mousemove');
equals(function() { return called == 1; }, true);
2015-04-08 04:42:50 -04:00
emitter.off('mousemove', handler2);
emitter.emit('mousemove');
equals(function() { return called == 2; }, true);
equals(function() { return !uninstalled }, true);
2015-04-08 04:42:50 -04:00
emitter.off('mousemove', handler);
emitter.emit('mousemove');
equals(function() { return called == 2; }, true);
equals(function() { return uninstalled }, true);
2015-04-08 04:42:50 -04:00
called = 0;
emitter.emit('custom');
equals(function() { return called == 1; }, true);
emitter.off('custom', handler);
emitter.emit('custom');
equals(function() { return called == 1; }, true);
2015-04-08 04:42:50 -04:00
});
test('emit()', function() {
var emitter = new Base(Emitter),
called,
handler = function (e) {called = e};
// fake event type registration
emitter._eventTypes = {mousemove: {} };
emitter.on('mousemove', handler);
emitter.on('custom', handler);
2015-04-08 04:42:50 -04:00
emitter.emit('mousemove', 'mousemove');
equals(function() {
return called == 'mousemove';
}, true);
2015-04-08 04:42:50 -04:00
emitter.emit('custom', 'custom');
equals(function() {
return called == 'custom';
}, true);
2015-04-08 04:42:50 -04:00
});