Skip to content

Commit

Permalink
docs: expand testing doc
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak committed Mar 5, 2025
1 parent 698e4df commit 7547428
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ sidebar_position: 50

# Testing

## Setting up a mock
## Setting up the mock

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.
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.

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

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

[//]: # '### Writing tests'
[//]: #
[//]: # '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.'
## Writing tests

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):

```jsx title="App.test.js"
import {
GoogleOneTapSignIn,
OneTapResponse,
} from '@react-native-google-signin/google-signin';
import * as React from 'react';
import { render, screen, fireEvent } from '@testing-library/react-native';
import { Button, Text } from 'react-native';
import { useState } from 'react';

function GoogleSignInComponent() {
const [userInfo, setUserInfo] = useState<OneTapResponse | undefined>();

return (
<>
<Button
title="Sign in with Google"
onPress={async () => {
GoogleOneTapSignIn.configure({
webClientId: 'autoDetect',
});
const userInfo = await GoogleOneTapSignIn.signIn();
setUserInfo(userInfo);
}}
/>
{userInfo && <Text>{userInfo.data?.user.name}</Text>}
</>
);
}

it('GoogleSignInComponent should display user name after signing in', async () => {
// Render the component
render(<GoogleSignInComponent />);

const expectedUserName = 'mockFullName';
// Verify user name is not displayed initially
expect(screen.queryByText(expectedUserName)).toBeNull();

// Find and press the sign-in button
const signInButton = screen.getByText('Sign in with Google');
fireEvent.press(signInButton);

// verify the user name is displayed
const userName = await screen.findByText(expectedUserName);
expect(userName).toBeTruthy();
});
```

0 comments on commit 7547428

Please sign in to comment.