Skip to content

Commit

Permalink
Completed tests fo SingleCheckbox
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamZarger committed Aug 11, 2017
1 parent 137d4ba commit e722dbf
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
64 changes: 59 additions & 5 deletions src/Filters/__tests__/SingleCheckBox.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import ReduxSingleCheckbox, { SingleCheckbox, mapDispatchToProps } from '../SingleCheckbox'
import React from 'react'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'
import SingleCheckbox from '../SingleCheckbox'
import renderer from 'react-test-renderer'
import { shallow } from 'enzyme'
import thunk from 'redux-thunk'

function setupSnapshot() {
function setupEnzyme(initialProps={}) {
const props = Object.assign({
changeFlagFilter: jest.fn(),
fieldName: 'has_narrative',
isChecked: true
}, initialProps)

const target = shallow(<SingleCheckbox {...props} />);

return {
props,
target
}
}

function setupSnapshot(query={}) {
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
const store = mockStore({
query: {},
aggs: {}
query
})

return renderer.create(
<Provider store={store}>
<SingleCheckbox />
<ReduxSingleCheckbox fieldName="has_narrative" />
</Provider>
)
}
Expand All @@ -26,5 +41,44 @@ describe('initial state', () => {
const tree = target.toJSON()
expect(tree).toMatchSnapshot()
});

it('pre-check filter based on query', () => {
const target = setupSnapshot({
has_narrative: true
})
const tree = target.toJSON()
expect(tree).toMatchSnapshot()
})
});

describe('component::SingleCheckbox', () => {
describe('componentWillReceiveProps', () => {
it('does not trigger a new update', () => {
const {target, props} = setupEnzyme()
target.setProps({ has_narrative: true })
expect(props.changeFlagFilter).not.toHaveBeenCalled()
})
})

describe('flag filter changed', () => {
it('triggers an update when checkbox is clicked', () => {
const { target, props } = setupEnzyme()
const input = target.find('input[id="theCheckbox"]')

input.simulate('click')
const actual = props.changeFlagFilter.mock.calls[0]

expect(actual[0]).toEqual('has_narrative')
expect(actual[1]).toEqual(false)
})
})

describe('mapDispatchToProps', () => {
it('hooks into changeFlagFilter', () => {
const dispatch = jest.fn()
const props = {fieldName: 'qaz'}
mapDispatchToProps(dispatch, props).changeFlagFilter('foo', 'bar')
expect(dispatch.mock.calls.length).toEqual(1)
})
})
})
24 changes: 23 additions & 1 deletion src/Filters/__tests__/__snapshots__/SingleCheckBox.spec.jsx.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
exports[`initial state pre-check filter based on query 1`] = `
<section
className="single-checkbox">
<h5 />
<div
className="m-form-field m-form-field__checkbox">
<input
checked={true}
className="a-checkbox"
id="theCheckbox"
onClick={[Function]}
type="checkbox"
value="has_narrative" />
<label
className="a-label"
htmlFor="theCheckbox">
Yes
</label>
</div>
</section>
`;

exports[`initial state renders without crashing 1`] = `
<section
className="single-checkbox">
Expand All @@ -10,7 +32,7 @@ exports[`initial state renders without crashing 1`] = `
id="theCheckbox"
onClick={[Function]}
type="checkbox"
value={undefined} />
value="has_narrative" />
<label
className="a-label"
htmlFor="theCheckbox">
Expand Down

0 comments on commit e722dbf

Please sign in to comment.