-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
3052bb6
commit 247cadf
Showing
7 changed files
with
151 additions
and
5 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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as assertComponent } from "./src/assertComponent.mjs"; | ||
export { default as assertComponents } from "./src/assertComponents.mjs"; | ||
export { default as mockComponent } from "./src/mockComponent.mjs"; | ||
export { default as TestErrorBoundary } from "./src/TestErrorBoundary.mjs"; |
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,42 @@ | ||
import React, { Component } from "react"; | ||
|
||
const h = React.createElement; | ||
|
||
/** | ||
* @typedef State | ||
* @prop {object} [error] | ||
*/ | ||
|
||
/** | ||
* @extends Component<any, State> | ||
*/ | ||
class TestErrorBoundary extends Component { | ||
/** @type {State} */ | ||
state = { | ||
error: undefined, | ||
}; | ||
|
||
/** | ||
* @param {any} props | ||
*/ | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
/** | ||
* @param {object} [error] | ||
*/ | ||
componentDidCatch(error) { | ||
this.setState({ | ||
error, | ||
}); | ||
} | ||
|
||
render() { | ||
const error = this.state.error; | ||
return error ? h("div", null, `${error}`) : this.props.children; | ||
} | ||
} | ||
TestErrorBoundary.displayName = "TestErrorBoundary"; | ||
|
||
export default TestErrorBoundary; |
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,52 @@ | ||
import React from "react"; | ||
import TestRenderer from "react-test-renderer"; | ||
import assert from "node:assert/strict"; | ||
import mockFunction from "mock-fn"; | ||
import { assertComponents, TestErrorBoundary } from "../index.mjs"; | ||
|
||
const h = React.createElement; | ||
|
||
const { describe, it } = await (async () => { | ||
// @ts-ignore | ||
return process.isBun // @ts-ignore | ||
? Promise.resolve({ describe: (_, fn) => fn(), it: test }) | ||
: import("node:test"); | ||
})(); | ||
|
||
describe("TestErrorBoundary.test.mjs", () => { | ||
it("should render children if no errors", () => { | ||
//when | ||
const result = TestRenderer.create( | ||
h(TestErrorBoundary, null, "some child") | ||
).root; | ||
|
||
//then | ||
assertComponents(result.children, "some child"); | ||
}); | ||
|
||
it("should render error details if error during render", () => { | ||
//given | ||
// suppress intended error | ||
// see: https://github.com/facebook/react/issues/11098#issuecomment-412682721 | ||
const savedConsoleError = console.error; | ||
const consoleErrorMock = mockFunction(() => { | ||
console.error = savedConsoleError; | ||
}); | ||
console.error = consoleErrorMock; | ||
|
||
const ErrorComp = () => { | ||
throw Error("test error"); | ||
return h(React.Fragment); | ||
}; | ||
|
||
//when | ||
const result = TestRenderer.create( | ||
h(TestErrorBoundary, null, h(ErrorComp)) | ||
).root; | ||
|
||
//then | ||
assert.deepEqual(consoleErrorMock.times, 1); | ||
assert.deepEqual(console.error, savedConsoleError); | ||
assertComponents(result.children, h("div", null, "Error: test error")); | ||
}); | ||
}); |
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