Skip to content

Commit

Permalink
add test for native context creation
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg committed Jun 5, 2024
1 parent 88c5969 commit 552186a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions package/example/Tests/native-contexts-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import NativeSampleModule from "sample-native-module/js/NativeSampleModule";
import { Worklets, type IWorkletContext } from "react-native-worklets-core";
import { Platform } from "react-native";

export const native_contexts_tests = {
// This test has to run first, so that the default context / RNWC API isn't initialized yet from the JS side
call_custom_worklet_context_first: async () => {
// Check if the native module is available
if (NativeSampleModule == null) {
if (Platform.OS === "android") {
console.warn(
"NativeSampleModule is only implemented on iOS currently."
);
return;
} else {
throw new Error(
"NativeSampleModule is not available. Something went wrong with the native module setup."
);
}
}

// Note to library authors: We call on our custom native context before the Worklets API is initialized.
// RNWC is a lazy-loaded turbo module, so it will only be available once we call it in JS once.
// We call any property on the Worklets object to initialize it:
Worklets.defaultContext;

const context =
NativeSampleModule.createCustomWorkletContext() as IWorkletContext;
// Bug: this will never resolve (as it throws an error, which is swallowed [which is another bug])
await context.runAsync(() => {
"worklet";
console.log("ctx: test: running worklet");
return 100;
});
},
};
2 changes: 2 additions & 0 deletions package/example/Tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { worklet_tests } from "./worklet-tests";
import { sharedvalue_tests } from "./sharedvalue-tests";
import { wrapper_tests } from "./wrapper-tests";
import { worklet_context_tests } from "./worklet-context-tests";
import { native_contexts_tests } from "./native-contexts-tests";

export const Tests: { [key: string]: { [key: string]: () => Promise<void> } } =
{
NativeContexts: { ...native_contexts_tests },
Worklets: { ...worklet_tests },
Contexts: { ...worklet_context_tests },
SharedValues: { ...sharedvalue_tests },
Expand Down

0 comments on commit 552186a

Please sign in to comment.