@tanstack/react-query 5.0.0-alpha.12 → 5.0.0-alpha.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-query",
3
- "version": "5.0.0-alpha.12",
3
+ "version": "5.0.0-alpha.19",
4
4
  "description": "Hooks for managing, caching and syncing asynchronous and remote data in React",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "react-error-boundary": "^3.1.4"
36
36
  },
37
37
  "dependencies": {
38
- "@tanstack/query-core": "5.0.0-alpha.12"
38
+ "@tanstack/query-core": "5.0.0-alpha.19"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": "^18.0.0",
@@ -1728,6 +1728,159 @@ describe('useQuery', () => {
1728
1728
  })
1729
1729
  })
1730
1730
 
1731
+ it('should keep the previous data when placeholderData is set and select fn transform is used', async () => {
1732
+ const key = queryKey()
1733
+ const states: UseQueryResult<number>[] = []
1734
+
1735
+ function Page() {
1736
+ const [count, setCount] = React.useState(0)
1737
+
1738
+ const state = useQuery({
1739
+ queryKey: [key, count],
1740
+ queryFn: async () => {
1741
+ await sleep(10)
1742
+ return {
1743
+ count,
1744
+ }
1745
+ },
1746
+ select(data) {
1747
+ return data.count
1748
+ },
1749
+ placeholderData: keepPreviousData,
1750
+ })
1751
+
1752
+ states.push(state)
1753
+
1754
+ return (
1755
+ <div>
1756
+ <div>data: {state.data}</div>
1757
+ <button onClick={() => setCount(1)}>setCount</button>
1758
+ </div>
1759
+ )
1760
+ }
1761
+
1762
+ const rendered = renderWithClient(queryClient, <Page />)
1763
+
1764
+ await waitFor(() => rendered.getByText('data: 0'))
1765
+
1766
+ fireEvent.click(rendered.getByRole('button', { name: 'setCount' }))
1767
+
1768
+ await waitFor(() => rendered.getByText('data: 1'))
1769
+
1770
+ // Initial
1771
+ expect(states[0]).toMatchObject({
1772
+ data: undefined,
1773
+ isFetching: true,
1774
+ isSuccess: false,
1775
+ isPlaceholderData: false,
1776
+ })
1777
+ // Fetched
1778
+ expect(states[1]).toMatchObject({
1779
+ data: 0,
1780
+ isFetching: false,
1781
+ isSuccess: true,
1782
+ isPlaceholderData: false,
1783
+ })
1784
+ // Set state
1785
+ expect(states[2]).toMatchObject({
1786
+ data: 0,
1787
+ isFetching: true,
1788
+ isSuccess: true,
1789
+ isPlaceholderData: true,
1790
+ })
1791
+ // New data
1792
+ expect(states[3]).toMatchObject({
1793
+ data: 1,
1794
+ isFetching: false,
1795
+ isSuccess: true,
1796
+ isPlaceholderData: false,
1797
+ })
1798
+ })
1799
+
1800
+ it('should show placeholderData between multiple pending queries when select fn transform is used', async () => {
1801
+ const key = queryKey()
1802
+ const states: UseQueryResult<number>[] = []
1803
+
1804
+ function Page() {
1805
+ const [count, setCount] = React.useState(0)
1806
+
1807
+ const state = useQuery({
1808
+ queryKey: [key, count],
1809
+ queryFn: async () => {
1810
+ await sleep(10)
1811
+ return {
1812
+ count,
1813
+ }
1814
+ },
1815
+ select(data) {
1816
+ return data.count
1817
+ },
1818
+ placeholderData: keepPreviousData,
1819
+ })
1820
+
1821
+ states.push(state)
1822
+
1823
+ return (
1824
+ <div>
1825
+ <div>data: {state.data}</div>
1826
+ <button onClick={() => setCount((prev) => prev + 1)}>setCount</button>
1827
+ </div>
1828
+ )
1829
+ }
1830
+
1831
+ const rendered = renderWithClient(queryClient, <Page />)
1832
+
1833
+ await waitFor(() => rendered.getByText('data: 0'))
1834
+
1835
+ fireEvent.click(rendered.getByRole('button', { name: 'setCount' }))
1836
+ fireEvent.click(rendered.getByRole('button', { name: 'setCount' }))
1837
+ fireEvent.click(rendered.getByRole('button', { name: 'setCount' }))
1838
+
1839
+ await waitFor(() => rendered.getByText('data: 3'))
1840
+ // Initial
1841
+ expect(states[0]).toMatchObject({
1842
+ data: undefined,
1843
+ isFetching: true,
1844
+ isSuccess: false,
1845
+ isPlaceholderData: false,
1846
+ })
1847
+ // Fetched
1848
+ expect(states[1]).toMatchObject({
1849
+ data: 0,
1850
+ isFetching: false,
1851
+ isSuccess: true,
1852
+ isPlaceholderData: false,
1853
+ })
1854
+ // Set state -> count = 1
1855
+ expect(states[2]).toMatchObject({
1856
+ data: 0,
1857
+ isFetching: true,
1858
+ isSuccess: true,
1859
+ isPlaceholderData: true,
1860
+ })
1861
+ // Set state -> count = 2
1862
+ expect(states[3]).toMatchObject({
1863
+ data: 0,
1864
+ isFetching: true,
1865
+ isSuccess: true,
1866
+ isPlaceholderData: true,
1867
+ })
1868
+ // Set state -> count = 3
1869
+ expect(states[4]).toMatchObject({
1870
+ data: 0,
1871
+ isFetching: true,
1872
+ isSuccess: true,
1873
+ isPlaceholderData: true,
1874
+ })
1875
+ // New data
1876
+ expect(states[5]).toMatchObject({
1877
+ data: 3,
1878
+ isFetching: false,
1879
+ isSuccess: true,
1880
+ isPlaceholderData: false,
1881
+ })
1882
+ })
1883
+
1731
1884
  it('should transition to error state when placeholderData is set', async () => {
1732
1885
  const key = queryKey()
1733
1886
  const states: UseQueryResult<number>[] = []