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

Fix NativeModules.RNSnackbar being undefined during testing #58

Closed
wants to merge 1 commit into from

Conversation

arakawamoriyuki
Copy link

problem

Test fails with NativeModules.RNSnackbar undefined.

Cannot read property 'LENGTH_LONG' of undefined

env

  • react-native 0.47.1
  • react-native-snackbar 0.4.4

fixes

Fix to not use if undefined.

related issues

@cooperka
Copy link
Owner

Hi @arakawamoriyuki, thank you for the contribution! But I think most native modules actually work this way -- I suggest you mock it out instead of modifying the code here. For example, with Jest: https://facebook.github.io/jest/docs/en/manual-mocks.html

// your-app/__mocks__/react-native-snackbar.js

module.exports = {
  LENGTH_LONG: 0,
  LENGTH_SHORT: -1,
  LENGTH_INDEFINITE: -2,
};

or without Jest you could manually shim (I haven't tested this myself):

// your-app/run-this-before-your-tests.js

import { NativeModules } from 'react-native'

NativeModules.RNSnackbar = {
  LENGTH_LONG: 0,
  LENGTH_SHORT: -1,
  LENGTH_INDEFINITE: -2,
};

I'm happy to merge this PR if it is, in fact, some sort of standard to export something different during testing, but in my experience, this change would be unexpected behavior. It could break other peoples' tests who may have shimmed NativeModules.RNSnackbar.

@cooperka cooperka closed this Jan 12, 2018
@cooperka cooperka changed the title measures for NativeModules.RNSnackbar to become undefined during testing Fix NativeModules.RNSnackbar being undefined during testing Jan 12, 2018
@arakawamoriyuki
Copy link
Author

thank you for the advice. The test worked with the app/__mocks__/react-native-snackbar.js.

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 this pull request may close these issues.

None yet

2 participants