From: webfansplz <308241863@qq.com> Date: Tue, 10 Aug 2021 17:59:58 +0000 (+0800) Subject: test(reactivity): add test case for effectScope (#4239) X-Git-Tag: v3.2.2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e04680b0aad233edd541500f906cad6ec7d3c5df;p=thirdparty%2Fvuejs%2Fcore.git test(reactivity): add test case for effectScope (#4239) --- diff --git a/packages/reactivity/__tests__/effectScope.spec.ts b/packages/reactivity/__tests__/effectScope.spec.ts index a9e4ab0c79..6028882772 100644 --- a/packages/reactivity/__tests__/effectScope.spec.ts +++ b/packages/reactivity/__tests__/effectScope.spec.ts @@ -192,6 +192,25 @@ describe('reactivity/effect/scope', () => { expect(dummy).toBe(7) }) + it('should warn onDispose() is called when there is no active effect scope', () => { + const spy = jest.fn() + const scope = new EffectScope() + scope.run(() => { + onScopeDispose(spy) + }) + + expect(spy).toHaveBeenCalledTimes(0) + + onScopeDispose(spy) + + expect( + '[Vue warn] onDispose() is called when there is no active effect scope to be associated with.' + ).toHaveBeenWarned() + + scope.stop() + expect(spy).toHaveBeenCalledTimes(1) + }) + it('should derefence child scope from parent scope after stopping child scope (no memleaks)', async () => { const parent = new EffectScope() const child = parent.run(() => new EffectScope())! diff --git a/packages/reactivity/src/effectScope.ts b/packages/reactivity/src/effectScope.ts index db1ebdea51..05a700be43 100644 --- a/packages/reactivity/src/effectScope.ts +++ b/packages/reactivity/src/effectScope.ts @@ -98,7 +98,7 @@ export function onScopeDispose(fn: () => void) { activeEffectScope.cleanups.push(fn) } else if (__DEV__) { warn( - `onDispose() is called when there is no active effect scope ` + + `onDispose() is called when there is no active effect scope` + ` to be associated with.` ) }