From 31df90ffb025c13a31f711bf33bf3d4f51be9bb5 Mon Sep 17 00:00:00 2001 From: David Maskasky Date: Tue, 7 Jan 2025 22:10:54 -0800 Subject: [PATCH] test: should update dependents with the value of the unwrapped atom when the promise resolves --- src/vanilla/utils/unwrap.ts | 3 ++- tests/vanilla/utils/unwrap.test.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/vanilla/utils/unwrap.ts b/src/vanilla/utils/unwrap.ts index 2504f68d51..2a6cc8c37c 100644 --- a/src/vanilla/utils/unwrap.ts +++ b/src/vanilla/utils/unwrap.ts @@ -96,7 +96,7 @@ export function unwrap( promiseAndValueAtom.debugPrivate = true } - return atom( + const unwrappedAtom = atom( (get) => { const state = get(promiseAndValueAtom) if ('f' in state) { @@ -108,6 +108,7 @@ export function unwrap( (_get, set, ...args) => set(anAtom as WritableAtom, ...args), ) + return unwrappedAtom }, anAtom, fallback, diff --git a/tests/vanilla/utils/unwrap.test.ts b/tests/vanilla/utils/unwrap.test.ts index be294dde54..2e0164337b 100644 --- a/tests/vanilla/utils/unwrap.test.ts +++ b/tests/vanilla/utils/unwrap.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' import { atom, createStore } from 'jotai/vanilla' +import type { Atom, Getter, Setter } from 'jotai/vanilla' import { unwrap } from 'jotai/vanilla/utils' describe('unwrap', () => { @@ -150,3 +151,17 @@ describe('unwrap', () => { expect(store.get(syncAtom)).toEqual('concrete') }) }) + +it.only('should update dependents with the value of the unwrapped atom when the promise resolves', async () => { + const store = createStore() + const asyncTarget = atom(() => Promise.resolve('value')) + const target = unwrap(asyncTarget) + const results: string[] = [] + const derived = atom(async (get) => { + await Promise.resolve() + results.push('effect ' + get(target)) + }) + store.sub(derived, () => {}) + await new Promise((r) => setTimeout(r)) + expect(results).toEqual(['effect undefined', 'effect value']) +})