Skip to content

Instantly share code, notes, and snippets.

@quicksnap
Created July 28, 2015 21:52
Show Gist options
  • Select an option

  • Save quicksnap/7b3d1a5356b92fd0161f to your computer and use it in GitHub Desktop.

Select an option

Save quicksnap/7b3d1a5356b92fd0161f to your computer and use it in GitHub Desktop.
/**
* Sets up globals necessary for React to render without a real browser.
*/
var jsdom = require('jsdom').jsdom;
global.document = jsdom('<html><body></body></html>');
global.window = global.document.defaultView;
import test from 'tape';
import React from 'react/addons';
import Example from '../../src/components/ExampleComponent';
import { EXCLAIM_PHRASE } from '../../src/constants/actionTypes';
var TestUtils = React.addons.TestUtils;
// A simple mock to record the dispatched action
var dispatchResult = null;
function mockedDispatch(action) {
dispatchResult = action;
}
/**
* Our tested component setup
*
* @param {String} state The initial state supplied to `phrase` prop
* @return {ReactComponent}
*/
function setup(state = '') {
return TestUtils.renderIntoDocument(
<Example dispatch={mockedDispatch} phrase={state} />
);
}
test('Dispatches correct event when clicked', t => {
var renderedComponent = setup();
var exclaimBtnComponent = TestUtils.findRenderedDOMComponentWithClass(
renderedComponent,
'exclaimBtn'
);
TestUtils.Simulate.click(exclaimBtnComponent);
t.equal(
dispatchResult.type,
EXCLAIM_PHRASE,
'Did not dispatch action with correct type'
);
t.end();
});
test('Updates input correctly when state is altered', t => {
const TEST_PHRASE = 'some phrase';
var renderedComponent = setup(TEST_PHRASE);
var input = TestUtils.findRenderedDOMComponentWithTag(
renderedComponent,
'input'
);
// We could aslo use `findDOMNode` to get the actual DOM node, but
// testing this is fine IMO.
t.equal(input.props.value, TEST_PHRASE, 'You fucked up');
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment