-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 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
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,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() | ||
}) | ||
}) |