diff --git a/packages/react-query/src/bin/postinstall.spec.ts b/packages/react-query/src/bin/postinstall.spec.ts new file mode 100644 index 000000000..2428a487b --- /dev/null +++ b/packages/react-query/src/bin/postinstall.spec.ts @@ -0,0 +1,53 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { getTanStackReactQueryPackageJson } from './utils/package' +import { switchVersion } from './utils/switchVersion' + +vi.mock('./utils/package') +vi.mock('./utils/switchVersion') + +describe('postinstall', () => { + const mockConsoleWarn = vi.spyOn(console, 'warn') + const mockGetTanStackReactQueryPackageJson = vi.mocked(getTanStackReactQueryPackageJson) + const mockSwitchVersion = vi.mocked(switchVersion) + + const runPostInstall = async (version: string) => { + mockGetTanStackReactQueryPackageJson.mockReturnValue({ + name: 'tanstack-query', + version, + description: `TanStack Query v${version.split('.')[0]}`, + }) + + await import('./postinstall') + } + + beforeEach(() => { + vi.resetModules() + vi.clearAllMocks() + }) + + it('should switch to @suspensive/react-query-4 when @tanstack/react-query@^4 is installed', async () => { + await runPostInstall('4.2.3') + + expect(mockGetTanStackReactQueryPackageJson).toHaveBeenCalledTimes(1) + expect(mockSwitchVersion).toHaveBeenCalledWith(4) + expect(mockSwitchVersion).toHaveBeenCalledTimes(1) + expect(mockConsoleWarn).not.toHaveBeenCalled() + }) + + it('should switch to @suspensive/react-query-5 when @tanstack/react-query@^5 is installed', async () => { + await runPostInstall('5.2.3') + + expect(mockGetTanStackReactQueryPackageJson).toHaveBeenCalledTimes(1) + expect(mockSwitchVersion).toHaveBeenCalledWith(5) + expect(mockSwitchVersion).toHaveBeenCalledTimes(1) + expect(mockConsoleWarn).not.toHaveBeenCalled() + }) + + it('should show warning when unsupported version is installed', async () => { + await runPostInstall('3.3.4') + + expect(mockGetTanStackReactQueryPackageJson).toHaveBeenCalledTimes(1) + expect(mockSwitchVersion).not.toHaveBeenCalled() + expect(mockConsoleWarn).toHaveBeenCalledWith('[@suspensive/react-query]', 'version v3.3.4 is not supported.') + }) +}) diff --git a/packages/react-query/src/bin/utils/package.spec.ts b/packages/react-query/src/bin/utils/package.spec.ts index 0c04f64e5..3d68eae1d 100644 --- a/packages/react-query/src/bin/utils/package.spec.ts +++ b/packages/react-query/src/bin/utils/package.spec.ts @@ -3,6 +3,7 @@ import packageJson from '../../../package.json' import { getPackageJson, getSuspensiveReactQueryPackageJson, + getTanStackReactQueryAPIs, getTanStackReactQueryPackageJson, getTargetSuspensiveReactQueryAPIs, getTargetSuspensiveReactQueryVersion, @@ -64,6 +65,30 @@ describe('package', () => { expect(result).toBeDefined() expect(result.name).toBe('@suspensive/react-query-4') }) + + it('should return correct APIs for version 5', () => { + const apis = getTanStackReactQueryAPIs('5') + + expect(apis).toEqual([ + 'useSuspenseQuery', + 'useSuspenseQueries', + 'useSuspenseInfiniteQuery', + 'usePrefetchQuery', + 'usePrefetchInfiniteQuery', + 'queryOptions', + 'infiniteQueryOptions', + ]) + }) + + it('should return placeholder for version 4', () => { + const apis = getTanStackReactQueryAPIs('4') + + expect(apis).toEqual(['-']) + }) + + it('should throw error for missing version', () => { + expect(() => getTanStackReactQueryAPIs('')).toThrow('@tanstack/react-query version is required') + }) }) describe('loadModule', () => {