Skip to content

Commit

Permalink
work on tests for ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Requinard committed Jul 5, 2022
1 parent 16e4601 commit bef294b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
cache: 'npm'
- run: yarn install
- run: yarn lint
- run: yarn test
- run: yarn test --forceExit
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
]
},
"jest": {
"preset": "react-native",
"preset": "@testing-library/react-native",
"setupFilesAfterEnv": [
"@testing-library/jest-native/extend-expect",
"./src/setupTests.js"
Expand Down
5 changes: 2 additions & 3 deletions src/components/Utilities/LoadingIndicator.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { render, screen } from "@testing-library/react-native";

import { render, screen } from "../../testUtils";
import { LoadingIndicator } from "./LoadingIndicator";

describe("LoadingIndicator", function () {
it("renders", () => {
it("renders", async () => {
render(<LoadingIndicator />);

expect(screen.getByTestId("loadingindicator")).toBeTruthy();
Expand Down
15 changes: 9 additions & 6 deletions src/hoc/withPlatform.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import { screen } from "@testing-library/react-native";
import { Text } from "react-native";

import { render } from "../testUtils";
import { withPlatform } from "./withPlatform";

const TestComponent = (props: any) => <Text testID={"withPlatformComponent"}>I am some text</Text>;
describe("withPlatform", function () {
beforeAll(() => {
beforeEach(() => {
jest.mock("react-native/Libraries/Utilities/Platform", () => ({
OS: "web",
}));
});

it("renders on the current platform", () => {
afterEach(() => {
jest.unmock("react-native/Libraries/Utilities/Platform");
});

it("renders on the current platform", async () => {
const Wrapped = withPlatform(TestComponent, ["web"]);
render(<Wrapped />);
const screen = render(<Wrapped />);

expect(screen.getByTestId("withPlatformComponent")).toBeTruthy();
});

it("does not render on a different platform", () => {
it("does not render on a different platform", async () => {
const Wrapped = withPlatform(TestComponent, ["android"]);
render(<Wrapped />);
const screen = render(<Wrapped />);

expect(screen.queryByTestId("withPlatformComponent")).toBeFalsy();
});
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useNow.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ describe("useNow", function () {
return () => jest.requireActual("moment")("2020-01-01T12:00:00.000Z");
});
});
it("returns now when no state is supplied", () => {
it("returns now when no state is supplied", async () => {
const { result } = renderHook(() => useNow(), {});

expect(result.current[0].valueOf()).toBeCloseTo(moment().valueOf(), -2);
});

it("returns a time in the future if a state is supplied", () => {
it("returns a time in the future if a state is supplied", async () => {
const { result } = customRenderHook(() => useNow(), {
preloadedState: {
timetravel: {
Expand All @@ -30,7 +30,7 @@ describe("useNow", function () {

expect(diff).toBe(60);
});
it("returns a time in the past if a state is supplied with a negative amount", () => {
it("returns a time in the past if a state is supplied with a negative amount", async () => {
const { result } = customRenderHook(() => useNow(), {
preloadedState: {
timetravel: {
Expand All @@ -46,7 +46,7 @@ describe("useNow", function () {
expect(diff).toBe(-60);
});

it("does not apply timetravel when it is not enabled", () => {
it("does not apply timetravel when it is not enabled", async () => {
const { result } = customRenderHook(() => useNow(), {
preloadedState: {
timetravel: {
Expand Down
2 changes: 1 addition & 1 deletion src/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mockAsyncStorage from "@react-native-async-storage/async-storage/jest/async-storage-mock";

import "@testing-library/jest-native/extend-expect";
jest.mock("@react-native-async-storage/async-storage", () => mockAsyncStorage);
jest.useFakeTimers();
3 changes: 2 additions & 1 deletion src/testUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { legacy_createStore } from "@reduxjs/toolkit";
import { render, renderHook } from "@testing-library/react-native";
import { ReactElement } from "react";
import { Provider as StoreProvider } from "react-redux";

import { reducers, RootState, store } from "./store";
Expand All @@ -9,7 +10,7 @@ type ReduxOptions = {
store?: typeof store;
};

export const customRender = <Result,>(ui: any, options: ReduxOptions = {}) => {
export const customRender = <Result,>(ui: ReactElement<Result>, options: ReduxOptions = {}): ReturnType<typeof render> => {
const preloadedState = options.preloadedState as any;
const store = legacy_createStore(reducers, preloadedState);

Expand Down

0 comments on commit bef294b

Please sign in to comment.