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

Check all arguments sent to action() for event objects #132

Merged
merged 2 commits into from
Apr 21, 2016
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
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