Skip to content

Commit 18c9d06

Browse files
authored
docs: expand testing doc (#60)
1 parent 698e4df commit 18c9d06

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

docs/testing.md

+53-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ sidebar_position: 50
44

55
# Testing
66

7-
## Setting up a mock
7+
## Setting up the mock
88

9-
If you use Jest for testing, you may need to mock the functionality of the native module - this is because the native code cannot run in Node environment.
9+
If you want to write JS-level tests that depend on Google Sign In, you need to mock the functionality of the native module - this is because the native code cannot run in Node environment.
1010

1111
This library ships with a Jest mock that you can add to the `setupFiles` array in your Jest config.
1212

@@ -20,6 +20,54 @@ By default, the mock behaves as if the calls were successful and returns mock us
2020
}
2121
```
2222

23-
[//]: # '### Writing tests'
24-
[//]: #
25-
[//]: # 'You can use [`@testing-library/react-native`](https://callstack.github.io/react-native-testing-library/) to write tests for React components that use React Native Google Sign In.'
23+
## Writing tests
24+
25+
You can use [`@testing-library/react-native`](https://callstack.github.io/react-native-testing-library/) to write tests for React components that use React Native Google Sign In. Minimal example (make sure to [set up the mock](#setting-up-the-mock) first):
26+
27+
```jsx title="App.test.js"
28+
import {
29+
GoogleOneTapSignIn,
30+
OneTapResponse,
31+
} from '@react-native-google-signin/google-signin';
32+
import * as React from 'react';
33+
import { render, screen, fireEvent } from '@testing-library/react-native';
34+
import { Button, Text } from 'react-native';
35+
import { useState } from 'react';
36+
37+
function GoogleSignInComponent() {
38+
const [userInfo, setUserInfo] = useState<OneTapResponse | undefined>();
39+
40+
return (
41+
<>
42+
<Button
43+
title="Sign in with Google"
44+
onPress={async () => {
45+
GoogleOneTapSignIn.configure({
46+
webClientId: 'autoDetect',
47+
});
48+
const userInfo = await GoogleOneTapSignIn.signIn();
49+
setUserInfo(userInfo);
50+
}}
51+
/>
52+
{userInfo && <Text>{userInfo.data?.user.name}</Text>}
53+
</>
54+
);
55+
}
56+
57+
it('GoogleSignInComponent should display user name after signing in', async () => {
58+
// Render the component
59+
render(<GoogleSignInComponent />);
60+
61+
const expectedUserName = 'mockFullName';
62+
// Verify user name is not displayed initially
63+
expect(screen.queryByText(expectedUserName)).toBeNull();
64+
65+
// Find and press the sign-in button
66+
const signInButton = screen.getByText('Sign in with Google');
67+
fireEvent.press(signInButton);
68+
69+
// verify the user name is displayed
70+
const userName = await screen.findByText(expectedUserName);
71+
expect(userName).toBeTruthy();
72+
});
73+
```

0 commit comments

Comments
 (0)