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

Handling testing/adding a mockfile #184

Closed
2 tasks done
Meegan opened this issue Jun 27, 2024 · 6 comments
Closed
2 tasks done

Handling testing/adding a mockfile #184

Meegan opened this issue Jun 27, 2024 · 6 comments
Labels
feature request New feature or request

Comments

@Meegan
Copy link

Meegan commented Jun 27, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

I'm attempting to run test via jest in my app, and I'm getting this:

TypeError: Cannot read properties of undefined (reading 'configure')

when attempting the below:

GiphySDK.configure({ apiKey: ${mySdkKey} });

I've tried adding the @Giphy library to transformIgnorePatterns in my jestfile, but there's no difference there.

Is there a mockfile I can load into the library? If not, what would be required to set that up? (Admittedly, I'm somewhat new at working with these tests.)

Motivation 🔦

This is preventing me from my does it render test for my app.

@Meegan Meegan added the feature request New feature or request label Jun 27, 2024
@Meegan
Copy link
Author

Meegan commented Jun 27, 2024

Okay, so I realized I WASN'T on the latest version of @giphy/react-native-sdk, after updating I'm encountering a new error:

Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RTNGiphySDKModule' could not be found. Verify that a module by this name is registered in the native binary.

Seems to be coming in from the root import statement for the Giphy components.

@ALexanderLonsky
Copy link
Contributor

Hey @Meegan,

To mock the Giphy module:

  1. Add jest.config.js in the root folder:
module.exports = {
  preset: 'react-native',
  moduleNameMapper: {
    '^@giphy/react-native-sdk$': '<rootDir>/__mocks__/giphy-react-native-sdk.js',
  },
};
  1. Alternatively, you can add this directly to the package.json right after devDependencies section:
"jest": {
  "preset": "react-native",    
  "moduleNameMapper": {
    "^@giphy/react-native-sdk$": "<rootDir>/__mocks__/giphy-react-native-sdk.js"
  }
}

Then

__mocks__/react-native-giphy-sdk.js:

const GiphyDialog = {
  show: jest.fn(),
};  

const GiphySDK = {
  configure: jest.fn(),
};

export { GiphyDialog, GiphySDK };

App.tsx:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 */

import React from 'react';
import type {PropsWithChildren} from 'react';
import {
  SafeAreaView,
  useColorScheme,
  Button
} from 'react-native';

import {
  Colors,
  DebugInstructions,
  Header,
  LearnMoreLinks,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

import { GiphyContent, GiphyDialog, GiphySDK } from '@giphy/react-native-sdk';

GiphySDK.configure({ apiKey: '******************' })

function App(): React.JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  return (
    <SafeAreaView>
      <Button title="Show Giphy Dialog" onPress={() => GiphyDialog.show()} />
    </SafeAreaView>
  );
}

export default App;

__tests__/App.test.tsx:

import 'react-native';
import React from 'react';
import App from '../App';
import { it, expect, jest } from '@jest/globals';
import { render, fireEvent } from '@testing-library/react-native';

it('shows Giphy dialog on button press', () => {
  const { getByText } = render(<App />);
  const button = getByText('Show Giphy Dialog');

  fireEvent.press(button);

  const { GiphyDialog } = require('@giphy/react-native-sdk');
  expect(GiphyDialog.show).toHaveBeenCalledTimes(1);
});

Please let me know if that works for you.

@Meegan
Copy link
Author

Meegan commented Jul 2, 2024

This resolved my issue, thanks! Can/should we either:

  1. update this task
  2. create a new task

And I can make a PR to:

  1. add a default mockfile in the repo
  2. add this in the documentation

Let me know -- otherwise I can mark this soled and close the issue.

@ALexanderLonsky
Copy link
Contributor

Hey @Meegan,
Thank you for the suggestion.
I don't see any sense in adding testing mocks to the SDK, as it is more appropriate for the app side where tests need to be implemented. Different apps have different environments.
However, it may be beneficial to add a documentation section with guides on how to mock Giphy module.
So, feel free to open a PR.

@ALexanderLonsky
Copy link
Contributor

@Meegan,
I've updated the documentation.

@Meegan
Copy link
Author

Meegan commented Jul 11, 2024

@ALexanderLonsky ah, thank you! Sorry, busy few weeks for me 😄

As a final note, I also had to mock a configure method on GiphyDialog, but that might be specific to my setup.

@Meegan Meegan closed this as completed Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants