Skip to content

Commit

Permalink
a new actions
Browse files Browse the repository at this point in the history
  • Loading branch information
evheniy committed Jun 28, 2018
1 parent 35faaf4 commit 6216145
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 2 deletions.
15 changes: 14 additions & 1 deletion docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ With payload:
title: 'text',
}
```
You can put many parameters:

The same result you can get using addFormElementAction:

```javascript
rl.addParamAction('title', '');
```

**You can put many parameters**:

```javascript
rl.addAction('clear', { title: '', body: '' }, { asParams: ['title', 'body'] });
Expand Down Expand Up @@ -165,6 +172,12 @@ With payload:
}
```

The same result you can get using addFormElementAction:

```javascript
rl.addParamsAction('clear', { title: '', body: '' });
```

## asParams with isFormElement

You can use **asParams** option with **isFormElement**:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-lazy",
"version": "0.3.1",
"version": "0.4.0",
"description": "redux-lazy",
"main": "lib/index.js",
"files": [
Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ class RL {
this.addAction(name, {}, { isEvent: true });
}

addParamAction(name, defaultValue = null) {
this.addAction(
name,
{ [name]: defaultValue },
{ asParams: name },
);
}

addParamsAction(name, payload = {}) {
this.addAction(
name,
payload,
{ asParams: Object.keys(payload) },
);
}

getNSKey() {
return `@@${this.ns}`;
}
Expand Down
162 changes: 162 additions & 0 deletions tests/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,5 +693,167 @@ describe('Testing actions', () => {

expect(isError).to.be.equal(true);
});

it('should test addParamAction', () => {
const newRl = new RL('post');

const defaultValue = 'defaultValue';
const value = 'value';

newRl.addParamAction('test', defaultValue);

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction(value);

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);
expect(action.test).to.be.equal(value);

expect(action).to.be.eql({
test: value,
type: newRlTypes.POST_TEST,
});
});

it('should test addParamAction without value', () => {
const newRl = new RL('post');

const defaultValue = 'defaultValue';

newRl.addParamAction('test', defaultValue);

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction();

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);
expect(action.test).to.be.equal(defaultValue);

expect(action).to.be.eql({
test: defaultValue,
type: newRlTypes.POST_TEST,
});
});

it('should test addParamAction without value and defaultValue', () => {
const newRl = new RL('post');

newRl.addParamAction('test');

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction();

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);
expect(action.test).to.be.equal(null);

expect(action).to.be.eql({
test: null,
type: newRlTypes.POST_TEST,
});
});

it('should test addParamsAction', () => {
const newRl = new RL('post');

const payload = {
field1: 'field1',
field2: 'field2',
};

newRl.addParamsAction('test', payload);

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction(1, 2);

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);
expect(action.field1).to.be.equal(1);
expect(action.field2).to.be.equal(2);

expect(action).to.be.eql({
field1: 1,
field2: 2,
type: newRlTypes.POST_TEST,
});
});

it('should test addParamsAction with only first value', () => {
const newRl = new RL('post');

const payload = {
field1: 'field1',
field2: 'field2',
};

newRl.addParamsAction('test', payload);

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction(1);

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);
expect(action.field1).to.be.equal(1);
expect(action.field2).to.be.equal('field2');

expect(action).to.be.eql({
field1: 1,
field2: 'field2',
type: newRlTypes.POST_TEST,
});
});

it('should test addParamsAction without values', () => {
const newRl = new RL('post');

const payload = {
field1: 'field1',
field2: 'field2',
};

newRl.addParamsAction('test', payload);

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction();

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);
expect(action.field1).to.be.equal('field1');
expect(action.field2).to.be.equal('field2');

expect(action).to.be.eql({
field1: 'field1',
field2: 'field2',
type: newRlTypes.POST_TEST,
});
});

it('should test addParamsAction with empty payload', () => {
const newRl = new RL('post');

newRl.addParamsAction('test');

const { types: newRlTypes, actions: newRlActions } = newRl.flush();

const action = newRlActions.testAction();

expect(action).to.be.a('object');

expect(action.type).to.be.equal(newRlTypes.POST_TEST);

expect(action).to.be.eql({ type: newRlTypes.POST_TEST });
});
});
});

0 comments on commit 6216145

Please sign in to comment.