Skip to content

Commit

Permalink
Test helpers - navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne committed Aug 31, 2023
1 parent 4c6d5b4 commit add9d1b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
21 changes: 4 additions & 17 deletions src/components/Buttons/AppInfoButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import analytics from '@react-native-firebase/analytics';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import AppInfoButton from './AppInfoButton';

jest.mock('@react-navigation/native-stack', () => ({
__esModule: true,
NativeStackNavigationProp: jest.fn(),
}));


type NativeStackNavigationPropAlias = NativeStackNavigationProp<NonNullable<unknown>>;
import { useNavigationMock } from '../../../test/test-helpers';

describe('AppInfoButton', () => {
let navigation: Partial<NativeStackNavigationPropAlias>;
beforeEach(() => {
navigation = {
navigate: jest.fn()
};
});
const navigation = useNavigationMock();

it('should navigate to AppInfo screen when pressed', () => {
const { getByRole } = render(<AppInfoButton navigation={navigation as NativeStackNavigationPropAlias} />);
const { getByRole } = render(<AppInfoButton navigation={navigation} />);
const button = getByRole('button');
fireEvent.press(button);
expect(navigation.navigate).toHaveBeenCalledWith('AppInfo');
});

it('should log an analytics event when pressed', async () => {
const { getByRole } = render(<AppInfoButton navigation={navigation as NativeStackNavigationPropAlias} />);
const { getByRole } = render(<AppInfoButton navigation={navigation} />);
const button = getByRole('button');

fireEvent.press(button);
Expand Down
19 changes: 6 additions & 13 deletions src/components/Buttons/CheckButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import analytics from '@react-native-firebase/analytics';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import CheckButton from './CheckButton';

type NativeStackNavigationPropAlias = NativeStackNavigationProp<NonNullable<unknown>>;
import { useNavigationMock } from '../../../test/test-helpers';

describe('CheckButton', () => {
let navigation: Partial<NativeStackNavigationPropAlias>;

beforeEach(() => {
navigation = {
navigate: jest.fn()
};
});
const navigation = useNavigationMock();

it('should navigate to Game screen when pressed', () => {
const { getByRole } = render(<CheckButton navigation={navigation as NativeStackNavigationPropAlias} />);
const { getByRole } = render(<CheckButton navigation={navigation} />);
const button = getByRole('button');
fireEvent.press(button);
expect(navigation.navigate).toHaveBeenCalledWith('Game');
});

it('should log an analytics event when pressed', async () => {
const { getByRole } = render(<CheckButton navigation={navigation as NativeStackNavigationPropAlias} />);
const { getByRole } = render(<CheckButton navigation={navigation} />);
const button = getByRole('button');

fireEvent.press(button);

waitFor(() => {
await waitFor(() => {
expect(analytics().logEvent).toHaveBeenCalledWith('save_game');
});
});
Expand Down
20 changes: 20 additions & 0 deletions test/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NativeStackNavigationProp } from '@react-navigation/native-stack';

type NativeStackNavigationPropAlias = NativeStackNavigationProp<NonNullable<unknown>>;

const navigationMock: Partial<NativeStackNavigationPropAlias> = {
navigate: jest.fn(),
goBack: jest.fn(),
dispatch: jest.fn(),
setParams: jest.fn(),
reset: jest.fn(),
isFocused: jest.fn(),
canGoBack: jest.fn(),
addListener: jest.fn(),
removeListener: jest.fn(),
setOptions: jest.fn(),
};

export const useNavigationMock = () => {
return navigationMock as NativeStackNavigationPropAlias;
};

0 comments on commit add9d1b

Please sign in to comment.