Skip to content

Commit

Permalink
chore(watchEffect): migrating watchEffect warn and test cases from vu…
Browse files Browse the repository at this point in the history
…e3. (#757)
  • Loading branch information
ygj6 committed Jul 10, 2021
1 parent 38704f9 commit e044215
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/apis/watch.ts
Expand Up @@ -103,8 +103,6 @@ function getWatcherOption(options?: Partial<WatchOptions>): WatchOptions {
function getWatchEffectOption(options?: Partial<WatchOptions>): WatchOptions {
return {
...{
immediate: true,
deep: false,
flush: 'pre',
},
...options,
Expand Down Expand Up @@ -208,6 +206,21 @@ function createWatcher(
cb: WatchCallback<any> | 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
Expand Down
34 changes: 34 additions & 0 deletions test/apis/watch.spec.js
Expand Up @@ -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', () => {
Expand Down

0 comments on commit e044215

Please sign in to comment.