I've been struggling for a little while now with accessing a variable that is inside the constructor of a component, to test different methods in which it's being used. See example below.
The constructor of my component Timer. I want to access countDown within my timer.test.js file.
constructor(props){
super(props)
countDown = null,
...
}
Below is one of the function I want to test (in which coutDown is used) :
pauseTimer(){
this.setState({timerIsActive : false})
clearInterval(countDown)
}
Below is an example of my timer.test.js file :
import React from 'react'
import renderer from 'react-test-renderer'
import {shallow} from 'enzyme'
const wrapper = shallow(<Timer/>)
const componentInstance = wrapper.instance()
describe('My timer ', () => {
it('Shallow rendering', () => {
expect(wrapper).toMatchSnapshot()
})
it('Should update state customeTimeValue to 20', ()=>{
componentInstance.setState({...componentInstance.state, customTimeValue : {focus : '20'}})
expect(componentInstance.state.customTimeValue.focus).toEqual('20')
})
it('isValueOutOfRange with 20 should return 20', ()=>{
componentInstance.setState({...componentInstance.state, customTimeValue : {focus : '20'}})
expect(componentInstance.isValueOutOfRange('focus')).toEqual(20)
})
it('isValueOutOfRange with 67 should return 59', ()=>{
componentInstance.setState({...componentInstance.state, customTimeValue : {focus : '67'}})
expect(componentInstance.isValueOutOfRange('focus')).toEqual(59)
I have already searched on multiple posts and docs but none of these are what I am looking for. I've tried different things on my own but with no results.
I hope you guys will be able to help me. Thanks !