Skip to content

Commit e44385b

Browse files
authored
chore(tests): Use fake timers (#22)
1 parent 48f9036 commit e44385b

File tree

7 files changed

+36
-7
lines changed

7 files changed

+36
-7
lines changed

.mocharc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"$schema": "https://json.schemastore.org/mocharc",
3-
"exit": true,
43
"extension": ["ts", "tsx"],
54
"recursive": true,
65
"require": [
76
"ts-node/register",
7+
"src/register.ts",
8+
"test/setup.ts",
89
"test/hooks.ts"
910
],
1011
"spec": ["test/**/*.test.*"]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"compile": "tsc",
3030
"lint": "eslint . --report-unused-disable-directives",
3131
"release": "semantic-release",
32-
"test": "RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true NODE_ENV=test mocha"
32+
"test": "NODE_ENV=test mocha"
3333
},
3434
"packageManager": "yarn@4.1.1",
3535
"dependencies": {

test/hooks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ export function mochaHooks(): Mocha.RootHookObject {
55
afterEach() {
66
Sinon.restore();
77
},
8+
beforeEach() {
9+
Sinon.useFakeTimers({
10+
advanceTimeDelta: 0,
11+
shouldAdvanceTime: true,
12+
});
13+
},
814
};
915
}

test/setup.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { userEvent } from "@testing-library/react-native";
2+
import Sinon from "sinon";
3+
4+
process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS = "true";
5+
6+
const newUserEvent = userEvent.setup({
7+
advanceTimers: delay => Sinon.clock.tickAsync(delay).then(),
8+
delay: 0,
9+
});
10+
11+
Object.assign(userEvent, newUserEvent);

test/unit/lib/mockNative.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import "../../../src/register";
2-
31
import { expect } from "@assertive-ts/core";
42
import { render } from "@testing-library/react-native";
53
import { ReactElement, useEffect, useRef, useState } from "react";

test/unit/register.test.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import "../../src/register";
2-
31
import { expect } from "@assertive-ts/core";
42
import { render, waitFor, userEvent } from "@testing-library/react-native";
53
import { ReactElement, useCallback, useRef, useState } from "react";
@@ -9,6 +7,7 @@ import { Rect, Svg } from "react-native-svg";
97
function TestScreen(): ReactElement {
108

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

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

34+
const changeGreet = useCallback(() => {
35+
setGreet("I said hello!!!");
36+
}, []);
37+
3538
return (
3639
<ScrollView>
3740
<View>
@@ -59,6 +62,8 @@ function TestScreen(): ReactElement {
5962
<Animated.View style={{ marginLeft: enterLeft }}>
6063
<Text>{`Animated view: ${animated}`}</Text>
6164
</Animated.View>
65+
<Button title="Long press me!" onPress={changeGreet} />
66+
<Text>{greet}</Text>
6267
</ScrollView>
6368
);
6469
}
@@ -90,6 +95,14 @@ describe("[Unit] register.test.ts", () => {
9095
await userEvent.press(clickMeButton);
9196

9297
await waitFor(() => getByText("Animated view: true"));
98+
99+
const longPressButton = await findByText("Long press me!");
100+
101+
expect(getByText("Hello!")).toBePresent();
102+
103+
await userEvent.longPress(longPressButton);
104+
105+
await waitFor(() => getByText("I said hello!!!"));
93106
});
94107
});
95108
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"esModuleInterop": true,
77
"importHelpers": true,
88
"incremental": true,
9-
"lib": ["DOM", "ES2019", "ES2020", "ES2021", "ES2022", "ES2023"],
109
"jsx": "react-jsx",
10+
"lib": ["DOM", "ES2023"],
1111
"module": "CommonJS",
1212
"moduleResolution": "Node",
1313
"noImplicitReturns": true,

0 commit comments

Comments
 (0)