Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES7 stores #3

Merged
merged 3 commits into from
May 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"<="
],
"excludeFiles": [
"dist/*"
"dist/*",
"src/store/__tests__/handlesSpec.js",
"src/store/__tests__/storeClassProperties.js"
],
"maximumLineLength": {
"value": 120,
Expand Down
4 changes: 3 additions & 1 deletion .jshintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist/*
dist/*
src/store/__tests__/handlesSpec.js
src/store/__tests__/storeClassProperties.js
10 changes: 9 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var fs = require('fs');
var _ = require('lodash');
var yaml = require('js-yaml');
var shell = require('shelljs');
var babelify = require('babelify');

var SCRIPTS = [
'./src/http-state-source/__tests__/lib/mockServer.js'
Expand Down Expand Up @@ -119,7 +120,14 @@ module.exports = function (config) {
browserNoActivityTimeout: 100000,
browserify: {
bare: true,
debug: true
debug: true,
configure: function (bundle) {
bundle.transform(babelify.configure({
optional: ['es7.decorators', 'es7.classProperties']
}));

bundle.transform('envify');
}
},
files: [
'test/browser/setup.js',
Expand Down
32 changes: 32 additions & 0 deletions src/store/__tests__/handlesSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let _ = require('lodash');
let sinon = require('sinon');
let expect = require('chai').expect;
let { dispatch } = require('../../test-utils');
let buildMarty = require('../../../test/lib/buildMarty');

describe('@handles', () => {
let app, Marty, handler;

beforeEach(() => {
Marty = buildMarty();
handler = sinon.stub();

class UserStore extends Marty.Store {
@Marty.handles('FOO', 'BAR')
actionHandler(foo, bar) {
handler(foo, bar);
}
}

app = new Marty.Application();
app.register('store', UserStore);

dispatch(app, 'FOO', 1, 2);
dispatch(app, 'BAR', 'a', 'b');
});

it('should handle the dispatched actions', () => {
expect(handler).to.be.calledWith(1, 2);
expect(handler).to.be.calledWith('a', 'b');
});
});
64 changes: 64 additions & 0 deletions src/store/__tests__/storeClassProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
let _ = require('lodash');
let sinon = require('sinon');
let expect = require('chai').expect;
let { dispatch } = require('../../test-utils');
let buildMarty = require('../../../test/lib/buildMarty');

describe('Store statics', () => {
let app, Marty, handler;

beforeEach(() => {
Marty = buildMarty();
handler = sinon.stub();
});

describe('when class properties', () => {
beforeEach(() => {
class UserStore extends Marty.Store {
handlers = {
actionHandler: ['FOO', 'BAR']
}

actionHandler(foo, bar) {
handler(foo, bar);
}
}

app = new Marty.Application();
app.register('store', UserStore);

dispatch(app, 'FOO', 1, 2);
dispatch(app, 'BAR', 'a', 'b');
});

it('should handle the dispatched actions', () => {
expect(handler).to.be.calledWith(1, 2);
expect(handler).to.be.calledWith('a', 'b');
});
});

describe('when static properties', () => {
beforeEach(() => {
class UserStore extends Marty.Store {
actionHandler(foo, bar) {
handler(foo, bar);
}
}

UserStore.handlers = {
actionHandler: ['FOO', 'BAR']
};

app = new Marty.Application();
app.register('store', UserStore);

dispatch(app, 'FOO', 1, 2);
dispatch(app, 'BAR', 'a', 'b');
});

it('should handle the dispatched actions', () => {
expect(handler).to.be.calledWith(1, 2);
expect(handler).to.be.calledWith('a', 'b');
});
});
});
7 changes: 7 additions & 0 deletions src/store/getHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let { extend } = require('../mindash');

function getHandlers(store) {
return extend({}, store.handlers, store.constructor.handlers);
}

module.exports = getHandlers;
3 changes: 2 additions & 1 deletion src/store/handleAction.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
let _ = require('../mindash');
let getHandlers = require('./getHandlers');

function handleAction(action) {
this.__validateHandlers();

let store = this;
let handlers = _.object(_.map(store.handlers, getHandlerWithPredicates));
let handlers = _.object(_.map(getHandlers(store), getHandlerWithPredicates));

_.each(handlers, function (predicates, handlerName) {
_.each(predicates, function (predicate) {
Expand Down
15 changes: 15 additions & 0 deletions src/store/handles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let { toArray, extend } = require('../mindash');

function handles() {
let constants = toArray(arguments);

return function (target, name, descriptor) {
target.handlers = extend({}, target.handlers, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - no need to muck with hasOwnProperty or whatever in this case like for Application#register!

At some point later on it might be possible to use this to hide the handler, though it'd require changing how Store#handlers looks.

[name]: constants
});

return descriptor;
};
}

module.exports = handles;
1 change: 1 addition & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = function (marty) {
marty.when = require('./when');
marty.fetch = require('./fetch');
marty.Store = require('./store');
marty.handles = require('./handles');
marty.createStore = require('./createStoreClass');
};
3 changes: 2 additions & 1 deletion src/store/validateHandlers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
let _ = require('../mindash');
let getHandlers = require('./getHandlers');
let ActionHandlerNotFoundError = require('../errors/actionHandlerNotFoundError');
let ActionPredicateUndefinedError = require('../errors/actionPredicateUndefinedError');

function validateHandlers(store) {
_.each(store.handlers, (actionPredicate, handlerName) => {
_.each(getHandlers(store), (actionPredicate, handlerName) => {
let actionHandler = store[handlerName];

if (_.isUndefined(actionHandler) || _.isNull(actionHandler)) {
Expand Down
47 changes: 47 additions & 0 deletions src/test-utils/createApplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var _ = require('../mindash');
var DEFAULT_OPTIONS = {
include: [],
exclude: [],
stub: {}
};

function createApplication(Application, options) {
var {
include,
exclude,
stub
} = _.defaults(options || {}, DEFAULT_OPTIONS);

// Inherit from application so we modify prototype
class TestApplication extends Application { }

var _register = TestApplication.prototype.register;

if (!_.isArray(include)) {
include = [include];
}

if (!_.isArray(exclude)) {
exclude = [exclude];
}

TestApplication.prototype.register = function stubRegister(key, value) {
if (!_.isString(key)) {
_register.apply(this, arguments);
} else if (stub[key]) {
this[key] = stub[key];
} else if (include.length) {
if (include.indexOf(key) !== -1) {
_register.call(this, key, value);
}
} else if (exclude.length) {
if (include.indexOf(key) === -1) {
_register.call(this, key, value);
}
}
};

return new TestApplication();
}

module.exports = createApplication;
16 changes: 16 additions & 0 deletions src/test-utils/dispatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function dispatch(app, type, ...args) {
if (!app) {
throw new Error('Must specify the application');
}

if (!type) {
throw new Error('Must specify the action type');
}

return app.dispatcher.dispatchAction({
type: type,
arguments: args
});
}

module.exports = dispatch;
4 changes: 4 additions & 0 deletions src/test-utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
dispatch: require('./dispatch'),
createApplication: require('./createApplication')
};