Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "reflux", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"homepage": "https://github.com/spoike/reflux", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -185,2 +185,3 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Reflux=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | ||
},{}],2:[function(_dereq_,module,exports){ | ||
var _ = _dereq_('./utils'); | ||
module.exports = { | ||
@@ -204,6 +205,9 @@ | ||
* @param {Function} callback The callback to register as event handler | ||
* @param {Function} defaultCallback The callback to register as default handler | ||
*/ | ||
listenTo: function(listenable, callback) { | ||
listenTo: function(listenable, callback, defaultCallback) { | ||
var unsubscribe = listenable.listen(callback, this); | ||
this.subscriptions.push(unsubscribe); | ||
_.handleDefaultCallback(this, listenable, defaultCallback); | ||
}, | ||
@@ -219,3 +223,3 @@ | ||
},{}],3:[function(_dereq_,module,exports){ | ||
},{"./utils":7}],3:[function(_dereq_,module,exports){ | ||
var createAction = _dereq_('./createAction'); | ||
@@ -383,3 +387,3 @@ | ||
_.extend(Store.prototype, definition); | ||
Store.prototype.listenTo = function(listenable, callback) { | ||
Store.prototype.listenTo = function(listenable, callback, defaultCallback) { | ||
if (listenable === this) { | ||
@@ -394,4 +398,10 @@ throw Error("Store is not able to listen to itself"); | ||
} | ||
_.handleDefaultCallback(this, listenable, defaultCallback); | ||
this.registered.push(listenable); | ||
return listenable.listen(callback, this); | ||
var unsubscribe = listenable.listen(callback, this); | ||
var self = this; | ||
return function () { | ||
unsubscribe(); | ||
self.registered.splice(self.registered.indexOf(listenable), 1); | ||
}; | ||
}; | ||
@@ -458,7 +468,7 @@ Store.prototype.listen = function(callback, bindContext) { | ||
/* | ||
* isObject, extend and isFunction are taken from undescore/lodash in | ||
* isObject, extend, isFunction, and bind are taken from undescore/lodash in | ||
* order to remove the dependency | ||
*/ | ||
var isObject = module.exports.isObject = function(obj) { | ||
var isObject = exports.isObject = function(obj) { | ||
var type = typeof obj; | ||
@@ -468,3 +478,3 @@ return type === 'function' || type === 'object' && !!obj; | ||
module.exports.extend = function(obj) { | ||
exports.extend = function(obj) { | ||
if (!isObject(obj)) { | ||
@@ -483,13 +493,28 @@ return obj; | ||
module.exports.isFunction = function(value) { | ||
var isFunction = exports.isFunction = function(value) { | ||
return typeof value === 'function'; | ||
}; | ||
module.exports.EventEmitter = _dereq_('eventemitter3'); | ||
module.exports.nextTick = function(callback) { | ||
exports.EventEmitter = _dereq_('eventemitter3'); | ||
exports.nextTick = function(callback) { | ||
setTimeout(callback, 0); | ||
}; | ||
exports.handleDefaultCallback = function (listener, listenable, defaultCallback) { | ||
if (defaultCallback && isFunction(defaultCallback)) { | ||
if (listenable.getDefaultData && isFunction(listenable.getDefaultData)) { | ||
data = listenable.getDefaultData(); | ||
if (data && data.then && isFunction(data.then)) { | ||
data.then(function() { | ||
defaultCallback.apply(listener, arguments); | ||
}); | ||
} else { | ||
defaultCallback.call(listener, data); | ||
} | ||
} | ||
} | ||
}; | ||
},{"eventemitter3":1}]},{},[6]) | ||
(6) | ||
}); |
@@ -1,1 +0,1 @@ | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.Reflux=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){"use strict";function c(){this._events={}}c.prototype.listeners=function(a){return Array.apply(this,this._events[a]||[])},c.prototype.emit=function(a,b,c,d,e,f){if(!this._events||!this._events[a])return!1;var g,h,i=this._events[a],j=i.length,k=arguments.length,l=i[0];if(1===j)switch(l.__EE3_once&&this.removeListener(a,l),k){case 1:l.call(l.__EE3_context||this);break;case 2:l.call(l.__EE3_context||this,b);break;case 3:l.call(l.__EE3_context||this,b,c);break;case 4:l.call(l.__EE3_context||this,b,c,d);break;case 5:l.call(l.__EE3_context||this,b,c,d,e);break;case 6:l.call(l.__EE3_context||this,b,c,d,e,f);break;default:for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];l.apply(l.__EE3_context||this,g)}else{for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];for(h=0;j>h;l=i[++h])l.__EE3_once&&this.removeListener(a,l),l.apply(l.__EE3_context||this,g)}return!0},c.prototype.on=function(a,b,c){return this._events||(this._events={}),this._events[a]||(this._events[a]=[]),b.__EE3_context=c,this._events[a].push(b),this},c.prototype.once=function(a,b,c){return b.__EE3_once=!0,this.on(a,b,c)},c.prototype.removeListener=function(a,b){if(!this._events||!this._events[a])return this;for(var c=this._events[a],d=[],e=0,f=c.length;f>e;e++)b&&c[e]!==b&&d.push(c[e]);return this._events[a]=d.length?d:null,this},c.prototype.removeAllListeners=function(a){return this._events?(a?this._events[a]=null:this._events={},this):this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prototype.setMaxListeners=function(){return this},c.EventEmitter=c,c.EventEmitter2=c,c.EventEmitter3=c;try{b.exports=c}catch(d){}},{}],2:[function(a,b){b.exports={componentWillMount:function(){this.subscriptions=[]},listenTo:function(a,b){var c=a.listen(b,this);this.subscriptions.push(c)},componentWillUnmount:function(){this.subscriptions.forEach(function(a){a()}),this.subscriptions=[]}}},{}],3:[function(a,b){var c=a("./createAction"),d=Array.prototype.slice;b.exports=function(){function a(){g=new Array(i),h=new Array(i)}function b(a){return function(){g[a]=!0,h[a]=d.call(arguments),e()}}function e(){f()&&(j.apply(j,h),a())}function f(){for(var a=0;i>a;a++)if(!g[a])return!1;return!0}var g,h,i=arguments.length,j=c();j.hasListener=function(a){for(var b,c=0;c<h.length;++c)if(b=h[c],b===a||b.hasListener&&b.hasListener(a))return!0;return!1},a();for(var k=0;i>k;k++)arguments[k].listen(b(k),null);return j}},{"./createAction":4}],4:[function(a,b){var c=a("./utils");b.exports=function(){var a,b=new c.EventEmitter,d="action";return a=function(){var e=arguments;c.nextTick(function(){a.preEmit.apply(a,e),a.shouldEmit.apply(a,e)&&b.emit(d,e)})},a.listen=function(a,c){var e=function(b){a.apply(c,b)};return b.addListener(d,e),function(){b.removeListener(d,e)}},a.preEmit=function(){},a.shouldEmit=function(){return!0},a}},{"./utils":7}],5:[function(a,b){var c=a("./utils");b.exports=function(a){function b(){this.registered=[],this.init&&c.isFunction(this.init)&&this.init()}var d=new c.EventEmitter,e="change";return c.extend(b.prototype,a),b.prototype.listenTo=function(a,b){if(a===this)throw Error("Store is not able to listen to itself");if(!c.isFunction(a.listen))throw new TypeError(a+" is missing a listen method");if(this.hasListener(a))throw Error("Store cannot listen to this listenable because of circular loop");return this.registered.push(a),a.listen(b,this)},b.prototype.listen=function(a,b){var c=function(c){a.apply(b,c)};return c.l=a,d.addListener(e,c),function(){d.removeListener(e,c)}},b.prototype.trigger=function(){d.emit(e,arguments)},b.prototype.hasListener=function(a){for(var b,c=0;c<this.registered.length;++c)if(b=this.registered[c],b===a||b.hasListener&&b.hasListener(a))return!0;return!1},new b}},{"./utils":7}],6:[function(a,b,c){c.createAction=a("./createAction"),c.createStore=a("./createStore"),c.ListenerMixin=a("./ListenerMixin"),c.all=a("./all"),c.createActions=function(a){for(var b=0,d={};b<a.length;b++)d[a[b]]=c.createAction();return d},c.setEventEmitter=function(b){var c=a("./utils");c.EventEmitter=b},c.nextTick=function(b){var c=a("./utils");c.nextTick=b}},{"./ListenerMixin":2,"./all":3,"./createAction":4,"./createStore":5,"./utils":7}],7:[function(a,b){var c=b.exports.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a};b.exports.extend=function(a){if(!c(a))return a;for(var b,d,e=1,f=arguments.length;f>e;e++){b=arguments[e];for(d in b)a[d]=b[d]}return a},b.exports.isFunction=function(a){return"function"==typeof a},b.exports.EventEmitter=a("eventemitter3"),b.exports.nextTick=function(a){setTimeout(a,0)}},{eventemitter3:1}]},{},[6])(6)}); | ||
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;"undefined"!=typeof window?b=window:"undefined"!=typeof global?b=global:"undefined"!=typeof self&&(b=self),b.Reflux=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){"use strict";function c(){this._events={}}c.prototype.listeners=function(a){return Array.apply(this,this._events[a]||[])},c.prototype.emit=function(a,b,c,d,e,f){if(!this._events||!this._events[a])return!1;var g,h,i=this._events[a],j=i.length,k=arguments.length,l=i[0];if(1===j)switch(l.__EE3_once&&this.removeListener(a,l),k){case 1:l.call(l.__EE3_context||this);break;case 2:l.call(l.__EE3_context||this,b);break;case 3:l.call(l.__EE3_context||this,b,c);break;case 4:l.call(l.__EE3_context||this,b,c,d);break;case 5:l.call(l.__EE3_context||this,b,c,d,e);break;case 6:l.call(l.__EE3_context||this,b,c,d,e,f);break;default:for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];l.apply(l.__EE3_context||this,g)}else{for(h=1,g=new Array(k-1);k>h;h++)g[h-1]=arguments[h];for(h=0;j>h;l=i[++h])l.__EE3_once&&this.removeListener(a,l),l.apply(l.__EE3_context||this,g)}return!0},c.prototype.on=function(a,b,c){return this._events||(this._events={}),this._events[a]||(this._events[a]=[]),b.__EE3_context=c,this._events[a].push(b),this},c.prototype.once=function(a,b,c){return b.__EE3_once=!0,this.on(a,b,c)},c.prototype.removeListener=function(a,b){if(!this._events||!this._events[a])return this;for(var c=this._events[a],d=[],e=0,f=c.length;f>e;e++)b&&c[e]!==b&&d.push(c[e]);return this._events[a]=d.length?d:null,this},c.prototype.removeAllListeners=function(a){return this._events?(a?this._events[a]=null:this._events={},this):this},c.prototype.off=c.prototype.removeListener,c.prototype.addListener=c.prototype.on,c.prototype.setMaxListeners=function(){return this},c.EventEmitter=c,c.EventEmitter2=c,c.EventEmitter3=c;try{b.exports=c}catch(d){}},{}],2:[function(a,b){var c=a("./utils");b.exports={componentWillMount:function(){this.subscriptions=[]},listenTo:function(a,b,d){var e=a.listen(b,this);this.subscriptions.push(e),c.handleDefaultCallback(this,a,d)},componentWillUnmount:function(){this.subscriptions.forEach(function(a){a()}),this.subscriptions=[]}}},{"./utils":7}],3:[function(a,b){var c=a("./createAction"),d=Array.prototype.slice;b.exports=function(){function a(){g=new Array(i),h=new Array(i)}function b(a){return function(){g[a]=!0,h[a]=d.call(arguments),e()}}function e(){f()&&(j.apply(j,h),a())}function f(){for(var a=0;i>a;a++)if(!g[a])return!1;return!0}var g,h,i=arguments.length,j=c();j.hasListener=function(a){for(var b,c=0;c<h.length;++c)if(b=h[c],b===a||b.hasListener&&b.hasListener(a))return!0;return!1},a();for(var k=0;i>k;k++)arguments[k].listen(b(k),null);return j}},{"./createAction":4}],4:[function(a,b){var c=a("./utils");b.exports=function(){var a,b=new c.EventEmitter,d="action";return a=function(){var e=arguments;c.nextTick(function(){a.preEmit.apply(a,e),a.shouldEmit.apply(a,e)&&b.emit(d,e)})},a.listen=function(a,c){var e=function(b){a.apply(c,b)};return b.addListener(d,e),function(){b.removeListener(d,e)}},a.preEmit=function(){},a.shouldEmit=function(){return!0},a}},{"./utils":7}],5:[function(a,b){var c=a("./utils");b.exports=function(a){function b(){this.registered=[],this.init&&c.isFunction(this.init)&&this.init()}var d=new c.EventEmitter,e="change";return c.extend(b.prototype,a),b.prototype.listenTo=function(a,b,d){if(a===this)throw Error("Store is not able to listen to itself");if(!c.isFunction(a.listen))throw new TypeError(a+" is missing a listen method");if(this.hasListener(a))throw Error("Store cannot listen to this listenable because of circular loop");c.handleDefaultCallback(this,a,d),this.registered.push(a);var e=a.listen(b,this),f=this;return function(){e(),f.registered.splice(f.registered.indexOf(a),1)}},b.prototype.listen=function(a,b){var c=function(c){a.apply(b,c)};return c.l=a,d.addListener(e,c),function(){d.removeListener(e,c)}},b.prototype.trigger=function(){d.emit(e,arguments)},b.prototype.hasListener=function(a){for(var b,c=0;c<this.registered.length;++c)if(b=this.registered[c],b===a||b.hasListener&&b.hasListener(a))return!0;return!1},new b}},{"./utils":7}],6:[function(a,b,c){c.createAction=a("./createAction"),c.createStore=a("./createStore"),c.ListenerMixin=a("./ListenerMixin"),c.all=a("./all"),c.createActions=function(a){for(var b=0,d={};b<a.length;b++)d[a[b]]=c.createAction();return d},c.setEventEmitter=function(b){var c=a("./utils");c.EventEmitter=b},c.nextTick=function(b){var c=a("./utils");c.nextTick=b}},{"./ListenerMixin":2,"./all":3,"./createAction":4,"./createStore":5,"./utils":7}],7:[function(a,b,c){var d=c.isObject=function(a){var b=typeof a;return"function"===b||"object"===b&&!!a};c.extend=function(a){if(!d(a))return a;for(var b,c,e=1,f=arguments.length;f>e;e++){b=arguments[e];for(c in b)a[c]=b[c]}return a};var e=c.isFunction=function(a){return"function"==typeof a};c.EventEmitter=a("eventemitter3"),c.nextTick=function(a){setTimeout(a,0)},c.handleDefaultCallback=function(a,b,c){c&&e(c)&&b.getDefaultData&&e(b.getDefaultData)&&(data=b.getDefaultData(),data&&data.then&&e(data.then)?data.then(function(){c.apply(a,arguments)}):c.call(a,data))}},{eventemitter3:1}]},{},[6])(6)}); |
{ | ||
"name": "reflux", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A simple library for uni-directional dataflow application architecture inspired by ReactJS Flux", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,5 +5,5 @@ # Reflux | ||
[![Build Status](https://travis-ci.org/spoike/reflux.svg?branch=master)](https://travis-ci.org/spoike/reflux) | ||
[![Build Status](https://travis-ci.org/spoike/refluxjs.svg?branch=master)](https://travis-ci.org/spoike/refluxjs) | ||
[![Browser support](https://ci.testling.com/spoike/reflux.png)](https://ci.testling.com/spoike/reflux) | ||
[![Browser support](https://ci.testling.com/spoike/refluxjs.png)](https://ci.testling.com/spoike/refluxjs) | ||
@@ -295,2 +295,20 @@ You can read an overview of Flux [here](http://facebook.github.io/react/docs/flux-overview.html), however the gist of it is to introduce a more functional programming style architecture by eschewing MVC like pattern and adopting a single data flow pattern. | ||
### Sending default data with the listenTo function | ||
The `listenTo` function provided by the `Store` and the `ListenerMixin` has a third parameter that accepts a callback. This callback will be invoked when the listener is registered with whatever the `getInitialData` is returning. | ||
```javascript | ||
var exampleStore = Reflux.createStore({ | ||
init: function() {}, | ||
getInitialData: function() { | ||
return "the initial data"; | ||
} | ||
}); | ||
// Anything that will listen to the example store | ||
this.listenTo(exampleStore, onChangeCallback, initialCallback) | ||
// initialCallback will be invoked immediately | ||
``` | ||
## Colophon | ||
@@ -297,0 +315,0 @@ |
@@ -19,3 +19,3 @@ var _ = require('./utils'); | ||
_.extend(Store.prototype, definition); | ||
Store.prototype.listenTo = function(listenable, callback) { | ||
Store.prototype.listenTo = function(listenable, callback, defaultCallback) { | ||
if (listenable === this) { | ||
@@ -30,4 +30,10 @@ throw Error("Store is not able to listen to itself"); | ||
} | ||
_.handleDefaultCallback(this, listenable, defaultCallback); | ||
this.registered.push(listenable); | ||
return listenable.listen(callback, this); | ||
var unsubscribe = listenable.listen(callback, this); | ||
var self = this; | ||
return function () { | ||
unsubscribe(); | ||
self.registered.splice(self.registered.indexOf(listenable), 1); | ||
}; | ||
}; | ||
@@ -34,0 +40,0 @@ Store.prototype.listen = function(callback, bindContext) { |
@@ -0,1 +1,2 @@ | ||
var _ = require('./utils'); | ||
module.exports = { | ||
@@ -19,6 +20,9 @@ | ||
* @param {Function} callback The callback to register as event handler | ||
* @param {Function} defaultCallback The callback to register as default handler | ||
*/ | ||
listenTo: function(listenable, callback) { | ||
listenTo: function(listenable, callback, defaultCallback) { | ||
var unsubscribe = listenable.listen(callback, this); | ||
this.subscriptions.push(unsubscribe); | ||
_.handleDefaultCallback(this, listenable, defaultCallback); | ||
}, | ||
@@ -25,0 +29,0 @@ |
/* | ||
* isObject, extend and isFunction are taken from undescore/lodash in | ||
* isObject, extend, isFunction, and bind are taken from undescore/lodash in | ||
* order to remove the dependency | ||
*/ | ||
var isObject = module.exports.isObject = function(obj) { | ||
var isObject = exports.isObject = function(obj) { | ||
var type = typeof obj; | ||
@@ -11,3 +11,3 @@ return type === 'function' || type === 'object' && !!obj; | ||
module.exports.extend = function(obj) { | ||
exports.extend = function(obj) { | ||
if (!isObject(obj)) { | ||
@@ -26,9 +26,24 @@ return obj; | ||
module.exports.isFunction = function(value) { | ||
var isFunction = exports.isFunction = function(value) { | ||
return typeof value === 'function'; | ||
}; | ||
module.exports.EventEmitter = require('eventemitter3'); | ||
module.exports.nextTick = function(callback) { | ||
exports.EventEmitter = require('eventemitter3'); | ||
exports.nextTick = function(callback) { | ||
setTimeout(callback, 0); | ||
}; | ||
exports.handleDefaultCallback = function (listener, listenable, defaultCallback) { | ||
if (defaultCallback && isFunction(defaultCallback)) { | ||
if (listenable.getDefaultData && isFunction(listenable.getDefaultData)) { | ||
data = listenable.getDefaultData(); | ||
if (data && data.then && isFunction(data.then)) { | ||
data.then(function() { | ||
defaultCallback.apply(listener, arguments); | ||
}); | ||
} else { | ||
defaultCallback.call(listener, data); | ||
} | ||
} | ||
} | ||
}; |
@@ -72,7 +72,12 @@ var chai = require('chai'), | ||
it('should emit multiple times', function(done) { | ||
var callArgs = []; | ||
all.listen(function() { | ||
callArgs.push([].slice.call(arguments)); | ||
}, null); | ||
it('should emit multiple times', function() { | ||
var promise = Q.promise(function(resolve) { | ||
var callArgs = []; | ||
all.listen(function() { | ||
callArgs.push([].slice.call(arguments)); | ||
if (callArgs.length === 2) { | ||
resolve(callArgs); | ||
} | ||
}); | ||
}); | ||
@@ -87,9 +92,6 @@ action1('a'); | ||
setTimeout(function() { | ||
assert.deepEqual(callArgs, [ | ||
[['a'], ['b'], ['c']], | ||
[['x'], ['y'], ['z']] | ||
]); | ||
done(); | ||
}, 200); | ||
return assert.eventually.deepEqual(promise, [ | ||
[['a'], ['b'], ['c']], | ||
[['x'], ['y'], ['z']] | ||
]); | ||
}); | ||
@@ -96,0 +98,0 @@ |
var chai = require('chai'), | ||
assert = chai.assert, | ||
Reflux = require('../src'), | ||
_ = require('../src/utils'), | ||
Q = require('q'); | ||
@@ -70,2 +71,9 @@ | ||
it('can listenTo the same action again', function() { | ||
store.listenTo(action, store.actionCalled); | ||
action(1337, 'ninja'); | ||
return assert.eventually.deepEqual(promise, [1337, 'ninja']); | ||
}); | ||
}); | ||
@@ -112,2 +120,75 @@ | ||
describe('with one store listening to another store', function() { | ||
var action, | ||
baseDefinition; | ||
beforeEach(function () { | ||
action = Reflux.createAction(); | ||
baseDefinition = { | ||
init: function() { | ||
this.listenTo(action, this.actionCalled); | ||
}, | ||
actionCalled: function() { | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
this.trigger(args); | ||
} | ||
}; | ||
}); | ||
function createPromiseForTest(store) { | ||
return Q.Promise(function(resolve) { | ||
var storeTriggered = function (args) { | ||
args = args.map(function (arg) { | ||
return '[...] ' + arg; | ||
}); | ||
this.trigger(args); | ||
resolve(args); | ||
}; | ||
Reflux.createStore({ | ||
init: function() { | ||
this.listenTo(store, this.storeTriggered, storeTriggered); | ||
}, | ||
storeTriggered: storeTriggered | ||
}); | ||
}); | ||
} | ||
it('should be triggered with argument from upstream store', function() { | ||
var promise = createPromiseForTest(Reflux.createStore(baseDefinition)); | ||
action('my argument'); | ||
return assert.eventually.equal(promise, '[...] my argument'); | ||
}); | ||
it('should be triggered with arbitrary arguments from upstream store', function() { | ||
var promise = createPromiseForTest(Reflux.createStore(baseDefinition)); | ||
action(1337, 'ninja'); | ||
return assert.eventually.deepEqual(promise, ['[...] 1337', '[...] ninja']); | ||
}); | ||
it('should get default data from getDefaultData()', function() { | ||
var store = Reflux.createStore(_.extend(baseDefinition, { | ||
getDefaultData: function () { | ||
return ['default data']; | ||
} | ||
})); | ||
var promise = createPromiseForTest(store); | ||
return assert.eventually.equal(promise, '[...] default data'); | ||
}); | ||
it('should get default data from getDefaultData() returned promise', function() { | ||
var store = Reflux.createStore(_.extend(baseDefinition, { | ||
getDefaultData: function () { | ||
return Q.Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve(['default data']); | ||
}, 20); | ||
}); | ||
} | ||
})); | ||
var promise = createPromiseForTest(store); | ||
return assert.eventually.equal(promise, '[...] default data'); | ||
}); | ||
}); | ||
}); |
@@ -11,3 +11,4 @@ var chai = require('chai'), | ||
action, | ||
promise; | ||
promise, | ||
store; | ||
@@ -55,4 +56,4 @@ beforeEach(function() { | ||
setTimeout(function() { | ||
assert.equal(resolved, false); | ||
done(); | ||
assert.equal(resolved, false); | ||
done(); | ||
}, 200); | ||
@@ -62,2 +63,39 @@ }); | ||
describe('get default data', function () { | ||
beforeEach(function() { | ||
component.componentWillUnmount(); | ||
}); | ||
function mountComponent() { | ||
component.componentWillMount(); | ||
promise = Q.Promise(function(resolve) { | ||
var setData = function () { | ||
resolve(Array.prototype.slice.call(arguments, 0)); | ||
}; | ||
component.listenTo(store, setData, setData); | ||
}); | ||
} | ||
it('should get default data from getDefaultData()', function () { | ||
store = Reflux.createStore({ | ||
getDefaultData: function () { | ||
return 'default data'; | ||
} | ||
}); | ||
mountComponent(); | ||
return assert.eventually.equal(promise, 'default data'); | ||
}); | ||
it('should get default data from getDefaultData() returned promise', function () { | ||
store = Reflux.createStore({ | ||
getDefaultData: function () { | ||
return Q.Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve('default data'); | ||
}, 20); | ||
}); | ||
} | ||
}); | ||
mountComponent(); | ||
return assert.eventually.equal(promise, 'default data'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71949
25
1437
321