Skip to content

Commit

Permalink
test: add more test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrior committed Jan 15, 2024
1 parent 78cedba commit 8d81184
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/@helpers/jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ import React from 'react'
// Define global React to solve "ReferenceError: React is not defined in jest tests"
// https://stackoverflow.com/questions/58980934/referenceerror-react-is-not-defined-in-jest-tests
global.React = React

// 不打印 console 错误信息
jest.spyOn(console, 'error').mockImplementation(jest.fn())
41 changes: 41 additions & 0 deletions test/core-react/components/ErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ErrorBoundary from '@core-react/components/ErrorBoundary'
import { render, screen } from '@testing-library/react'

const ERROR_TEXT = '_ERROR_FROM_COMPONENT_'

const ErrorComponent = () => {
throw new Error(ERROR_TEXT)
}

describe('ErrorBoundary 验证', () => {
test('组件抛出错误应该被捕获并展示出来', async () => {
render(
<ErrorBoundary>
<ErrorComponent />
</ErrorBoundary>
)
const node = screen.queryByText(new RegExp(ERROR_TEXT))
expect(node).toBeInTheDocument()
})

test('设置为 silent 模式时,组件错误不应该展示出来', async () => {
render(
<ErrorBoundary catchErrorTips="silent">
<ErrorComponent />
</ErrorBoundary>
)
const node = screen.queryByText(new RegExp(ERROR_TEXT))
expect(node).not.toBeInTheDocument()
})

test('自定义错误时,应该捕获错误并展示自定义的错误信息', async () => {
const CUSTOM_ERROR_TEXT = 'CUSTOM_ERROR_TIPS'
render(
<ErrorBoundary catchErrorTips={() => CUSTOM_ERROR_TEXT}>
<ErrorComponent />
</ErrorBoundary>
)
const node = screen.queryByText(new RegExp(CUSTOM_ERROR_TEXT))
expect(node).toBeInTheDocument()
})
})

0 comments on commit 8d81184

Please sign in to comment.