@@ -1524,7 +1524,8 @@ describe('useSWR - local mutation', () => {
1524
1524
)
1525
1525
// call bound mutate
1526
1526
fireEvent . click ( container . firstElementChild )
1527
- // expect new updated value
1527
+ // expect new updated value (after a tick)
1528
+ await 0
1528
1529
expect ( container . firstChild . textContent ) . toMatchInlineSnapshot (
1529
1530
`"data: mutated"`
1530
1531
)
@@ -1547,6 +1548,7 @@ describe('useSWR - local mutation', () => {
1547
1548
expect ( container . textContent ) . toMatchInlineSnapshot ( `"1"` ) // directly from cache
1548
1549
await act ( ( ) => new Promise ( res => setTimeout ( res , 150 ) ) ) // still suspending
1549
1550
mutate ( 'mutate-2' , 3 ) // set it to 3. this will drop the ongoing request
1551
+ await 0
1550
1552
expect ( container . textContent ) . toMatchInlineSnapshot ( `"3"` )
1551
1553
await act ( ( ) => new Promise ( res => setTimeout ( res , 100 ) ) )
1552
1554
expect ( container . textContent ) . toMatchInlineSnapshot ( `"3"` )
@@ -1568,9 +1570,14 @@ describe('useSWR - local mutation', () => {
1568
1570
await act ( ( ) => new Promise ( res => setTimeout ( res , 250 ) ) )
1569
1571
expect ( container . textContent ) . toMatchInlineSnapshot ( `"off"` ) // Initial state
1570
1572
1573
+ mutate ( 'mutate-3' , 'on' , false )
1574
+
1575
+ // Validate local state is now "on"
1576
+ await 0
1577
+ expect ( container . textContent ) . toMatchInlineSnapshot ( `"on"` )
1578
+
1571
1579
// Simulate toggling "on"
1572
1580
await act ( async ( ) => {
1573
- mutate ( 'mutate-3' , 'on' , false )
1574
1581
expect (
1575
1582
mutate (
1576
1583
'mutate-3' ,
@@ -1585,12 +1592,14 @@ describe('useSWR - local mutation', () => {
1585
1592
) . resolves . toBe ( 'on' )
1586
1593
} )
1587
1594
1588
- // Validate local state is now "on"
1589
- expect ( container . textContent ) . toMatchInlineSnapshot ( `"on"` )
1595
+ mutate ( 'mutate-3' , 'off' , false )
1596
+
1597
+ // Validate local state is now "off"
1598
+ await 0
1599
+ expect ( container . textContent ) . toMatchInlineSnapshot ( `"off"` )
1590
1600
1591
1601
// Simulate toggling "off"
1592
1602
await act ( async ( ) => {
1593
- mutate ( 'mutate-3' , 'off' , false )
1594
1603
expect (
1595
1604
mutate (
1596
1605
'mutate-3' ,
@@ -1605,15 +1614,12 @@ describe('useSWR - local mutation', () => {
1605
1614
) . resolves . toBe ( 'off' )
1606
1615
} )
1607
1616
1608
- // Validate local state is now "off"
1609
- expect ( container . textContent ) . toMatchInlineSnapshot ( `"off"` )
1610
-
1611
1617
// Wait for toggling "on" promise to resolve, but the "on" mutation is cancelled
1612
- await act ( ( ) => new Promise ( res => setTimeout ( res , 200 ) ) )
1618
+ await act ( ( ) => new Promise ( res => setTimeout ( res , 210 ) ) )
1613
1619
expect ( container . textContent ) . toMatchInlineSnapshot ( `"off"` )
1614
1620
1615
1621
// Wait for toggling "off" promise to resolve
1616
- await act ( ( ) => new Promise ( res => setTimeout ( res , 200 ) ) )
1622
+ await act ( ( ) => new Promise ( res => setTimeout ( res , 210 ) ) )
1617
1623
expect ( container . textContent ) . toMatchInlineSnapshot ( `"off"` )
1618
1624
} )
1619
1625
@@ -1731,6 +1737,39 @@ describe('useSWR - local mutation', () => {
1731
1737
expect ( ref ) . toEqual ( refs [ 0 ] )
1732
1738
}
1733
1739
} )
1740
+
1741
+ it ( 'should dedupe synchronous mutations' , async ( ) => {
1742
+ const mutationRecivedValues = [ ]
1743
+ const renderRecivedValues = [ ]
1744
+
1745
+ function Component ( ) {
1746
+ const { data, mutate : boundMutate } = useSWR ( 'mutate-6' , ( ) => 0 )
1747
+
1748
+ useEffect ( ( ) => {
1749
+ setTimeout ( ( ) => {
1750
+ // let's mutate twice, synchronously
1751
+ boundMutate ( v => {
1752
+ mutationRecivedValues . push ( v ) // should be 0
1753
+ return 1
1754
+ } , false )
1755
+ boundMutate ( v => {
1756
+ mutationRecivedValues . push ( v ) // should be 1
1757
+ return 2
1758
+ } , false )
1759
+ } , 1 )
1760
+ } , [ ] )
1761
+
1762
+ renderRecivedValues . push ( data ) // should be 0 -> 2, never render 1 in between
1763
+ return null
1764
+ }
1765
+
1766
+ render ( < Component /> )
1767
+
1768
+ await act ( ( ) => new Promise ( res => setTimeout ( res , 50 ) ) )
1769
+
1770
+ expect ( mutationRecivedValues ) . toEqual ( [ 0 , 1 ] )
1771
+ expect ( renderRecivedValues ) . toEqual ( [ undefined , 0 , 2 ] )
1772
+ } )
1734
1773
} )
1735
1774
1736
1775
describe ( 'useSWR - context configs' , ( ) => {
0 commit comments