-
-
Save oieduardorabelo/e9537037a6b696e9173eb547078d391e to your computer and use it in GitHub Desktop.
Testando com Jest: Dica #4
This file contains hidden or 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
import renderer from 'react-test-renderer' | |
import Header from '../Header' | |
import store from '../store' | |
jest.mock('react-redux-loading-bar', () => { | |
const ReactReduxLoadingBar = require.requireActual('react-redux-loading-bar') | |
return { | |
// Operação "spread" do módulo original | |
...ReactReduxLoadingBar, | |
// Mockamos apenas o componente React | |
default: 'LoadingBar' | |
} | |
}) | |
// Sobrescrevendo o método dispatch da nossa store para mockar a função que | |
// nos permite ver qual argumento foi usando na chamada da função | |
store.dispatch = jest.fn() | |
it('should render correctly and dispatch showLoading during mount', () => { | |
// Ao invés de envolver em um <Provider> nós podemos passar a store diretamente como prop 😏 | |
const component = renderer.create(<Header store={store} />) | |
expect(component.toJSON()).toMatchSnapshot() | |
expect(store.dispatch.mock.calls).toMatchSnapshot() | |
}) |
This file contains hidden or 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
import { Component } from 'react' | |
import { default as LoadingBar, showLoading } from 'react-redux-loading-bar' | |
import { connect } from 'react-redux' | |
class Header extends Component { | |
componentDidMount() { | |
this.props.dispatch(showLoading()) | |
} | |
render() { | |
return ( | |
<header> | |
<LoadingBar /> | |
</header> | |
) | |
} | |
} | |
export default connect()(Header) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment