Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tests): Use fake timers #22

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"$schema": "https://json.schemastore.org/mocharc",
"exit": true,
"extension": ["ts", "tsx"],
"recursive": true,
"require": [
"ts-node/register",
"src/register.ts",
"test/setup.ts",
"test/hooks.ts"
],
"spec": ["test/**/*.test.*"]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"compile": "tsc",
"lint": "eslint . --report-unused-disable-directives",
"release": "semantic-release",
"test": "RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true NODE_ENV=test mocha"
"test": "NODE_ENV=test mocha"
},
"packageManager": "[email protected]",
"dependencies": {
Expand Down
6 changes: 6 additions & 0 deletions test/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ export function mochaHooks(): Mocha.RootHookObject {
afterEach() {
Sinon.restore();
},
beforeEach() {
Sinon.useFakeTimers({
advanceTimeDelta: 0,
shouldAdvanceTime: true,
});
},
};
}
11 changes: 11 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { userEvent } from "@testing-library/react-native";
import Sinon from "sinon";

process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS = "true";

const newUserEvent = userEvent.setup({
advanceTimers: delay => Sinon.clock.tickAsync(delay).then(),
delay: 0,
});

Object.assign(userEvent, newUserEvent);
2 changes: 0 additions & 2 deletions test/unit/lib/mockNative.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "../../../src/register";

import { expect } from "@assertive-ts/core";
import { render } from "@testing-library/react-native";
import { ReactElement, useEffect, useRef, useState } from "react";
Expand Down
17 changes: 15 additions & 2 deletions test/unit/register.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import "../../src/register";

import { expect } from "@assertive-ts/core";
import { render, waitFor, userEvent } from "@testing-library/react-native";
import { ReactElement, useCallback, useRef, useState } from "react";
Expand All @@ -9,6 +7,7 @@ import { Rect, Svg } from "react-native-svg";
function TestScreen(): ReactElement {

const [animated, setAnimated] = useState(false);
const [greet, setGreet] = useState("Hello!");

const enterLeft = useRef(new Animated.Value(100, { useNativeDriver: true })).current;
const movePoint = useRef(new Animated.ValueXY({ x: 0, y: 0 }, { useNativeDriver: true })).current;
Expand All @@ -32,6 +31,10 @@ function TestScreen(): ReactElement {
});
}, []);

const changeGreet = useCallback(() => {
setGreet("I said hello!!!");
}, []);

return (
<ScrollView>
<View>
Expand Down Expand Up @@ -59,6 +62,8 @@ function TestScreen(): ReactElement {
<Animated.View style={{ marginLeft: enterLeft }}>
<Text>{`Animated view: ${animated}`}</Text>
</Animated.View>
<Button title="Long press me!" onPress={changeGreet} />
<Text>{greet}</Text>
</ScrollView>
);
}
Expand Down Expand Up @@ -90,6 +95,14 @@ describe("[Unit] register.test.ts", () => {
await userEvent.press(clickMeButton);

await waitFor(() => getByText("Animated view: true"));

const longPressButton = await findByText("Long press me!");

expect(getByText("Hello!")).toBePresent();

await userEvent.longPress(longPressButton);

await waitFor(() => getByText("I said hello!!!"));
});
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"esModuleInterop": true,
"importHelpers": true,
"incremental": true,
"lib": ["DOM", "ES2019", "ES2020", "ES2021", "ES2022", "ES2023"],
"jsx": "react-jsx",
"lib": ["DOM", "ES2023"],
"module": "CommonJS",
"moduleResolution": "Node",
"noImplicitReturns": true,
Expand Down
Loading