[web] Add tests for js/components/FlowView/Nav.jsx

This commit is contained in:
Matthew Shao 2017-05-30 21:46:17 +08:00
parent 248dde2604
commit f840d018e5
3 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,38 @@
import React from 'react'
import renderer from 'react-test-renderer'
import Nav, { NavAction } from '../../../components/FlowView/Nav'
describe('Nav Component', () => {
let tabs = ['foo', 'bar'],
onSelectTab = jest.fn(),
nav = renderer.create(<Nav active='foo' tabs={tabs} onSelectTab={onSelectTab}/>),
tree = nav.toJSON()
it('should render correctly', () => {
expect(tree).toMatchSnapshot()
})
it('should handle click', () => {
let mockEvent = { preventDefault: jest.fn() }
tree.children[0].props.onClick(mockEvent)
expect(mockEvent.preventDefault).toBeCalled()
expect(onSelectTab).toBeCalledWith('foo')
})
})
describe('NavAction Component', () => {
let clickFn = jest.fn(),
navAction = renderer.create(<NavAction icon="foo" title="bar" onClick={clickFn}/>),
tree = navAction.toJSON()
it('should render correctly', () => {
expect(tree).toMatchSnapshot()
})
it('should handle click', () => {
let mockEvent = { preventDefault: jest.fn() }
tree.props.onClick(mockEvent)
expect(mockEvent.preventDefault).toBeCalled()
expect(clickFn).toBeCalledWith(mockEvent)
})
})

View File

@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Nav Component should render correctly 1`] = `
<nav
className="nav-tabs nav-tabs-sm"
>
<a
className="active"
href="#"
onClick={[Function]}
>
Foo
</a>
<a
className=""
href="#"
onClick={[Function]}
>
Bar
</a>
</nav>
`;
exports[`NavAction Component should render correctly 1`] = `
<a
className="nav-action"
href="#"
onClick={[Function]}
title="bar"
>
<i
className="fa fa-fw foo"
/>
</a>
`;

View File

@ -2,6 +2,7 @@ import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import classnames from 'classnames'
import _ from 'lodash'
NavAction.propTypes = {
icon: PropTypes.string.isRequired,
@ -9,7 +10,7 @@ NavAction.propTypes = {
onClick: PropTypes.func.isRequired,
}
function NavAction({ icon, title, onClick }) {
export function NavAction({ icon, title, onClick }) {
return (
<a title={title}
href="#"