Skip to content

Commit e044215

Browse files
authored
chore(watchEffect): migrating watchEffect warn and test cases from vue3. (#757)
1 parent 38704f9 commit e044215

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/apis/watch.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ function getWatcherOption(options?: Partial<WatchOptions>): WatchOptions {
103103
function getWatchEffectOption(options?: Partial<WatchOptions>): WatchOptions {
104104
return {
105105
...{
106-
immediate: true,
107-
deep: false,
108106
flush: 'pre',
109107
},
110108
...options,
@@ -208,6 +206,21 @@ function createWatcher(
208206
cb: WatchCallback<any> | null,
209207
options: WatchOptions
210208
): () => void {
209+
if (__DEV__ && !cb) {
210+
if (options.immediate !== undefined) {
211+
warn(
212+
`watch() "immediate" option is only respected when using the ` +
213+
`watch(source, callback, options?) signature.`
214+
)
215+
}
216+
if (options.deep !== undefined) {
217+
warn(
218+
`watch() "deep" option is only respected when using the ` +
219+
`watch(source, callback, options?) signature.`
220+
)
221+
}
222+
}
223+
211224
const flushMode = options.flush
212225
const isSync = flushMode === 'sync'
213226
let cleanup: (() => void) | null

test/apis/watch.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,40 @@ describe('api/watch', () => {
446446
vm.count++
447447
expect(spy).toHaveBeenLastCalledWith(1)
448448
})
449+
450+
it('warn immediate option when using effect', async () => {
451+
const count = ref(0)
452+
let dummy
453+
watchEffect(
454+
() => {
455+
dummy = count.value
456+
},
457+
{ immediate: false }
458+
)
459+
expect(dummy).toBe(0)
460+
expect(`"immediate" option is only respected`).toHaveBeenWarned()
461+
462+
count.value++
463+
await nextTick()
464+
expect(dummy).toBe(1)
465+
})
466+
467+
it('warn and not respect deep option when using effect', async () => {
468+
const arr = ref([1, [2]])
469+
const spy = jest.fn()
470+
watchEffect(
471+
() => {
472+
spy()
473+
return arr
474+
},
475+
{ deep: true }
476+
)
477+
expect(spy).toHaveBeenCalledTimes(1)
478+
arr.value[1][0] = 3
479+
await nextTick()
480+
expect(spy).toHaveBeenCalledTimes(1),
481+
expect(`"deep" option is only respected`).toHaveBeenWarned()
482+
})
449483
})
450484

451485
describe('Multiple sources', () => {

0 commit comments

Comments
 (0)