Screens with a <Scene> can be tested with Jest (jest-expo) and React Native Testing Library. Tests run in Node with no GPU, so mock Apricot with @apricotjs/react-native/mock.
In the mock, <Scene> is a plain View with the testID apricot-scene. 3D components draw nothing but render their children, so your own components and the text in a <Label> can still be queried. useLayout and fileStorage are the real ones, so you can test layout logic such as adding, moving, undoing and saving. See What the mock returns for the rest.
Setup#
Apricot ships TypeScript sources. Let Jest compile them:
module.exports = {
preset: "jest-expo",
setupFiles: ["<rootDir>/jest.setup.js"],
transformIgnorePatterns: [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@apricotjs/.*)",
],
};Mock the entry points your screens use, in each test file or once in the setup file that setupFiles names:
jest.mock("@apricotjs/react-native", () => require("@apricotjs/react-native/mock"));
jest.mock("@apricotjs/react-native/model", () => require("@apricotjs/react-native/mock"));
jest.mock("@apricotjs/react-native/capture", () => require("@apricotjs/react-native/mock"));A test#
import { fireEvent, render, screen } from "@testing-library/react-native";
import { Showroom } from "./Showroom";
jest.mock("@apricotjs/react-native", () => require("@apricotjs/react-native/mock"));
jest.mock("@apricotjs/react-native/model", () => require("@apricotjs/react-native/mock"));
test("shows the scene and the colour picker", () => {
render(<Showroom />);
expect(screen.getByTestId("apricot-scene")).toBeTruthy();
fireEvent.press(screen.getByText("Red"));
expect(screen.getByText("Red")).toBeTruthy();
});What the mock returns#
| Export | In the mock |
|---|---|
<Scene> |
A View (testID="apricot-scene") with its style and children |
| Every other component | Renders its children only. A ref gets a handle whose methods do nothing |
useFrame, useSceneTouches, useAttach, useTransform |
Do nothing |
useScene, useParent, usePath, useTrip, useWaves, useShore |
Objects whose properties and methods do nothing |
useAnimatable(value) |
Returns a reader that gives value |
useDaylight() |
Returns a reader that gives null |
useModel |
{ object: null, animations: [], error: null } |
useTexture, useTextureState |
null, { texture: null, error: null } |
captureScene |
Resolves { uri: "file:///apricot/capture.png", width: 1, height: 1 } |
gpuSupport |
Resolves "webgpu" |
captureUri |
null |
deleteCapture, clearCaptures |
false, 0 |
useBounds |
null |
leaveOutOfFootprint |
Does nothing |
MOONLIT, DAY and other constants |
Objects that do nothing |
download, isDownloaded, measureModel |
download and measureModel resolve at once (measureModel to a 1 m box); isDownloaded returns true |
useLayout, fileStorage |
The real ones |
Scenes that start, or fail#
By default the mock <Scene> never starts, so onCreated and onReady aren't called. For flows that wait on the scene (a loading state, a fallback for phones without WebGPU), set how every mock scene behaves:
import { mockScene } from "@apricotjs/react-native/mock";
beforeEach(() => mockScene({ ready: true })); // calls onCreated, then onReady, after mounting
afterEach(() => mockScene()); // back to never starting
test("shows the fallback when WebGPU is missing", () => {
mockScene({ error: new Error("no WebGPU") }); // calls onError
// render and assert your fallback here
});Taps on 3D objects can't be simulated. Test what your handlers do by calling them, or by pressing the React Native buttons that drive the scene.
Related#
- Placement:
useLayout. - Installation.