shichongrui / react-native-test-utils

A test utils library for testing react native components
24 stars 5 forks source link

`createApi()` methods return cached values of component #11

Open phillbaker opened 6 years ago

phillbaker commented 6 years ago

After rendering a component, I believe that the component api text() function and props property are returning cached values of the rendered component's values because this line calls toJSON():

https://github.com/shichongrui/react-native-test-utils/blob/ecf0a7951721274298e0b0cd70e9b8428a33e9b5/index.js#L65

Example:

test('it has the correct text', () => {
  let view = renderer(<MyComponent />)

  expect(view.text()).toContain('Hello') // passes

  let textInputView = view.query("TextInput[placeholder='Name']")
  textInputView.simulate('changeText', 'react-native-test-utils')

  expect(view.text()).toContain('react-native-test-utils') // fails
})

Proposed fix: pass a reference to the view and call toJSON() in the functions themselves.

shichongrui commented 6 years ago

That's a great catch. I'll do some digging to see if this is the culprit and see if we can fix it.