Permalink
Browse files

test: adding unit tests

  • Loading branch information...
willmendesneto authored and jamiebuilds committed Oct 15, 2018
1 parent b426385 commit ef4de24d2e5ff15f847ff61ea36612587094e745
Showing with 41 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +27 −4 __tests__/unstated.js
  3. +13 −0 package.json
View
@@ -3,3 +3,4 @@ node_modules
lib
.cache
dist
coverage
View
@@ -1,12 +1,18 @@
// @flow
import React from 'react';
import assert from 'assert';
import renderer from 'react-test-renderer';
import { Provider, Subscribe, Container } from '../src/unstated';
function render(element) {
return renderer.create(element).toJSON();
}
async function click({ children = [] }, id) {
const el = children.find(({ props = {} }) => props.id === id);
el.props.onClick();
}
class CounterContainer extends Container<{ count: number }> {
state = { count: 0 };
increment(amount = 1) {
@@ -30,8 +36,12 @@ function Counter() {
{counter => (
<div>
<span>{counter.state.count}</span>
<button onClick={() => counter.decrement()}>-</button>
<button onClick={() => counter.increment()}>+</button>
<button id="decrement" onClick={() => counter.decrement()}>
-
</button>
<button id="increment" onClick={() => counter.increment()}>
+
</button>
</div>
)}
</Subscribe>
@@ -75,6 +85,19 @@ function CounterWithAmountApp() {
);
}
test('basic', () => {
// still too lazy
test('counter', async () => {
let counter = new CounterContainer();
let tree = render(
<Provider inject={[counter]}>
<Counter />
</Provider>
);
assert.equal(counter.state.count, 0);
await click(tree, 'increment');
assert.equal(counter.state.count, 1);
await click(tree, 'decrement');
assert.equal(counter.state.count, 0);
});
View
@@ -53,5 +53,18 @@
},
"lint-staged": {
"*.{js,json,md}": ["prettier --write", "git add"]
},
"jest": {
"collectCoverage": true,
"coverageDirectory": "coverage",
"collectCoverageFrom": ["src/**/*.js"],
"coverageThreshold": {
"global": {
"lines": 76,
"statements": 74,
"functions": 86,
"branches": 45
}
}
}
}

0 comments on commit ef4de24

Please sign in to comment.