Skip to content

Commit

Permalink
test: update the test for sync mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Mar 1, 2021
1 parent a1fabda commit cb05301
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, render, screen, fireEvent } from '@testing-library/react'
import React, { useEffect, useState } from 'react'
import { unstable_batchedUpdates } from 'react-dom'
import useSWR, { mutate, cache } from '../src'
import { sleep } from './utils'

Expand Down Expand Up @@ -502,7 +503,7 @@ describe('useSWR - local mutation', () => {
}
})

it('should dedupe synchronous mutations', async () => {
it('should apply synchronous mutations independently', async () => {
const mutationRecivedValues = []
const renderRecivedValues = []

Expand All @@ -511,15 +512,19 @@ describe('useSWR - local mutation', () => {

useEffect(() => {
setTimeout(() => {
// let's mutate twice, synchronously
boundMutate(v => {
mutationRecivedValues.push(v) // should be 0
return 1
}, false)
boundMutate(v => {
mutationRecivedValues.push(v) // should be 1
return 2
}, false)
// swr no longer batch sync mutations
// so you have to batch the updates on yourself
unstable_batchedUpdates(() => {
// let's mutate twice, synchronously
boundMutate(v => {
mutationRecivedValues.push(v) // should be 0
return 1
}, false)
boundMutate(v => {
mutationRecivedValues.push(v) // should be 1
return 2
}, false)
})
}, 1)
// the mutate function is guaranteed to be the same reference
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down

0 comments on commit cb05301

Please sign in to comment.