Skip to content

Commit

Permalink
Check all arguments sent to action() for event objects (#132)
Browse files Browse the repository at this point in the history
* Check for events passed to action() in all arguments, rather than just the first

* Add test case for new event serialization
  • Loading branch information
Ashley Blurton authored and arunoda committed Apr 21, 2016
1 parent af5970f commit 6a5d04f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
9 changes: 6 additions & 3 deletions dist/client/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ var ClientApi = function () {

// Remove events from the args. Otherwise, it creates a huge JSON string.

if (args[0] && typeof args[0].preventDefault === 'function') {
args[0] = '[SyntheticEvent]';
}
args = args.map(function (arg) {
if (typeof arg.preventDefault === 'function') {
return '[SyntheticEvent]';
}
return arg;
});

var id = ++idGenerator;
var data = { name: name, args: args };
Expand Down
32 changes: 32 additions & 0 deletions src/client/__tests__/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,38 @@ describe('client.ClientApi', () => {
count: 1,
}]);
});

it('should replace any Synthetic Event with it\'s name when not the first argument', () => {
const api = getClientApi();
api._syncedStore.getData = () => ({ actions: [] });
api._syncedStore.setData = sinon.stub();

const event = {
preventDefault() {},
};
const data = {
type: 'delete',
};

const cb = api.action('hello');
cb(data, event);

const args = api._syncedStore.setData.args[0];
const actions = clearActionId(args[0].actions);

expect(actions).to.be.deep.equal([{
data: {
name: 'hello',
args: [
{
type: 'delete',
},
'[SyntheticEvent]',
],
},
count: 1,
}]);
});
});

describe('linkTo', () => {
Expand Down
14 changes: 7 additions & 7 deletions src/client/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ export default class ClientApi {
const syncedStore = this._syncedStore;

return function (..._args) {
const args = Array.from(_args);
let args = Array.from(_args);
let { actions = [] } = syncedStore.getData();

// Remove events from the args. Otherwise, it creates a huge JSON string.
if (
args[0] &&
typeof args[0].preventDefault === 'function'
) {
args[0] = '[SyntheticEvent]';
}
args = args.map(arg => {
if (typeof arg.preventDefault === 'function') {
return '[SyntheticEvent]';
}
return arg;
});

const id = ++idGenerator;
const data = { name, args };
Expand Down

0 comments on commit 6a5d04f

Please sign in to comment.