Skip to content

Instantly share code, notes, and snippets.

View stipsan's full-sized avatar

Cody Olsen stipsan

View GitHub Profile
@stipsan
stipsan / Sidebar-test.jsx
Last active December 19, 2023 12:43
Testing with Jest: Tip #5
import renderer from 'react-test-renderer'
import Sidebar from '../Sidebar'
jest.mock('react-router-dom', () => ({
Link: 'Link',
Route: ({ children, path }) => children({ match: path === '/somewhere' })
}))
it('should render correctly', () => {
const component = renderer.create(<Sidebar />)
@stipsan
stipsan / Select-test.jsx
Last active June 16, 2017 09:36
Testing with Jest: Tip #3
import renderer from 'react-test-renderer'
import Select from '../Select'
jest.mock('react-select', () => {
const { createElement } = require('react')
// Note that since 'react-select' is mocked we have to use
// require.requireActual, even inside the factory function that mocks it!
// Just watch the movie Inception and skip to the Dream within a Dream and you'll get it
const ReactSelect = require.requireActual('react-select')
@stipsan
stipsan / App-test.jsx
Last active June 16, 2017 09:44
Testing with Jest: Tip #6
import renderer from 'react-test-renderer'
import App from '../App'
jest.mock('react-router-dom', () => ({
Link: 'Link',
Route: ({ children, ...props }) =>
typeof children === 'function'
? children({ match: path === '/somewhere' })
: createElement('Route', props)
}))
@stipsan
stipsan / App-test.jsx
Last active June 16, 2017 09:44
Testing with Jest: Tip #7
import renderer from 'react-test-renderer'
import App from '../App'
// Generated snapshot is exactly like the previous example
// while it's possible to override the default mock
// by using the same jest.mock command as before
// if this test need a different behavior than the
// glboal mock.
it('should render correctly', () => {
@stipsan
stipsan / package.json
Last active August 10, 2018 17:43
Testing with Jest: Tip #8
{
"jest": {
"setupFiles": [
"<rootDir>/test-setup.js"
]
}
}
@stipsan
stipsan / package.json
Last active August 29, 2018 16:52
Testing with Jest: Tip #9
{
"jest": {
"setupFiles": [
"<rootDir>/test-setup.js"
]
}
}
@stipsan
stipsan / Input-test.jsx
Last active June 16, 2017 09:49
Testing with Jest: Tip #10
import renderer from 'react-test-renderer'
import Input from '../Input'
it('should render correctly', () => {
const component = renderer.create(<Input />)
expect(component.toJSON()).toMatchSnapshot()
// getInstance is returning the `this` object you have in your component
// meaning anything accessible on `this` inside your component
// can be accessed on getInstance, including props!
@stipsan
stipsan / Notifications-test.jsx
Last active June 16, 2017 09:51
Testing with Jest: Tip #11
import renderer from 'react-test-renderer'
import NotificationsContainer from '../NotificationsContainer'
// we can just pass through the component since we pass dispatch prop directly
jest.mock('react-redux', () => component => component)
it('should render correctly', () => {
const dispatch = jest.fn()
const component = renderer.create(
<NotificationsContainer dispatch={dispatch} />
@stipsan
stipsan / Form-test.jsx
Last active June 16, 2017 09:52
Testing with Jest: Tip #12
import renderer from 'react-test-renderer'
import Form, { validate } from '../Form'
jest.mock('redux-form', () => ({
Field: 'Field',
reduxForm: options => {
// Wrap the component and return the new component, just like the real hoc does
return Form => props => {
// call the validate error to make sure errors are detected
options.validate({}, props)
@stipsan
stipsan / Canvas-test.jsx
Last active June 17, 2017 18:07
Testing with Jest: Tip #13
import renderer from 'react-test-renderer'
import Canvas from '../Canvas'
it('should render correctly', () => {
const component = renderer.create(<Form x={0} y={0} />)
expect(component.toJSON()).toMatchSnapshot()
const instance = component.getInstance()
const spy = jest.spyOn(instance, 'calculateGrid')