Sfoglia il codice sorgente

test(reactivity): add test case for effectScope (#4239)

webfansplz 4 anni fa
parent
commit
e04680b0aa

+ 19 - 0
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())!

+ 1 - 1
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.`
     )
   }