Skip to content

Commit

Permalink
added test for new setData + updated examples accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
justinjung04 committed Jul 5, 2016
1 parent f3c6db7 commit 9841a82
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions example/react-counter/components/counter-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import Treeful from '../../../src/treeful';

export default class CounterControl extends Component {
increment() {
Treeful.setData('count', Treeful.getData('count') + 1);
Treeful.setData('count', (e) => e + 1);
// Or you may use a helper function
// Treeful.incrementData('count');
}

decrement() {
Treeful.setData('count', Treeful.getData('count') - 1);
Treeful.setData('count', (e) => e - 1);
// Or you may use a helper function
// Treeful.decrementData('count');
}
Expand Down
15 changes: 7 additions & 8 deletions example/react-todo/components/todo-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import Treeful from '../../../src/treeful';

export default class TodoController extends Component {
addTodo() {
const data = Treeful.getData('todos');

data.push({
name: this.refs.name.value,
color: this.refs.color.value,
id: data.length + 1
Treeful.setData('todos', (e) => {
e.push({
name: this.refs.name.value,
color: this.refs.color.value,
id: e.length + 1
});
return e;
});

Treeful.setData('todos', data);

// Or you may use a helper function

// Treeful.pushData('todos', {
Expand Down
4 changes: 2 additions & 2 deletions example/vanilla-counter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Treeful.addNode('count', 0);
let unsubscribe;

const onClickInc = () => {
Treeful.setData('count', Treeful.getData('count') + 1);
Treeful.setData('count', (e) => e + 1);
// Or you may use a helper function
// Treeful.incrementData('count');
};

const onClickDec = () => {
Treeful.setData('count', Treeful.getData('count') - 1);
Treeful.setData('count', (e) => e - 1);
// Or you may use a helper function
// Treeful.decrementData('count');
};
Expand Down
6 changes: 6 additions & 0 deletions src/treeful.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ class Treeful {
}
};

const checkIfDataIsFunction = (data) => {
if(isType(data, 'function')) {
throw new TypeError('Data cannot be a function.');
}
};

const checkDataType = (data, type) => {
if(!isType(data, type)) {
throw new TypeError('Data type must be a(n) ' + type + '.');
Expand Down
9 changes: 9 additions & 0 deletions test/treeful.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ describe('treeful', () => {
Treeful.destroy();
});

it('accepts function and sets data of a node', () => {
Treeful.addNode('1', 1);
Treeful.setData('1', (e) => {
return e + 1;
});
expect(Treeful.getData('1')).toEqual(2);
Treeful.destroy();
});

/** subscribe **/

it('calls callback functions when a node\'s data is changed, and passes data', () => {
Expand Down

0 comments on commit 9841a82

Please sign in to comment.