-
// vitest
test("async atomWithDefault", async () => {
let resolve = () => {
return;
};
const countAtom = atomWithDefault<number | Promise<number>>(async () => {
await new Promise<void>((r) => (resolve = r));
return 1;
});
const Counter = () => {
const [count, setCount] = useAtom(countAtom);
return (
<>
<div>count: {count}</div>
<button
onClick={() => {
setCount(count + 1);
}}
>
button
</button>
</>
);
};
const SuspenseCounter = () => {
return <>loading</>;
};
const store = createStore();
// route loader
store.get(countAtom);
resolve();
expect(await store.get(countAtom)).toBe(1);
const { getByText } = render(
<Provider store={store}>
<Suspense fallback={<SuspenseCounter />}>
<Counter />
</Suspense>
</Provider>,
);
if (await expect.element(getByText("loading")).toBeInTheDocument()) {
throw new Error("How to prevent this?");
}
}) |
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Nov 26, 2024
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Zebeqo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
unwrap(countAtom, () => 1 /* or any default value */)
should do. While there's no exact case, you might find Jotai Tips interesting.