Closed duro closed 6 years ago
Ok, I was able to figure this out, as it had to do with how snapshots work in a React + Jest world.
"refs" in snapshots are by default always null
, unless you make use of jest's createNodeMock
feature.
I was able to mock the ref node the way that react-textfit wants it (since it relies heavily on DOM specific APIs).
const createNodeMock = el => {
if (el.type === 'div') {
const mockEl = document.createElement('div')
mockEl.style.padding = '0'
Object.defineProperty(mockEl, 'clientHeight', {
get() {
return 100
},
enumerable: true,
configurable: true
})
Object.defineProperty(mockEl, 'clientWidth', {
get() {
return 300
},
enumerable: true,
configurable: true
})
return mockEl
}
return null
}
Full usage example:
import Component from './Component'
import renderer from 'react-test-renderer'
const createNodeMock = el => {
if (el.type === 'div') {
const mockEl = document.createElement('div')
mockEl.style.padding = '0'
Object.defineProperty(mockEl, 'clientHeight', {
get() {
return 100
},
enumerable: true,
configurable: true
})
Object.defineProperty(mockEl, 'clientWidth', {
get() {
return 300
},
enumerable: true,
configurable: true
})
return mockEl
}
return null
}
describe('CreateNodeMock testing ', () => {
it('mock node', () => {
const tree = renderer.create(<Component />, { createNodeMock }).toJSON()
expect(tree).toMatchSnapshot()
})
})
This does not seem to happen anywhere outside a test environment.
Full stack trace: