Skip to content

Commit 5426f23

Browse files
docs: how to mock the Giphy dependency in Jest
re #184
1 parent 734201d commit 5426f23

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Facebook, Slack, Instagram, and more – with just a few lines of code.
2424
- [GIPHY Clips: GIFs with Sound](docs/clips.md)
2525
- [GIPHY Animated Text Creation](docs/animated.md)
2626
- [Example App](https://github.com/Giphy/giphy-react-native-sdk/tree/main/example)
27+
- [How to mock the Giphy dependency in Jest](docs/testing-jest.md)
2728

2829
### [API Reference](docs/api.md)
2930

docs/testing-jest.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# This section describes how to mock the Giphy dependency in Jest.
2+
3+
1. Add `jest.config.js` in the root folder:
4+
5+
```
6+
module.exports = {
7+
preset: 'react-native',
8+
moduleNameMapper: {
9+
'^@giphy/react-native-sdk$': '<rootDir>/__mocks__/giphy-react-native-sdk.js',
10+
},
11+
};
12+
```
13+
14+
2. Alternatively, you can add this directly to the `package.json` right after `devDependencies` section:
15+
```
16+
"jest": {
17+
"preset": "react-native",
18+
"moduleNameMapper": {
19+
"^@giphy/react-native-sdk$": "<rootDir>/__mocks__/giphy-react-native-sdk.js"
20+
}
21+
}
22+
```
23+
Then
24+
`__mocks__/react-native-giphy-sdk.js`:
25+
```
26+
const GiphyDialog = {
27+
show: jest.fn(),
28+
};
29+
30+
const GiphySDK = {
31+
configure: jest.fn(),
32+
};
33+
34+
export { GiphyDialog, GiphySDK };
35+
```
36+
37+
`App.tsx`:
38+
```typescript
39+
import React from 'react';
40+
import type {PropsWithChildren} from 'react';
41+
import {
42+
SafeAreaView,
43+
useColorScheme,
44+
Button
45+
} from 'react-native';
46+
47+
import {
48+
Colors,
49+
DebugInstructions,
50+
Header,
51+
LearnMoreLinks,
52+
ReloadInstructions,
53+
} from 'react-native/Libraries/NewAppScreen';
54+
55+
import { GiphyContent, GiphyDialog, GiphySDK } from '@giphy/react-native-sdk';
56+
57+
GiphySDK.configure({ apiKey: '******************' })
58+
59+
function App(): React.JSX.Element {
60+
const isDarkMode = useColorScheme() === 'dark';
61+
62+
const backgroundStyle = {
63+
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
64+
};
65+
66+
return (
67+
<SafeAreaView>
68+
<Button title="Show Giphy Dialog" onPress={() => GiphyDialog.show()} />
69+
</SafeAreaView>
70+
);
71+
}
72+
73+
export default App;
74+
```
75+
76+
`__tests__/App.test.tsx`:
77+
```typescript
78+
import 'react-native';
79+
import React from 'react';
80+
import App from '../App';
81+
import { it, expect, jest } from '@jest/globals';
82+
import { render, fireEvent } from '@testing-library/react-native';
83+
84+
it('shows Giphy dialog on button press', () => {
85+
const { getByText } = render(<App />);
86+
const button = getByText('Show Giphy Dialog');
87+
88+
fireEvent.press(button);
89+
90+
const { GiphyDialog } = require('@giphy/react-native-sdk');
91+
expect(GiphyDialog.show).toHaveBeenCalledTimes(1);
92+
});
93+
```

0 commit comments

Comments
 (0)