0

I am learning to jest test react js reflux action calls from the componentDidMount() method

the jsx imports the store call like so

import MyStore from '~/stores/myStore'

componentDidMount() {
  MyStore.getData()
}

In my myStore.test.jsx code

import React from 'react'
import renderer from 'react-test-renderer'

jest.dontMock('./myStore.jsx')

const MyStore = require('./myStore.jsx').default

describe('', () => {
   it(''), () => {
     const spy = jest.spyOn(MyStore.prototype, 'MyStore.getData')
     render.create(<MyStore/>).getInstance()
     expect(spy).toHaveBeenCalled()
   }
}

I was following another similar post

https://stackoverflow.com/questions/43245040/using-jest-to-spy-on-method-call-in-componentdidmount

But I am getting the following jest test error

Cannot spy the MyStore.getData() property because it is not a function

I have not been able to find a testing on reflux action which involves a call to the store.

Is there a fix? Is there a better way to test the calls in componentDidMount?

Thanks

c0micrage
  • 1,118
  • 2
  • 21
  • 56

1 Answers1

1

Assuming that getData is a method on the prototype, you should use:

const spy = jest.spyOn(MyStore.prototype, 'getData')

If you want to spy on methods on the prototype.

Ivan V.
  • 7,593
  • 2
  • 36
  • 53