Skip to content

Commit

Permalink
chore(tests): Use fake timers (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion committed Apr 27, 2024
1 parent 48f9036 commit e44385b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 7 deletions.
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": "yarn@4.1.1",
"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

0 comments on commit e44385b

Please sign in to comment.