-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for
react-query
package (#1375)
# 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
1 parent
41c7e66
commit 6662e3f
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters