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

Failed unit test: TypeError: Cannot read properties of undefined (reading 'rootedDetectionMethods') #198

Closed
ducminhleeh opened this issue Apr 17, 2023 · 2 comments

Comments

@ducminhleeh
Copy link

ducminhleeh commented Apr 17, 2023

I have used jail-monkey for my app. But when i wrote the unit test, i found the problem like this:
TypeError: Cannot read properties of undefined (reading 'rootedDetectionMethods')

This is my code and test
Code:

  const onLoginPress = () => {
    if (Platform.OS === 'ios') {
      if (JailMonkey.isJailBroken()) {
        // do something
        return;
      }
    }
  };

Test:

// Import the `jail-monkey` package
import JailMonkey from 'jail-monkey';

// Import any necessary components for your UI
import MyButton from './MyButton';
import MyJailbreakWarning from './MyJailbreakWarning';

describe('MyButton', () => {
  it('should show jailbreak warning when clicked on jailbroken device', () => {
    // Set the mock implementation to return true for jailbreak detection
    JailMonkey.isJailBroken.mockReturnValueOnce(true);

    // Render the component that contains the button and the jailbreak warning
    const { getByText, getByTestId } = render(<MyButton />);

    // Simulate a user click on the button
    const button = getByTestId('my-button');
    fireEvent.press(button);

    // Expect the jailbreak warning component to be present in the rendered output
    const jailbreakWarning = getByText('Warning: Your device is jailbroken.');
    expect(jailbreakWarning).toBeDefined();
  });

  it('should not show jailbreak warning when clicked on non-jailbroken device', () => {
    // Set the mock implementation to return false for jailbreak detection
    JailMonkey.isJailBroken.mockReturnValueOnce(false);

    // Render the component that contains the button and the jailbreak warning
    const { getByText, getByTestId } = render(<MyButton />);

    // Simulate a user click on the button
    const button = getByTestId('my-button');
    fireEvent.press(button);

    // Expect the jailbreak warning component to not be present in the rendered output
    const jailbreakWarning = queryByText('Warning: Your device is jailbroken.');
    expect(jailbreakWarning).toBeNull();
  });
});

Can anyone help me to explain why this issue occurs? Thanks 🤔

@ducminhleeh ducminhleeh changed the title Failed unit test Failed unit test: TypeError: Cannot read properties of undefined (reading 'rootedDetectionMethods') Apr 17, 2023
@ducminhleeh
Copy link
Author

Thanks everyone, this is my bad

@murilogok
Copy link

Solved by doing:

jest.mock('react-native', () => {
  const RN = jest.requireActual('react-native'); // use original implementation, which comes with mocks out of the box

  RN.NativeModules.JailMonkey = jest.requireActual('jail-monkey');

  return RN;
});

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

No branches or pull requests

2 participants