Skip to content

Commit

Permalink
Update helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tmashuang committed Jan 8, 2020
1 parent e3ad54b commit 01f2c2f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 18 deletions.
3 changes: 3 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ require('jsdom-global')()
// localStorage
window.localStorage = {}

// override metamask-logo
window.requestAnimationFrame = () => {}

// crypto.getRandomValues
if (!window.crypto) {
window.crypto = {}
Expand Down
105 changes: 87 additions & 18 deletions test/lib/render-helpers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const { shallow, mount } = require('enzyme')
import sinon from 'sinon'
import React from 'react'
import { BrowserRouter } from 'react-router-dom'
import { shape } from 'prop-types'
import { MemoryRouter } from 'react-router-dom'
import PropTypes from 'prop-types'

module.exports = {
shallowWithStore,
mountWithStore,
mountWithRouter,
}

function shallowWithStore (component, store) {
const context = {
store,
}
return shallow(component, { context })
shallowWithRouter,
stubComponent,
}

function mountWithStore (component, store) {
Expand All @@ -23,27 +18,101 @@ function mountWithStore (component, store) {
return mount(component, { context })
}

function mountWithRouter (node) {
function mountWithRouter (component, store, pathname) {

// Instantiate router context
const router = {
history: new BrowserRouter().history,
history: new MemoryRouter().history,
route: {
location: {},
location: {
pathname: pathname || '/',
},
match: {},
},
}

const createContext = () => ({
context: { router, t: () => {} },
childContextTypes: { router: shape({}), t: () => {} },
context: {
router,
t: str => str,
tOrKey: str => str,
metricsEvent: () => {},
store,
},
childContextTypes: {
router: PropTypes.object,
t: PropTypes.func,
tOrKey: PropTypes.func,
metricsEvent: PropTypes.func,
store: PropTypes.object,
},
})

const Wrapper = () => (
<BrowserRouter>
{node}
</BrowserRouter>
<MemoryRouter initialEntries={[{ pathname }]} initialIndex={0}>
{component}
</MemoryRouter>
)

return mount(<Wrapper />, createContext())
}

function shallowWithRouter (component, store, pathname) {

// Instantiate router context
const router = {
history: new MemoryRouter().history,
route: {
location: {
pathname: pathname || '/',
},
match: {},
},
}

const createContext = () => ({
context: {
router,
t: str => str,
tOrKey: str => str,
metricsEvent: () => {},
store,
},
childContextTypes: {
router: PropTypes.object,
t: PropTypes.func,
tOrKey: PropTypes.func,
metricsEvent: PropTypes.func,
store: PropTypes.object,
},
})

const Wrapper = () => (
<MemoryRouter initialEntries={[{ pathname }]} initialIndex={0}>
{component}
</MemoryRouter>
)

return shallow(<Wrapper />, createContext())
}

function stubComponent (componentClass) {

const lifecycleMethods = [
'render',
'componentWillMount',
'componentDidMount',
'componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'componentDidUpdate',
'componentWillUnmount',
]

lifecycleMethods.forEach((method) => {
if (typeof componentClass.prototype[method] !== 'undefined') {
sinon.stub(componentClass.prototype, method).returns(null)
}
})

}

0 comments on commit 01f2c2f

Please sign in to comment.