Skip to content

Commit

Permalink
fix(error) always update error on revalidation (#93)
Browse files Browse the repository at this point in the history
* (test) add test case

Fixes #88
  • Loading branch information
charshin authored and darrenjennings committed Oct 12, 2020
1 parent fc09b19 commit 0c24ec1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/use-swrv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ const mutate = async <Data>(key: string, res: Promise<Data> | Data, cache = DATA
if (typeof newData.data !== 'undefined') {
r.data = newData.data
}
if (newData.error) {
r.error = newData.error
}
r.error = newData.error
r.isValidating = newData.isValidating

const isLast = idx === refs.length - 1
Expand Down
37 changes: 37 additions & 0 deletions tests/use-swrv.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,43 @@ describe('useSWRV - error', () => {
expect(vm.$el.textContent).toBe('count: 2 Error: uh oh!')
done()
})

it('should reset error if fetching succeeds', async done => {
let count = 0
let revalidate

const vm = new Vue({
template: `<div>count: {{ data }} {{ error }}</div>`,
setup () {
const { data, error, mutate } = useSWRV(
'error-4',
() => new Promise(
(resolve, reject) => setTimeout(() => ++count === 2 ? reject(new Error('uh oh!')) : resolve(count), 100)
),
{ dedupingInterval: 0 }
)
revalidate = mutate
return { data, error }
}
}).$mount()

timeout(100)
await tick(vm, 3)
expect(vm.$el.textContent).toBe('count: 1 ')

revalidate()
timeout(100)
await tick(vm, 3)
// stale data sticks around even when error exists
expect(vm.$el.textContent).toBe('count: 1 Error: uh oh!')

revalidate()
timeout(100)
await tick(vm, 3)
// error must be reset if fetching succeeds
expect(vm.$el.textContent).toBe('count: 3 ')
done()
})
})

describe('useSWRV - window events', () => {
Expand Down

0 comments on commit 0c24ec1

Please sign in to comment.