Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-router-native/Link does not call onPress #4811

Closed
benstepp opened this issue Mar 22, 2017 · 3 comments
Closed

react-router-native/Link does not call onPress #4811

benstepp opened this issue Mar 22, 2017 · 3 comments
Labels

Comments

@benstepp
Copy link
Contributor

benstepp commented Mar 22, 2017

The native Link does not call onPress when passed because it is overridden in order to provide its routing functionality.

I would expect it to behave more like the react-router-dom Link and call the onPress function if it is passed. The docs state that all props would be passed to the rendered component, but onPress is not.

To fix, either the documentation should be updated to correctly describe the current behavior, or the component should be updated to adhere to the documentation.

I'd be happy to submit a pull request, or here's a failing test to get you started.

import React from 'react'
import ReactTestUtils from 'react-addons-test-utils'
import { View } from 'react-native'
import Link from '../Link'

const createContext = () => ({
  router: {
    history: {
      push: jest.fn(),
      replace: jest.fn()
    }
  }
})

describe('<Link />', () => {
  it('calls onPress when pressed', () =>{
    const renderer = ReactTestUtils.createRenderer()
    const onPress = jest.fn()
    const context = createContext()

    renderer.render(
      <Link onPress={onPress} to='/'>
        <View />
      </Link>
    , context)

    const output = renderer.getRenderOutput()
    output.props.onPress() // simulate onPress on TouchableHighlight
    expect(onPress.mock.calls.length).toBe(1)
  })
})
@timdorr
Copy link
Member

timdorr commented Mar 22, 2017

Could you whip up that PR? Should be simple.

@benstepp
Copy link
Contributor Author

The change is simple, but a couple of quick questions:

  • Should the event.preventDefault behavior of react-router-dom/Link be preserved here? (ie: if user prevents default in their onPress handler, react-router does nothing)

  • Should any tests be done with jest? I noticed that only react-router-native uses jest as a testing framework, and I don't know if consistency across packages is a goal.

@timdorr
Copy link
Member

timdorr commented Mar 22, 2017

Yes, you should respect the event bubbling wishes of an onPress handler.

react-router-redux uses jest too (it also uses rollup for UMD builds). Use whatever testing framework is already established. Jest is built into RN, hence we used it for that sub-package.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants