Skip to content

Commit

Permalink
Fix tests for expo-52 issues with jest
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne committed Jan 21, 2025
1 parent b068e90 commit 69c4367
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 86 deletions.
138 changes: 72 additions & 66 deletions src/Navigation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,93 @@
import React from 'react';
import React from "react";

Check failure on line 1 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

import { configureStore } from '@reduxjs/toolkit';
import { render, waitFor } from '@testing-library/react-native';
import { Provider } from 'react-redux';
import { configureStore } from "@reduxjs/toolkit";

Check failure on line 3 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote
import { render, waitFor } from "@testing-library/react-native";

Check failure on line 4 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote
import { Provider } from "react-redux";

Check failure on line 5 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

import gamesReducer, { gameDefaults } from '../redux/GamesSlice';
import settingsReducer, { initialState as settingsState } from '../redux/SettingsSlice';
import { useNavigationMock } from '../test/test-helpers';
import gamesReducer, { gameDefaults } from "../redux/GamesSlice";

Check failure on line 7 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote
import settingsReducer, {
initialState as settingsState,
} from "../redux/SettingsSlice";

Check failure on line 10 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote
import { useNavigationMock } from "../test/test-helpers";

Check failure on line 11 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

import ListScreen from './screens/ListScreen';
import ListScreen from "./screens/ListScreen";

Check failure on line 13 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

jest.mock('Analytics');
jest.mock("Analytics");

Check failure on line 15 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote
jest.mock("expo-font"); // https://github.com/callstack/react-native-paper/issues/4561

Check failure on line 16 in src/Navigation.test.tsx

View workflow job for this annotation

GitHub Actions / build (16.x)

Strings must use singlequote

const mockStore = () => {
return configureStore({
reducer: {
settings: settingsReducer,
games: gamesReducer,
return configureStore({
reducer: {
settings: settingsReducer,
games: gamesReducer,
},
preloadedState: {
settings: {
...settingsState,
currentGameId: "123",
},
games: {
entities: {
"123": {
...gameDefaults,
id: "123",
title: "Game",
dateCreated: 1,
playerIds: [],
},
},
preloadedState: {
settings: {
...settingsState,
currentGameId: '123'
},
games: {
entities: {
'123': {
...gameDefaults,
id: '123',
title: 'Game',
dateCreated: 1,
playerIds: [],
}
},
ids: ['123']
}
}
});
ids: ["123"],
},
},
});
};

describe('Navigation', () => {
it('show the onboarding screen when onboardedSemVer 1.0.0', async () => {
const navigation = useNavigationMock();
const { Application } = require('expo-application');
expect(Application.nativeApplicationVersion).toBe('1.0.0');
describe("Navigation", () => {
it("show the onboarding screen when onboardedSemVer 1.0.0", async () => {
const navigation = useNavigationMock();
const { Application } = require("expo-application");
expect(Application.nativeApplicationVersion).toBe("1.0.0");

const store = mockStore();
const store = mockStore();

render(
<Provider store={store}>
<ListScreen navigation={navigation} />
</Provider>
);
render(
<Provider store={store}>
<ListScreen navigation={navigation} />
</Provider>
);

await waitFor(() => {
expect(navigation.navigate).toHaveBeenCalledWith('Onboarding', expect.objectContaining({
onboarding: true,
}));
});
await waitFor(() => {
expect(navigation.navigate).toHaveBeenCalledWith(
"Onboarding",
expect.objectContaining({
onboarding: true,
})
);
});
});

it('does not show the onboarding screen when onboardedSemVer is equal or greater than 2.5.7', async () => {
const navigation = useNavigationMock();
it("does not show the onboarding screen when onboardedSemVer is equal or greater than 2.5.7", async () => {
const navigation = useNavigationMock();

jest.doMock('expo-application', () => ({
Application: {
nativeApplicationVersion: '2.5.7',
},
}));
jest.doMock("expo-application", () => ({
Application: {
nativeApplicationVersion: "2.5.7",
},
}));

const { Application } = require('expo-application');
expect(Application.nativeApplicationVersion).toBe('2.5.7');
const { Application } = require("expo-application");
expect(Application.nativeApplicationVersion).toBe("2.5.7");

const store = mockStore();
const store = mockStore();

render(
<Provider store={store}>
<ListScreen navigation={navigation} />
</Provider>
);
render(
<Provider store={store}>
<ListScreen navigation={navigation} />
</Provider>
);

await waitFor(() => {
expect(navigation.navigate).not.toHaveBeenCalledWith('Onboarding');
});
await waitFor(() => {
expect(navigation.navigate).not.toHaveBeenCalledWith("Onboarding");
});
});
});
41 changes: 21 additions & 20 deletions src/components/Buttons/AppInfoButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { fireEvent, render, waitFor } from '@testing-library/react-native';
import { fireEvent, render, waitFor } from "@testing-library/react-native";

import { useNavigationMock } from '../../../test/test-helpers';
import { logEvent } from '../../Analytics';
import { useNavigationMock } from "../../../test/test-helpers";
import { logEvent } from "../../Analytics";

import AppInfoButton from './AppInfoButton';
import AppInfoButton from "./AppInfoButton";

jest.mock('../../Analytics');
jest.mock("../../Analytics");
jest.mock("expo-font"); // https://github.com/callstack/react-native-paper/issues/4561

describe('AppInfoButton', () => {
const navigation = useNavigationMock();
describe("AppInfoButton", () => {
const navigation = useNavigationMock();

it('should navigate to AppInfo screen when pressed', () => {
const { getByRole } = render(<AppInfoButton navigation={navigation} />);
const button = getByRole('button');
fireEvent.press(button);
expect(navigation.navigate).toHaveBeenCalledWith('AppInfo');
});
it("should navigate to AppInfo screen when pressed", () => {
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} />);
const button = getByRole('button');
it("should log an analytics event when pressed", async () => {
const { getByRole } = render(<AppInfoButton navigation={navigation} />);
const button = getByRole("button");

fireEvent.press(button);
fireEvent.press(button);

await waitFor(() => {
expect(logEvent).toHaveBeenCalledWith('app_info');
});
await waitFor(() => {
expect(logEvent).toHaveBeenCalledWith("app_info");
});
});
});

0 comments on commit 69c4367

Please sign in to comment.