From e0442155d7b95138c155b108cc3ce45d6c9d6d4f Mon Sep 17 00:00:00 2001 From: ygj6 Date: Sat, 10 Jul 2021 17:39:36 +0800 Subject: [PATCH] chore(watchEffect): migrating watchEffect warn and test cases from vue3. (#757) --- src/apis/watch.ts | 17 +++++++++++++++-- test/apis/watch.spec.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/apis/watch.ts b/src/apis/watch.ts index a03ba55d..67529575 100644 --- a/src/apis/watch.ts +++ b/src/apis/watch.ts @@ -103,8 +103,6 @@ function getWatcherOption(options?: Partial): WatchOptions { function getWatchEffectOption(options?: Partial): WatchOptions { return { ...{ - immediate: true, - deep: false, flush: 'pre', }, ...options, @@ -208,6 +206,21 @@ function createWatcher( cb: WatchCallback | null, options: WatchOptions ): () => void { + if (__DEV__ && !cb) { + if (options.immediate !== undefined) { + warn( + `watch() "immediate" option is only respected when using the ` + + `watch(source, callback, options?) signature.` + ) + } + if (options.deep !== undefined) { + warn( + `watch() "deep" option is only respected when using the ` + + `watch(source, callback, options?) signature.` + ) + } + } + const flushMode = options.flush const isSync = flushMode === 'sync' let cleanup: (() => void) | null diff --git a/test/apis/watch.spec.js b/test/apis/watch.spec.js index ab946e11..0bb8aeb9 100644 --- a/test/apis/watch.spec.js +++ b/test/apis/watch.spec.js @@ -446,6 +446,40 @@ describe('api/watch', () => { vm.count++ expect(spy).toHaveBeenLastCalledWith(1) }) + + it('warn immediate option when using effect', async () => { + const count = ref(0) + let dummy + watchEffect( + () => { + dummy = count.value + }, + { immediate: false } + ) + expect(dummy).toBe(0) + expect(`"immediate" option is only respected`).toHaveBeenWarned() + + count.value++ + await nextTick() + expect(dummy).toBe(1) + }) + + it('warn and not respect deep option when using effect', async () => { + const arr = ref([1, [2]]) + const spy = jest.fn() + watchEffect( + () => { + spy() + return arr + }, + { deep: true } + ) + expect(spy).toHaveBeenCalledTimes(1) + arr.value[1][0] = 3 + await nextTick() + expect(spy).toHaveBeenCalledTimes(1), + expect(`"deep" option is only respected`).toHaveBeenWarned() + }) }) describe('Multiple sources', () => {