diff --git a/tests/vanilla/memoryleaks.test.ts b/tests/vanilla/memoryleaks.test.ts index b1ea7821b2..a3fd98d46e 100644 --- a/tests/vanilla/memoryleaks.test.ts +++ b/tests/vanilla/memoryleaks.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest' import { atom, createStore } from 'jotai/vanilla' import type { Atom } from 'jotai/vanilla' -describe('test memory leaks (get & set only)', () => { +describe('memory leaks (get & set only)', () => { it('one atom', async () => { const store = createStore() let objAtom: Atom | undefined = atom({}) @@ -48,7 +48,7 @@ describe('test memory leaks (get & set only)', () => { }) }) -describe('test memory leaks (with subscribe)', () => { +describe('memory leaks (with subscribe)', () => { it('one atom', async () => { const store = createStore() let objAtom: Atom | undefined = atom({}) @@ -91,3 +91,45 @@ describe('test memory leaks (with subscribe)', () => { expect(await detector.isLeaking()).toBe(false) }) }) + +describe('memory leaks (with dependencies)', () => { + it('sync dependency', async () => { + const store = createStore() + let objAtom: Atom | undefined = atom({}) + const detector = new LeakDetector(store.get(objAtom)) + const atom1 = atom(0) + const atom2 = atom((get) => get(atom1) || (objAtom && get(objAtom))) + store.sub(atom2, () => {}) + store.set(atom1, 1) + objAtom = undefined + expect(await detector.isLeaking()).toBe(false) + }) + + it('async dependency', async () => { + const store = createStore() + let objAtom: Atom | undefined = atom({}) + const detector = new LeakDetector(store.get(objAtom)) + const atom1 = atom(0) + const atom2 = atom(async (get) => get(atom1) || (objAtom && get(objAtom))) + store.sub(atom2, () => {}) + store.set(atom1, 1) + objAtom = undefined + await Promise.resolve() + expect(await detector.isLeaking()).toBe(false) + }) + + it('async await dependency', async () => { + const store = createStore() + let objAtom: Atom | undefined = atom({}) + const detector = new LeakDetector(store.get(objAtom)) + const atom1 = atom(0) + const atom2 = atom(async (get) => { + await Promise.resolve() + return get(atom1) || (objAtom && get(objAtom)) + }) + store.sub(atom2, () => {}) + store.set(atom1, 1) + objAtom = undefined + expect(await detector.isLeaking()).toBe(false) + }) +})