-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.tsx
More file actions
127 lines (117 loc) · 3.46 KB
/
Copy pathApp.tsx
File metadata and controls
127 lines (117 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React from "react";
import { StyleSheet, View, SafeAreaView } from "react-native";
import {
MochaRemoteProvider,
ConnectionText,
StatusEmoji,
StatusText,
} from "mocha-remote-react-native";
import { suites as nodeAddonExamplesSuites } from "@react-native-node-api/node-addon-examples";
import { suites as nodeTestsSuites } from "@react-native-node-api/node-tests";
function describeIf(
condition: boolean,
title: string,
fn: (this: Mocha.Suite) => void,
) {
return condition ? describe(title, fn) : describe.skip(title, fn);
}
type Context = {
allTests?: boolean;
nodeAddonExamples?: boolean;
nodeTests?: boolean;
ferricExample?: boolean;
};
function loadTests({
allTests = false,
nodeAddonExamples = allTests,
nodeTests = allTests,
ferricExample = allTests,
}: Context) {
describeIf(nodeAddonExamples, "Node Addon Examples", () => {
for (const [suiteName, examples] of Object.entries(
nodeAddonExamplesSuites,
)) {
describe(suiteName, () => {
for (const [exampleName, requireExample] of Object.entries(examples)) {
it(exampleName, async function () {
if (exampleName === "threadsafe-function") {
// The ported Node.js suite marshals thousands of values across
// threads; every other example keeps the default timeout so a
// genuine deadlock still fails fast.
this.timeout(30_000);
}
const test = requireExample();
if (test instanceof Function) {
const result = test();
if (result instanceof Promise) {
await result;
}
}
});
}
});
}
});
describeIf(nodeTests, "Node Tests", () => {
function registerTestSuite(suite: typeof nodeTestsSuites) {
for (const [name, suiteOrTest] of Object.entries(suite)) {
if (typeof suiteOrTest === "function") {
it(name, suiteOrTest);
} else {
describe(name, () => {
registerTestSuite(suiteOrTest);
});
}
}
}
registerTestSuite(nodeTestsSuites);
});
describeIf(ferricExample, "ferric-example", () => {
it("exports a callable sum function", () => {
const exampleAddon =
/* eslint-disable-next-line @typescript-eslint/no-require-imports -- TODO: Determine why a dynamic import doesn't work on Android */
require("@react-native-node-api/ferric-example") as typeof import("@react-native-node-api/ferric-example");
const result = exampleAddon.sum(1, 3);
if (result !== 4) {
throw new Error(`Expected 1 + 3 to equal 4, but got ${result}`);
}
});
});
}
export default function App() {
return (
<MochaRemoteProvider tests={loadTests}>
<SafeAreaView style={styles.container}>
<ConnectionText style={styles.connectionText} />
<View style={styles.statusContainer}>
<StatusEmoji style={styles.statusEmoji} />
<StatusText style={styles.statusText} />
</View>
</SafeAreaView>
</MochaRemoteProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
statusContainer: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
statusEmoji: {
fontSize: 30,
margin: 30,
textAlign: "center",
},
statusText: {
fontSize: 20,
margin: 20,
textAlign: "center",
},
connectionText: {
textAlign: "center",
},
});