react-perimeter icon indicating copy to clipboard operation
react-perimeter copied to clipboard

Add test suite

Open aweary opened this issue 8 years ago • 4 comments

It's probably hard to test the actual core feature of react-perimeter , but there are a few things we can definitely test:

  • correctly exports the React component
  • renders children
  • attaches ref correctly with render callbacks
  • registers event listeners on mount
  • removes event listeners on unmount
  • removes event listeners after onBreach is called if once is true
  • more stuff

I'd like to use jest and enzyme for this.

aweary avatar Mar 17 '17 16:03 aweary

@aweary If your okay with it, I would like to take a stab at it.

binoy14 avatar Mar 18 '17 09:03 binoy14

@binoy14 go for it! Feel free to ping me here with any questions

aweary avatar Mar 18 '17 20:03 aweary

@aweary If you could help me with how to test event listeners that would be a big help I made initial commit here https://github.com/binoy14/react-perimeter/commit/7878a98caa8f126450f7401dc4dbce3d160c1bf6

binoy14 avatar Mar 18 '17 21:03 binoy14

@binoy14 I would probably mock window.addEventListener and window.removeEventListener and then assert that they've been called at the correct point and the correct number of times

See Jest's doc page on mocks: https://facebook.github.io/jest/docs/mock-functions.html

We can do this in the beforeEach hook, so the mock is reset before each test:

beforeEach(() => {
  window.addEventListener = jest.fn()
  window.removeEventListener = jest.fn()
})

The doc page I linked has some info on how to assert on mocked functions in Jest.

aweary avatar Mar 18 '17 21:03 aweary