|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from "react"; |
| 18 | +import { act, render, RenderResult } from "@testing-library/react"; |
| 19 | + |
| 20 | +import { filterConsole, stubClient } from "../../../test-utils"; |
| 21 | +import { UserOnboardingPage } from "../../../../src/components/views/user-onboarding/UserOnboardingPage"; |
| 22 | +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; |
| 23 | +import SdkConfig from "../../../../src/SdkConfig"; |
| 24 | + |
| 25 | +jest.mock("../../../../src/components/structures/EmbeddedPage", () => ({ |
| 26 | + __esModule: true, |
| 27 | + default: jest.fn().mockImplementation(({ url }) => <div>{url}</div>), |
| 28 | +})); |
| 29 | + |
| 30 | +jest.mock("../../../../src/components/structures/HomePage", () => ({ |
| 31 | + __esModule: true, |
| 32 | + default: jest.fn().mockImplementation(() => <div>home page</div>), |
| 33 | +})); |
| 34 | + |
| 35 | +describe("UserOnboardingPage", () => { |
| 36 | + let restoreConsole: () => void; |
| 37 | + |
| 38 | + const renderComponent = async (): Promise<RenderResult> => { |
| 39 | + const renderResult = render(<UserOnboardingPage />); |
| 40 | + await act(async () => { |
| 41 | + jest.runAllTimers(); |
| 42 | + }); |
| 43 | + return renderResult; |
| 44 | + }; |
| 45 | + |
| 46 | + beforeAll(() => { |
| 47 | + restoreConsole = filterConsole( |
| 48 | + // unrelated for this test |
| 49 | + "could not update user onboarding context", |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + stubClient(); |
| 55 | + jest.useFakeTimers(); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + jest.useRealTimers(); |
| 60 | + }); |
| 61 | + |
| 62 | + afterAll(() => { |
| 63 | + restoreConsole(); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("when the user registered before the cutoff date", () => { |
| 67 | + beforeEach(() => { |
| 68 | + jest.spyOn(MatrixClientPeg, "userRegisteredAfter").mockReturnValue(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should render the home page", async () => { |
| 72 | + expect((await renderComponent()).queryByText("home page")).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("when the user registered after the cutoff date", () => { |
| 77 | + beforeEach(() => { |
| 78 | + jest.spyOn(MatrixClientPeg, "userRegisteredAfter").mockReturnValue(true); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("and there is an explicit home page configured", () => { |
| 82 | + beforeEach(() => { |
| 83 | + jest.spyOn(SdkConfig, "get").mockReturnValue({ |
| 84 | + embedded_pages: { |
| 85 | + home_url: "https://example.com/home", |
| 86 | + }, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should render the configured page", async () => { |
| 91 | + expect((await renderComponent()).queryByText("https://example.com/home")).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe("and there is no home page configured", () => { |
| 96 | + it("should render the onboarding", async () => { |
| 97 | + expect((await renderComponent()).queryByTestId("user-onboarding-list")).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments