|
| 1 | +/* |
| 2 | + * Copyright (c) [2022-2026] SUSE LLC |
| 3 | + * |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License as published by the Free |
| 8 | + * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | + * any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | + * more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License along |
| 17 | + * with this program; if not, contact SUSE LLC. |
| 18 | + * |
| 19 | + * To contact SUSE LLC about this file by physical or electronic mail, you may |
| 20 | + * find current contact information at www.suse.com. |
| 21 | + */ |
| 22 | + |
| 23 | +import React from "react"; |
| 24 | +import { screen } from "@testing-library/react"; |
| 25 | +import { installerRender, mockRouteError } from "~/test-utils"; |
| 26 | +import ErrorPage from "./ErrorPage"; |
| 27 | + |
| 28 | +jest.mock("stacktracey", () => jest.fn()); |
| 29 | +const mockStackTracey = jest.requireMock("stacktracey"); |
| 30 | + |
| 31 | +const routeError = (status: number, statusText: string, data: unknown) => ({ |
| 32 | + __isRouteError: true, |
| 33 | + status, |
| 34 | + statusText, |
| 35 | + data, |
| 36 | +}); |
| 37 | + |
| 38 | +describe("ErrorPage", () => { |
| 39 | + beforeEach(() => { |
| 40 | + mockStackTracey.mockImplementation(() => ({ |
| 41 | + withSourcesAsync: jest.fn().mockResolvedValue({ |
| 42 | + filter: jest.fn().mockReturnThis(), |
| 43 | + asTable: jest.fn().mockReturnValue("app.ts:10 myFunc\napp.ts:20 caller"), |
| 44 | + }), |
| 45 | + filter: jest.fn().mockReturnThis(), |
| 46 | + asTable: jest.fn().mockReturnValue("app.ts:10 myFunc\napp.ts:20 caller"), |
| 47 | + })); |
| 48 | + }); |
| 49 | + describe("when the error is a route error response", () => { |
| 50 | + describe("when it is a 404", () => { |
| 51 | + beforeEach(() => { |
| 52 | + mockRouteError(routeError(404, "Not Found", null)); |
| 53 | + }); |
| 54 | + |
| 55 | + it("shows the HTTP status and statusText", () => { |
| 56 | + installerRender(<ErrorPage />); |
| 57 | + screen.getByText("404 Not Found"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("does not show a skeleton", () => { |
| 61 | + installerRender(<ErrorPage />); |
| 62 | + expect(screen.queryByText("Retrieving error details")).not.toBeInTheDocument(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("when the data payload is a string", () => { |
| 67 | + beforeEach(() => { |
| 68 | + mockRouteError(routeError(403, "Forbidden", "You do not have access")); |
| 69 | + }); |
| 70 | + |
| 71 | + it("shows the data payload as-is", () => { |
| 72 | + installerRender(<ErrorPage />); |
| 73 | + screen.getByText("You do not have access"); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("when the data payload is not a string", () => { |
| 78 | + beforeEach(() => { |
| 79 | + mockRouteError( |
| 80 | + routeError(422, "Unprocessable Entity", { field: "email", issue: "invalid" }), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("shows the JSON-serialised payload", () => { |
| 85 | + installerRender(<ErrorPage />); |
| 86 | + screen.getByText(/"field":"email"/); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("when the error is an unexpected error", () => { |
| 92 | + describe("when it is a standard Error", () => { |
| 93 | + beforeEach(() => { |
| 94 | + mockRouteError(new Error("Something exploded")); |
| 95 | + }); |
| 96 | + |
| 97 | + it("shows the 'Unexpected error' heading", async () => { |
| 98 | + installerRender(<ErrorPage />); |
| 99 | + screen.getByText("Unexpected error"); |
| 100 | + await screen.findByText(/app\.ts:10.*myFunc/); |
| 101 | + }); |
| 102 | + |
| 103 | + it("shows the error message", async () => { |
| 104 | + installerRender(<ErrorPage />); |
| 105 | + screen.getByText("Something exploded"); |
| 106 | + await screen.findByText(/app\.ts:10.*myFunc/); |
| 107 | + }); |
| 108 | + |
| 109 | + it("shows a skeleton while the trace is loading", async () => { |
| 110 | + installerRender(<ErrorPage />); |
| 111 | + screen.getByText("Retrieving error details"); |
| 112 | + await screen.findByText(/app\.ts:10.*myFunc/); |
| 113 | + }); |
| 114 | + |
| 115 | + it("shows the stack trace once loaded", async () => { |
| 116 | + installerRender(<ErrorPage />); |
| 117 | + await screen.findByText(/app\.ts:10.*myFunc/); |
| 118 | + }); |
| 119 | + |
| 120 | + it("hides the skeleton once the trace is loaded", async () => { |
| 121 | + installerRender(<ErrorPage />); |
| 122 | + await screen.findByText(/app\.ts:10.*myFunc/); |
| 123 | + expect(screen.queryByText("Retrieving error details")).not.toBeInTheDocument(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("when withSourcesAsync fails", () => { |
| 128 | + beforeEach(() => { |
| 129 | + mockStackTracey.mockImplementationOnce(() => ({ |
| 130 | + withSourcesAsync: jest.fn().mockRejectedValue(new Error("network error")), |
| 131 | + filter: jest.fn().mockReturnThis(), |
| 132 | + asTable: jest.fn().mockReturnValue("app.ts:10 myFunc (no sources)"), |
| 133 | + })); |
| 134 | + mockRouteError(new Error("Something exploded")); |
| 135 | + }); |
| 136 | + |
| 137 | + it("falls back to the raw stack table", async () => { |
| 138 | + installerRender(<ErrorPage />); |
| 139 | + await screen.findByText(/app\.ts:10.*myFunc \(no sources\)/); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe("when the thrown value is not an Error instance", () => { |
| 144 | + beforeEach(() => { |
| 145 | + mockRouteError({ code: 42, reason: "unknown" }); |
| 146 | + }); |
| 147 | + |
| 148 | + it("shows the 'Something went wrong' heading", async () => { |
| 149 | + installerRender(<ErrorPage />); |
| 150 | + screen.getByText("Something went wrong"); |
| 151 | + await screen.findByText(/"code":42/); |
| 152 | + }); |
| 153 | + |
| 154 | + it("shows 'Unknown error' as the message", async () => { |
| 155 | + installerRender(<ErrorPage />); |
| 156 | + screen.getByText("Unknown error"); |
| 157 | + await screen.findByText(/"code":42/); |
| 158 | + }); |
| 159 | + |
| 160 | + it("shows the JSON-serialised value", async () => { |
| 161 | + installerRender(<ErrorPage />); |
| 162 | + await screen.findByText(/"code":42/); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments