Skip to content

Commit

Permalink
refactor(test-utils): add package for test utils (#256)
Browse files Browse the repository at this point in the history
# Overview

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

I add new dev package @suspensive/test-utils to gather test-utils
resolve
#234 (comment)

## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.
  • Loading branch information
manudeli authored Oct 24, 2023
1 parent fc3acd8 commit 3c3928c
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 90 deletions.
6 changes: 6 additions & 0 deletions .changeset/real-hairs-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@suspensive/react-await": patch
"@suspensive/react": patch
---

refactor: add @suspensive/test-utils as dev dependency to remove unnecessary repetitive code
6 changes: 6 additions & 0 deletions configs/test-utils/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
root: true,
extends: ['@suspensive/eslint-config/react-ts'],
ignorePatterns: ['*.js*', 'dist', 'coverage'],
}
28 changes: 28 additions & 0 deletions configs/test-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@suspensive/test-utils",
"version": "0.0.0",
"private": true,
"funding": {
"type": "github",
"url": "https://github.com/sponsors/manudeli"
},
"license": "MIT",
"author": {
"name": "Jonghyeon Ko",
"email": "[email protected]"
},
"sideEffects": false,
"type": "module",
"main": "src/index.ts",
"scripts": {
"lint": "eslint \"**/*.ts*\"",
"type:check": "tsc --noEmit"
},
"devDependencies": {
"@types/react": "^18.2.0",
"react": "^18.2.0"
},
"peerDependencies": {
"react": "^16.8 || ^17 || ^18"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ export const Suspend = ({ during, toShow }: SuspendProps) => {
Suspend.reset = () => {
suspendIsNeed.current = true
}

export const delay = (ms: number) => new Promise((resolve) => setTimeout(() => resolve('done'), ms))

export const TEXT = 'TEXT' as const
export const ERROR_MESSAGE = 'ERROR_MESSAGE' as const
export const FALLBACK = 'FALLBACK' as const
export const MS_100 = 100 as const
33 changes: 33 additions & 0 deletions configs/test-utils/src/ThrowError.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { PropsWithChildren } from 'react'
import { useEffect, useState } from 'react'

const useSetTimeout = (fn: (...args: []) => void, delay: number) =>
useEffect(() => {
const timeout = setTimeout(fn, delay)
return () => clearTimeout(timeout)
}, [fn, delay])

const throwErrorIsNeed = { current: false }
type ThrowErrorProps = PropsWithChildren<{ message: string; after?: number }>
export const ThrowError = ({ message, after = 0, children }: ThrowErrorProps) => {
const [isNeedError, setIsNeedError] = useState(after === 0 ? true : throwErrorIsNeed.current)
if (isNeedError) {
throw new Error(message)
}
useSetTimeout(() => setIsNeedError(true), after)
return <>{children}</>
}

type ThrowNullProps = PropsWithChildren<{ after: number }>
export const ThrowNull = ({ after, children }: ThrowNullProps) => {
const [isNeedError, setIsNeedError] = useState(throwErrorIsNeed.current)
if (isNeedError) {
throw null
}
useSetTimeout(() => setIsNeedError(true), after)
return <>{children}</>
}

ThrowError.reset = () => {
throwErrorIsNeed.current = false
}
1 change: 1 addition & 0 deletions configs/test-utils/src/delay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const delay = (ms: number) => new Promise((resolve) => setTimeout(() => resolve('done'), ms))
7 changes: 7 additions & 0 deletions configs/test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { Suspend } from './Suspend'
export { ThrowError, ThrowNull } from './ThrowError'
export { delay } from './delay'
export const TEXT = 'TEXT' as const
export const ERROR_MESSAGE = 'ERROR_MESSAGE' as const
export const FALLBACK = 'FALLBACK' as const
export const MS_100 = 100 as const
7 changes: 7 additions & 0 deletions configs/test-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@suspensive/tsconfig/react-library.json",
"include": ["."],
"compilerOptions": {
"types": ["@testing-library/jest-dom"]
}
}
1 change: 1 addition & 0 deletions packages/react-await/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
},
"devDependencies": {
"@suspensive/react": "workspace:*",
"@suspensive/test-utils": "workspace:*",
"@suspensive/tsup": "workspace:*",
"@suspensive/vitest": "workspace:*",
"@types/node": "^18.16.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-await/src/Await.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorBoundary, Suspense } from '@suspensive/react'
import { ERROR_MESSAGE, FALLBACK, MS_100, TEXT, delay } from '@suspensive/test-utils'
import { act, render, screen, waitFor } from '@testing-library/react'
import { vi } from 'vitest'
import { ERROR_MESSAGE, FALLBACK, MS_100, TEXT, delay } from './utils/toTest'
import { Await, awaitClient, useAwait } from '.'

const key = (id: number) => ['key', id] as const
Expand Down
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"type:check": "tsc --noEmit"
},
"devDependencies": {
"@suspensive/test-utils": "workspace:*",
"@suspensive/tsup": "workspace:*",
"@suspensive/vitest": "workspace:*",
"@types/node": "^18.16.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/AsyncBoundary.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ERROR_MESSAGE, FALLBACK, MS_100, Suspend, TEXT, ThrowError } from '@suspensive/test-utils'
import { act, render, waitFor } from '@testing-library/react'
import type { ComponentProps } from 'react'
import { createElement } from 'react'
import { createRoot } from 'react-dom/client'
import { vi } from 'vitest'
import { ERROR_MESSAGE, FALLBACK, MS_100, Suspend, TEXT, ThrowError } from './utils/toTest'
import { AsyncBoundary, withAsyncBoundary } from '.'

let container = document.createElement('div')
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Delay.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MS_100, TEXT } from '@suspensive/test-utils'
import { act, render, screen, waitFor } from '@testing-library/react'
import { vi } from 'vitest'
import { MS_100, TEXT } from './utils/toTest'
import { Delay, withDelay } from '.'

