Skip to content

Commit

Permalink
test: add tests for react-query package (#1375)
Browse files Browse the repository at this point in the history
# Overview

<!--
    A clear and concise description of what this pr is about.
 -->

Issue #1292

For now, I managed to add tests for `postinstall.ts` and part of
`package.ts`

### AS-IS
![Captura de pantalla 2024-11-25 a las 6 47 22 p
 m](https://github.com/user-attachments/assets/e27ed362-68fe-4e4b-971e-a43f1715fa28)

### TO-BE
![Captura de pantalla 2024-11-25 a las 6 48 32 p
 m](https://github.com/user-attachments/assets/8f8dc937-4645-482d-987e-6574fa4c13dc)


## PR Checklist

- [✅] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
  • Loading branch information
saul-atomrigs authored Nov 26, 2024
1 parent 41c7e66 commit 6662e3f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/react-query/src/bin/postinstall.spec.ts
Original file line number Diff line number Diff line change
@@ -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.')
})
})
25 changes: 25 additions & 0 deletions packages/react-query/src/bin/utils/package.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import packageJson from '../../../package.json'
import {
getPackageJson,
getSuspensiveReactQueryPackageJson,
getTanStackReactQueryAPIs,
getTanStackReactQueryPackageJson,
getTargetSuspensiveReactQueryAPIs,
getTargetSuspensiveReactQueryVersion,
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 6662e3f

Please sign in to comment.