paper.js/test/tests/Emitter.js

110 lines
2.8 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 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
2015-04-08 04:42:50 -04:00
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
QUnit.module('Emitter');
2015-04-08 04:42:50 -04:00
test('on()', function() {
var emitter = new Item(),
installed;
// fake event type registration
2016-01-27 17:12:37 -05:00
emitter._eventTypes = {
mousemove: {
install: function() {
installed = true;
}
}
};
equals(function() {
return !emitter.responds('mousemove');
}, true);
2016-01-27 17:12:37 -05:00
emitter.on('mousemove', function() {});
2016-01-27 17:12:37 -05:00
equals(function() { return emitter.responds('mousemove'); }, true);
equals(function() { return installed; }, true);
2016-01-27 17:12:37 -05:00
// 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() {
2016-01-27 17:12:37 -05:00
return emitter.responds('customUnregistered');
2015-04-08 04:42:50 -04:00
}, true);
});
test('off()', function() {
var emitter = new Item(),
2016-01-27 17:12:37 -05:00
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);
2016-01-27 17:12:37 -05:00
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);
2016-01-27 17:12:37 -05:00
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 Item(),
called,
2016-01-27 17:12:37 -05:00
handler = function (e) {
called = e;
};
// fake event type registration
2016-01-27 17:12:37 -05:00
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
});