Skip to content

Instantly share code, notes, and snippets.

@FQ400
Last active April 4, 2017 09:22
Show Gist options
  • Save FQ400/d6017d23436684594d0e77df2584e21a to your computer and use it in GitHub Desktop.
Save FQ400/d6017d23436684594d0e77df2584e21a to your computer and use it in GitHub Desktop.
Inferno Component Unit-Test
import { linkEvent } from 'inferno';
import {
compose,
withState
} from 'incompose';
import classnames from 'classnames';
const toggle = (props) => props.toggle(!props.hidden);
const ExpandableBlock = (props) => {
const className = classnames('expandable-block', props.className);
return (
<div className={className}>
<a href='#' onClick={linkEvent(props, toggle)}>Expand</a>
<div className='expandable-block-content' hidden={props.hidden}>
{props.children}
</div>
</div>
);
};
const withHiddenState = withState('hidden', 'toggle', true);
export default compose(
withHiddenState,
)(ExpandableBlock);
import {
renderIntoDocument,
findRenderedVNodeWithType,
findAllInRenderedTree,
findAllInVNodeTree
} from 'inferno-test-utils';
import ExpandableBlock from './expandable-block';
import { hasClass } from '../../lib/utils';
const clearDOM = () => document.body.innerHTML = null;
describe('ExpendableBlock Component', () => {
afterEach(clearDOM)
it('has an expand handler', () => {
const renderedTree = renderIntoDocument(<ExpandableBlock />);
const vNode = findRenderedVNodeWithType(renderedTree, 'a');
expect(vNode).toBeDefined();
});
it('shows the content after using expand', () => {
jest.useFakeTimers();
const renderedTree = renderIntoDocument(<ExpandableBlock><p>MY CONTENT</p></ExpandableBlock>);
const vNode = findRenderedVNodeWithType(renderedTree, 'a');
expect(vNode).toBeDefined();
vNode.dom.click();
jest.runAllTicks();
jest.runAllTimers();
const hasClassName = (vNode) => hasClass(vNode.className, 'expandable-block-content');
const result = findAllInRenderedTree(renderedTree, hasClassName);
expect(result).toHaveLength(1);
expect(result[0].props.hidden).toBe(false);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment