Skip to content

Testing with Jest #216

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

Closed
mtakac opened this issue Jul 9, 2020 · 10 comments · Fixed by #587
Closed

Testing with Jest #216

mtakac opened this issue Jul 9, 2020 · 10 comments · Fixed by #587
Labels

Comments

@mtakac
Copy link

mtakac commented Jul 9, 2020

Question

I am having hard times trying to figure out how to write tests for forms using the datetime picker component and I couldn't really find any examples online. Wouldn't it be useful to include a simple example in the docs? I am using jest and react-native-testing-library. The problem is that every time I open the datetime picker in my tests it throws an error: TypeError: this._picker.current.setNativeProps is not a function.

I am not really sure if I can mock this call out or what the recommended approach would be.

@luancurti
Copy link
Member

@mtakac Can you provide a example of test that you are facing this issue?

@jaimepater
Copy link

I have the same issue
This is mi test example

const timePicker = await component.findByTestId('dateTimePicker');
fireEvent(timePicker, 'onChange', { nativeEvent: { timestamp: today } });

And I getting the same error TypeError: this._picker.current.setNativeProps is not a function
can someone provide us simple example of a unit test ?

@jaimepater
Copy link

If someone else has this issue , I had to mock it but it works fine for me

jest.mock('@react-native-community/datetimepicker', () => jest.fn());
RNDateTimePicker.mockImplementation((props) => (
  <Input testID={props.testID} onFocus={() => props.onChange} />
));

I hope that this can help

@tills98
Copy link

tills98 commented Oct 3, 2020

If someone else has this issue , I had to mock it but it works fine for me

jest.mock('@react-native-community/datetimepicker', () => jest.fn());
RNDateTimePicker.mockImplementation((props) => (
  <Input testID={props.testID} onFocus={() => props.onChange} />
));

I hope that this can help

I've tried that fix but I got the following error:

TypeError: _datetimepicker.default.mockImplementationOnce is not a function

With the following code:

import RNDateTimePicker from '@react-native-community/datetimepicker';
//...
        it.only('should fire onChange with new date', async () => {
            jest.mock('@react-native-community/datetimepicker', () => jest.fn());
            RNDateTimePicker.mockImplementation((props) => <View testID={props.testID} />);

            const handleChangeDate = jest.fn();

            // render component
            const { getByTestId, getByText, debug } = render(
                <NoHyphenatedTextProvider>
                    <DatePickerInput
                        value={new Date('2020-03-04')}
                        label={'Datum'}
                        onChangeDate={handleChangeDate}
                    />
                </NoHyphenatedTextProvider>
            );
            // ...
        });

Do you have any idea to solve this issue?

@jaimepater
Copy link

Sorry for the delay @tills98

Try to move

 jest.mock('@react-native-community/datetimepicker', () => jest.fn());
            RNDateTimePicker.mockImplementation((props) => <View testID={props.testID} />);

After you imports ( I mean outside of your it and/or describe )

@mehmetnyarar
Copy link

mehmetnyarar commented Nov 23, 2020

In your setup file:

//  I use expo v39, therefore I use `@react-native-community/datetimepicker`
jest.mock('@react-native-community/datetimepicker', () => {
  const React = require('React')
  const RealComponent = jest.requireActual(
    '@react-native-community/datetimepicker'
  )

  class Picker extends React.Component {
    render () {
      return React.createElement('Picker', this.props, this.props.children)
    }
  }

  Picker.propTypes = RealComponent.propTypes
  return Picker
})

Your test:

const picker = await findByA11yLabel('picker')
fireEvent(picker, 'onChange', null, date)
expect(picker.props.value).toEqual(date)

Original solution from @react-native-community/picker
Related documentation

@Andarius
Copy link

Andarius commented Dec 9, 2020

FYI this works to mock (https://jestjs.io/docs/en/tutorial-react-native#mock-native-modules-using-jestmock)

jest.mock('@react-native-community/datetimepicker', function () {
    const mockComponent = require('react-native/jest/mockComponent')
    return mockComponent('@react-native-community/datetimepicker')
})

@vonovak
Copy link
Member

vonovak commented Dec 9, 2020

hello 🙂
please refer to the readme: https://github.com/react-native-datetimepicker/datetimepicker#testing-with-jest

@sritharIOS
Copy link

const timePicker = await component.findByTestId('dateTimePicker');
fireEvent(timePicker, 'onChange', { nativeEvent: { timestamp: '01/01/1976' } });

@vonovak
Copy link
Member

vonovak commented Jul 27, 2022

🎉 This issue has been resolved in version 6.3.0 🎉

If this package helps you, consider sponsoring us! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants