From 6974ee630e6ae8f908e0ab2b3a594a3ac66b164a Mon Sep 17 00:00:00 2001 From: Nathalie Schulz Date: Fri, 10 Nov 2023 10:18:30 +0100 Subject: [PATCH 1/2] modify test to show the history back problem --- .../test/AppInsightsErrorBoundary.test.tsx | 68 +++++++------------ 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx b/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx index e55a225..f81167c 100644 --- a/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx +++ b/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx @@ -94,20 +94,14 @@ describe("", () => { }); function NewError() { - const navigate = useNavigate(); - const ErrorDisplay = () =>
You are on the error page
; function handleClick() { - navigate(-1); + history.back() } return (
- - - -
+
You are on the error page
+ ); } @@ -116,48 +110,38 @@ describe("", () => { if (orgError) { console.error = msg => { /* Do Nothing */ }; } - const Home = () =>
Home Page
- const About = () =>
About Page
+ const Home = () =>
Home Page
; + const About = () => { + throw new Error("something went wrong"); + }; try { render( - + +
- Home - About - Error - - - } /> - } /> - } /> - - -
-
+ Home + About + + + } /> + } /> + + + +
+
); expect(screen.getByText(/Home Page/i)).toBeInTheDocument() - - // go to error page - await userEvent.click(screen.getByText(/error/i)) - expect(screen.getByText(/You are on the error page/i)).toBeInTheDocument() - expect(trackExceptionSpy).toHaveBeenCalledTimes(1); - // go back to home page - await userEvent.click(screen.getByText(/go back/i)); - expect(screen.getByText(/Home Page/i)).toBeInTheDocument(); - expect(trackExceptionSpy).toHaveBeenCalledTimes(1); - - // go to error page again - await userEvent.click(screen.getByText(/error/i)) - expect(screen.getByText(/You are on the error page/i)).toBeInTheDocument() - console.log("track time", trackExceptionSpy.mock.calls.length); - - // navigate to about page - await userEvent.click(screen.getByText(/about/i)) - expect(screen.getByText(/About Page/i)).toBeInTheDocument() + // navigate to about page (throws error, so show error component) + await userEvent.click(screen.getByText(/about/i)); + expect(screen.getByText(/You are on the error page/i)).toBeInTheDocument(); console.log("track time", trackExceptionSpy.mock.calls.length); + // go back to home page (no error, so show home component) + await userEvent.click(screen.getByText(/go back/i)); + expect(screen.getByText(/Home Page/i)).toBeInTheDocument(); } finally { if (orgError) { console.error = orgError; From 5b44a59e2070bf55ef427094c01473746d4673bd Mon Sep 17 00:00:00 2001 From: Nathalie Schulz Date: Mon, 13 Nov 2023 12:12:40 +0100 Subject: [PATCH 2/2] replace history.back with navigate --- .../test/AppInsightsErrorBoundary.test.tsx | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx b/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx index f81167c..562ff86 100644 --- a/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx +++ b/applicationinsights-react-js/test/AppInsightsErrorBoundary.test.tsx @@ -94,9 +94,12 @@ describe("", () => { }); function NewError() { + const navigate = useNavigate(); + function handleClick() { - history.back() + navigate(-1); } + return (
@@ -105,6 +108,14 @@ describe("", () => { ); } + function BackButton() { + const navigate = useNavigate(); + + return ( + + ); + } + test("error state would not show again after user use back button", async () => { const orgError = console && console.error; if (orgError) { @@ -117,20 +128,20 @@ describe("", () => { try { render( - - + +
Home About - + } /> } />
-
-
+
+ ); expect(screen.getByText(/Home Page/i)).toBeInTheDocument()