Created
August 29, 2018 07:51
-
-
Save Aziaev/fb3120cd5261c040f5e83c2bb40a5eb1 to your computer and use it in GitHub Desktop.
mapDispatchToProps functions invoked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-------------------Tested component------------------- | |
// All required imports | |
export class App extends React.Component<Props> { | |
render() { | |
const { onClick } = this.props; | |
return ( | |
<> | |
<h1>Form</h1> | |
<input /> | |
<button onClick={() => onClick()} /> | |
</> | |
); | |
} | |
} | |
const mapStateToProps = (state) => { | |
return { | |
state | |
}; | |
}; | |
const mapDispatchToProps = (dispatch) => { | |
return { | |
onClick: () => dispatch(actions.onClick()) | |
}; | |
}; | |
export default connect(mapStateToProps, mapDispatchToProps)(App); | |
-------------------Test example------------------- | |
import { configure, mount } from 'enzyme'; | |
import Adapter from 'enzyme-adapter-react-16/build/index'; | |
import jsdom from 'jsdom'; | |
import React from 'react'; | |
import { Provider } from 'react-redux'; | |
import configureMockStore from 'redux-mock-store'; | |
import ConnectedApp, { App } from './App'; | |
function setUpDomEnvironment() { | |
const { JSDOM } = jsdom; | |
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/' }); | |
const { window } = dom; | |
global.window = window; | |
global.document = window.document; | |
global.navigator = { | |
userAgent: 'node.js', | |
}; | |
copyProps(window, global); | |
} | |
function copyProps(src, target) { | |
const props = Object.getOwnPropertyNames(src) | |
.filter(prop => typeof target[prop] === 'undefined') | |
.map(prop => Object.getOwnPropertyDescriptor(src, prop)); | |
Object.defineProperties(target, props); | |
} | |
setUpDomEnvironment(); | |
configure({ adapter: new Adapter() }); | |
const mockStore = configureMockStore(); | |
describe('App', () => { | |
describe('When App connected to store', () => { | |
describe('When button clicked', () => { | |
it('should not crush after click on login button', () => { | |
const onClick = jest.fn() | |
const store = mockStore(initialStates[1]); | |
const wrapper = mount( | |
<Provider store={store}> | |
<ConnectedApp /> | |
</Provider>); | |
wrapper.find('button').simulate('click'); | |
??? how to test that function passed from mapDispatchToProps was fired? | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment