vendredi 19 avril 2019

How Can I Test My Component's Methods When Using Connect() and PrefetchResourceContainer

I am trying to test for componentDidMount(). When I try to mount my component I receive a null instance. When I shallow render it, componentDidMount is never called because I can't mount it.

My Code:

class SingleOrderContainer extends Component {
  constructor(props) {
    super(props)
    this.toggle = this.toggle.bind(this)

    let initialOrderTab

    if(props.location && props.location.state) {
      initialOrderTab = props.locationa.state.followUpOrder
    }
    this.state = {
      activeTab: '1',
      activeOrderTab: initialOrderTab || 'primary',
      firstOrderLoaded: false,
      followUpOrderTabs: [],
      selectedDoc: null,
      noOrderFound: false,
      viewPdf: false,
      isDragOver: false
    }
    props.fees.filter()
  }

  componentDidMount() {
    window.addEventListener('dragover', (e) => {
      e = e || event
      e.preventDefault()
    }, false)
    window.addEventListener('drop', (e) => {
      e = e || event
      e.preventDefault()
    }, false)
  }

My Test:

it('componentDidUpdate', () => {
    // Shoutout to Connect(Prefetch) for this awesome next line of 100000 dives.
    const shallowWrap = shallow(<Router>
      <SingleOrderContainer {...props} store={store}/>
    </Router>
    ).dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive().dive()
    const componentDidMountSpy = jest.spyOn(shallowWrap.instance(), 'componentDidMount')
    mount(<Router><Provider store={store}>{shallowWrap.get(0)}</Provider></Router>)
    expect(componentDidMountSpy).toHaveBeenCalledTimes(1)
  })

(The .dive() spam is due the Connect(PrefetchResourceContainer))

Any help is greatly appreciated :)

Aucun commentaire:

Enregistrer un commentaire