|
1 | 1 | import { renderHook, waitFor } from '@testing-library/react'; |
2 | 2 | import { beforeEach, describe, expect, it, vi } from 'vitest'; |
3 | 3 |
|
| 4 | +import { createDeferredPromise } from '../../../utils/createDeferredPromise'; |
4 | 5 | import { useSubscription } from '../useSubscription'; |
5 | 6 | import { createMockClerk, createMockOrganization, createMockQueryClient, createMockUser } from './mocks/clerk'; |
6 | 7 | import { wrapper } from './wrapper'; |
@@ -144,4 +145,72 @@ describe('useSubscription', () => { |
144 | 145 | expect(getSubscriptionSpy).toHaveBeenCalledTimes(1); |
145 | 146 | expect(result.current.isFetching).toBe(false); |
146 | 147 | }); |
| 148 | + |
| 149 | + it('retains previous data while refetching when keepPreviousData=true', async () => { |
| 150 | + const { result, rerender } = renderHook( |
| 151 | + ({ orgId, keepPreviousData }) => { |
| 152 | + mockOrganization = createMockOrganization({ id: orgId }); |
| 153 | + return useSubscription({ for: 'organization', keepPreviousData }); |
| 154 | + }, |
| 155 | + { |
| 156 | + wrapper, |
| 157 | + initialProps: { orgId: 'org_1', keepPreviousData: true }, |
| 158 | + }, |
| 159 | + ); |
| 160 | + |
| 161 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 162 | + expect(result.current.data).toEqual({ id: 'sub_org_org_1' }); |
| 163 | + |
| 164 | + const deferred = createDeferredPromise(); |
| 165 | + getSubscriptionSpy.mockImplementationOnce(() => deferred.promise as Promise<{ id: string }>); |
| 166 | + |
| 167 | + rerender({ orgId: 'org_2', keepPreviousData: true }); |
| 168 | + |
| 169 | + await waitFor(() => expect(result.current.isFetching).toBe(true)); |
| 170 | + |
| 171 | + // Slight difference in behavior between SWR and React Query, but acceptable for the migration. |
| 172 | + if (__CLERK_USE_RQ__) { |
| 173 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 174 | + } else { |
| 175 | + await waitFor(() => expect(result.current.isLoading).toBe(true)); |
| 176 | + } |
| 177 | + expect(result.current.data).toEqual({ id: 'sub_org_org_1' }); |
| 178 | + |
| 179 | + deferred.resolve({ id: 'sub_org_org_2' }); |
| 180 | + |
| 181 | + await waitFor(() => expect(result.current.data).toEqual({ id: 'sub_org_org_2' })); |
| 182 | + expect(getSubscriptionSpy).toHaveBeenCalledTimes(2); |
| 183 | + }); |
| 184 | + |
| 185 | + it('clears data while refetching when keepPreviousData=false', async () => { |
| 186 | + const { result, rerender } = renderHook( |
| 187 | + ({ orgId, keepPreviousData }) => { |
| 188 | + mockOrganization = createMockOrganization({ id: orgId }); |
| 189 | + return useSubscription({ for: 'organization', keepPreviousData }); |
| 190 | + }, |
| 191 | + { |
| 192 | + wrapper, |
| 193 | + initialProps: { orgId: 'org_1', keepPreviousData: false }, |
| 194 | + }, |
| 195 | + ); |
| 196 | + |
| 197 | + await waitFor(() => expect(result.current.isLoading).toBe(false)); |
| 198 | + expect(result.current.data).toEqual({ id: 'sub_org_org_1' }); |
| 199 | + |
| 200 | + const deferred = createDeferredPromise(); |
| 201 | + getSubscriptionSpy.mockImplementationOnce(() => deferred.promise as Promise<{ id: string }>); |
| 202 | + |
| 203 | + rerender({ orgId: 'org_2', keepPreviousData: false }); |
| 204 | + |
| 205 | + await waitFor(() => expect(result.current.isFetching).toBe(true)); |
| 206 | + expect(result.current.isLoading).toBe(true); |
| 207 | + expect(result.current.data).toBeUndefined(); |
| 208 | + |
| 209 | + deferred.resolve({ id: 'sub_org_org_2' }); |
| 210 | + |
| 211 | + await waitFor(() => expect(result.current.isFetching).toBe(false)); |
| 212 | + expect(result.current.data).toEqual({ id: 'sub_org_org_2' }); |
| 213 | + expect(result.current.isLoading).toBe(false); |
| 214 | + expect(getSubscriptionSpy).toHaveBeenCalledTimes(2); |
| 215 | + }); |
147 | 216 | }); |
0 commit comments