describe('<Delay/>', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/ErrorBoundary.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ERROR_MESSAGE, FALLBACK, MS_100, TEXT, ThrowError, ThrowNull } from '@suspensive/test-utils'
import { act, render } from '@testing-library/react'
import type { ComponentProps, ComponentRef } from 'react'
import { createElement, createRef } from 'react'
import { createRoot } from 'react-dom/client'
import { vi } from 'vitest'
import { useSetTimeout } from './hooks'
import { assert } from './utils'
import { ERROR_MESSAGE, FALLBACK, MS_100, TEXT, ThrowError, ThrowNull } from './utils/toTest'
import { ErrorBoundary, useErrorBoundary, useErrorBoundaryFallbackProps, withErrorBoundary } from '.'

let container = document.createElement('div')
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/ErrorBoundaryGroup.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ERROR_MESSAGE, MS_100, TEXT, ThrowError } from '@suspensive/test-utils'
import { act, render, screen } from '@testing-library/react'
import { createElement } from 'react'
import { vi } from 'vitest'
import { assert } from './utils'
import { ERROR_MESSAGE, MS_100, TEXT, ThrowError } from './utils/toTest'
import { ErrorBoundary, ErrorBoundaryGroup, useErrorBoundaryGroup, withErrorBoundaryGroup } from '.'

const innerErrorBoundaryCount = 3
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Suspense.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FALLBACK, MS_100, Suspend, TEXT } from '@suspensive/test-utils'
import { act, render, screen, waitFor } from '@testing-library/react'
import { createElement } from 'react'
import { vi } from 'vitest'
import { FALLBACK, MS_100, Suspend, TEXT } from './utils/toTest'
import { Suspense, withSuspense } from '.'

describe('<Suspense/>', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/SuspensiveProvider.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FALLBACK, MS_100, Suspend, TEXT } from '@suspensive/test-utils'
import { act, render, screen, waitFor } from '@testing-library/react'
import { vi } from 'vitest'
import { FALLBACK, MS_100, Suspend, TEXT } from './utils/toTest'
import { Delay, Suspense, Suspensive, SuspensiveProvider } from '.'

const FALLBACK_GLOBAL = 'FALLBACK_GLOBAL'
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useSetTimeout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MS_100 } from '@suspensive/test-utils'
import { act, renderHook } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
import { MS_100 } from '../utils/toTest'
import { useSetTimeout } from '.'

vi.useFakeTimers()
Expand Down
52 changes: 0 additions & 52 deletions packages/react/src/utils/toTest.tsx

This file was deleted.

57 changes: 36 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"build:watch": { "outputs": ["dist/**"] },
"npm:publish": { "dependsOn": ["^build"] },
"lint": { "cache": false },
"lint:attw": { "dependsOn": ["^build"], "cache": false },
"lint:attw": { "dependsOn": ["^build"], "cache": false },
"lint:pub": { "cache": false },
"test": { "outputs": ["coverage/**"], "cache": false },
"test": { "dependsOn": ["^build"], "outputs": ["coverage/**"], "cache": false },
"test:tsd": {},
"test:watch": { "cache": false },
"prepack": { "dependsOn": ["^prepack"], "cache": false },
Expand Down

0 comments on commit 3c3928c

Please sign in to comment.