Skip to content

Commit

Permalink
Use legit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zalmoxisus committed Oct 15, 2015
1 parent a503d3f commit 26ec367
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 98 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"gulp-rename": "^1.2.2",
"gulp-util": "^3.0.5",
"gulp-zip": "^3.0.2",
"legit-tests": "^0.5.2",
"mocha": "^2.2.5",
"phantomjs": "^1.9.17",
"raw-loader": "^0.5.1",
Expand Down
74 changes: 27 additions & 47 deletions test/app/components/Counter.spec.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
import React from 'react';
import expect from 'expect';
import jsdomReact from '../jsdomReact';
import React from 'react/addons';
import Test from 'legit-tests';
import { clickButton } from '../testMixins';
import Counter from '../../../src/app/components/Counter';

const { TestUtils } = React.addons;

function setup() {
const actions = {
increment: expect.createSpy(),
incrementIfOdd: expect.createSpy(),
incrementAsync: expect.createSpy(),
decrement: expect.createSpy(),
bg: str => {}
};
const component = TestUtils.renderIntoDocument(<Counter counter={1} state={{ counter: { count: 1 } }} {...actions} />);
return {
component: component,
actions: actions,
buttons: TestUtils.scryRenderedDOMComponentsWithTag(component, 'button').map(button => {
return button.getDOMNode();
}),
p: TestUtils.findRenderedDOMComponentWithTag(component, 'p').getDOMNode()
};
}
const props = {
state: { counter: { count: 1 } },
increment: expect.createSpy(),
decrement: expect.createSpy(),
incrementIfOdd: expect.createSpy(),
incrementAsync: expect.createSpy(),
bg: expect.createSpy()
};

describe('Counter component', () => {
jsdomReact();

it('should display count', () => {
const { p } = setup();
expect(p.textContent).toMatch(/^Clicked: 1 times/);
});

it('first button should call increment', () => {
const { buttons, actions } = setup();
TestUtils.Simulate.click(buttons[0]);
expect(actions.increment).toHaveBeenCalled();
Test(<Counter {...props} />)
.find('p')
.renderToString(p => {
expect(p).toMatch(/Clicked: 1 times/);
});
});

it('second button should call decrement', () => {
const { buttons, actions } = setup();
TestUtils.Simulate.click(buttons[1]);
expect(actions.decrement).toHaveBeenCalled();
});
['increment', 'decrement', 'incrementIfOdd', 'incrementAsync', 'bg']
.forEach((toBeCalled, idx) => {
it('first button should call ' + toBeCalled, () => {
Test(<Counter {...props} />)
.mixin({ clickButton: clickButton })
.clickButton(idx)
.test(() => {
expect(props[toBeCalled]).toHaveBeenCalled();
});
});
});

it('third button should call incrementIfOdd', () => {
const { buttons, actions } = setup();
TestUtils.Simulate.click(buttons[2]);
expect(actions.incrementIfOdd).toHaveBeenCalled();
});

it('fourth button should call incrementAsync', () => {
const { buttons, actions } = setup();
TestUtils.Simulate.click(buttons[3]);
expect(actions.incrementAsync).toHaveBeenCalled();
});
});
69 changes: 25 additions & 44 deletions test/app/containers/App.spec.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,46 @@
import React from 'react';
import expect from 'expect';
import jsdomReact from '../jsdomReact';
import React from 'react/addons';
import Test from 'legit-tests';
import { clickButton } from '../testMixins';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import App from '../../../src/app/containers/Root';
// import configureStore from '../../../src/app/store/configureStore';
import counter from '../../../src/app/reducers/counter';

const { TestUtils } = React.addons;

function setup(initialState) {
// const store = configureStore(initialState);
function configureStore(initialState) {
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(combineReducers({ counter }), initialState);

const app = TestUtils.renderIntoDocument(
<Provider store={store}>
{() => <App />}
</Provider>
);
return {
app: app,
buttons: TestUtils.scryRenderedDOMComponentsWithTag(app, 'button').map(button => {
return button.getDOMNode();
}),
p: TestUtils.findRenderedDOMComponentWithTag(app, 'p').getDOMNode()
};
return createStoreWithMiddleware(combineReducers({counter}), initialState);
}

describe('containers', () => {
jsdomReact();

describe('App', () => {
it('should display initial count', () => {
const { p } = setup();
expect(p.textContent).toMatch(/^Clicked: 0 times/);
});

it('should display updated count after increment button click', () => {
const { buttons, p } = setup();
TestUtils.Simulate.click(buttons[0]);
expect(p.textContent).toMatch(/^Clicked: 1 times/);
Test(<Provider store={ configureStore() }>{ () => <App /> }</Provider>)
.find('p')
.renderToString(string => {
expect(string).toMatch(/Clicked: 0 times/);
});
});

it('should display updated count after decrement button click', () => {
const { buttons, p } = setup();
TestUtils.Simulate.click(buttons[1]);
expect(p.textContent).toMatch(/^Clicked: -1 times/);
[
{ title: 'should display updated count after increment button click', result: /Clicked: 1 times/ },
{ title: 'should display updated count after decrement button click', result: /Clicked: -1 times/ },
{ title: 'shouldnt change if even and if odd button clicked', result: /Clicked: 0 times/ },
{ idx: 2, title: 'should change if odd and if odd button clicked', value: { counter: { count: 1 } }, result: /Clicked: 2 times/ }
]
.forEach((rule, idx) => {
it(rule.title, () => {
Test(<Provider store={ configureStore(rule.value) }>{ () => <App /> }</Provider>)
.mixin({clickButton: clickButton})
.clickButton(rule.idx || idx)
.renderToString(string => {
expect(string).toMatch(rule.result);
});
});
});

it('shouldnt change if even and if odd button clicked', () => {
const { buttons, p } = setup();
TestUtils.Simulate.click(buttons[2]);
expect(p.textContent).toMatch(/^Clicked: 0 times/);
});

it('should change if odd and if odd button clicked', () => {
const { buttons, p } = setup({ counter: { count: 1 } });
TestUtils.Simulate.click(buttons[2]);
expect(p.textContent).toMatch(/^Clicked: 2 times/);
});
});
});
7 changes: 0 additions & 7 deletions test/app/jsdomReact.js

This file was deleted.

7 changes: 7 additions & 0 deletions test/app/testMixins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Find, Simulate } from 'legit-tests/middleware';

export function clickButton(idx) {
Find.call(this, 'button');
this.elements = { button: this.elements.button[idx] };
Simulate.call(this, {method: 'click', element: 'button'});
}

0 comments on commit 26ec367

Please sign in to comment.