Browse files

`getset` emits event

  • Loading branch information...
1 parent 8d0172c commit d8ec7dc9d32d4c28ab34f7a12a62dea55bc57d1e @fent fent committed Feb 15, 2012
Showing with 39 additions and 0 deletions.
  1. +2 −0 lib/eventvat.js
  2. +22 −0 test/events-test.js
  3. +15 −0 test/methods-test.js
View
2 lib/eventvat.js
@@ -368,6 +368,8 @@
EventVat.prototype.getset = function(key, value) {
var old = this.get(key);
this.set(key, value);
+ this.emit('getset ' + key, value, old);
+ this.emit('getset', key, value, old);
return old;
};
View
22 test/events-test.js
@@ -88,6 +88,28 @@ module.exports = simpleEvents({
test.done();
},
+ 'Raise event on `getset` method invokation': function (test) {
+
+ var vat = EventVat();
+
+ vat.on('getset', function(key, value, old) {
+ test.equal(key, 'foo');
+ test.equal(value, 2);
+ test.equal(old, 1);
+ });
+
+ vat.on('getset foo', function(value, old) {
+ test.equal(value, 2);
+ test.equal(old, 1);
+ });
+
+ vat.set('foo', 1);
+ vat.getset('foo', 2);
+
+ test.expect(5);
+ vat.die();
+ test.done();
+ },
'Raise event on `rename` method invokation': function (test) {
var vat = EventVat();
View
15 test/methods-test.js
@@ -74,6 +74,21 @@ this.methodSuite = {
test.done();
},
+ 'Invoke `getset` method and report returned value and stored value': function (test) {
+
+ var vat = EventVat();
+
+ test.equal(vat.get('foo'), null);
+ vat.set('foo', 1);
+ test.equal(vat.get('foo'), 1);
+
+ test.equal(vat.getset('foo', 2), 1);
+ test.equal(vat.get('foo'), 2);
+
+ vat.die();
+ test.done();
+
+ },
'Invoke `rename` method and get the value of the new key': function (test) {
var vat = EventVat();

0 comments on commit d8ec7dc

Please sign in to comment.