|
| 1 | +/* |
| 2 | +Copyright 2023 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, { ComponentProps } from "react"; |
| 18 | +import { render, RenderResult, screen } from "@testing-library/react"; |
| 19 | +import fetchMockJest from "fetch-mock-jest"; |
| 20 | +import { ClientEvent } from "matrix-js-sdk/src/client"; |
| 21 | +import { SyncState } from "matrix-js-sdk/src/sync"; |
| 22 | +import { MediaHandler } from "matrix-js-sdk/src/webrtc/mediaHandler"; |
| 23 | + |
| 24 | +import MatrixChat from "../../../src/components/structures/MatrixChat"; |
| 25 | +import * as StorageManager from "../../../src/utils/StorageManager"; |
| 26 | +import defaultDispatcher from "../../../src/dispatcher/dispatcher"; |
| 27 | +import { Action } from "../../../src/dispatcher/actions"; |
| 28 | +import { UserTab } from "../../../src/components/views/dialogs/UserTab"; |
| 29 | +import { flushPromises, getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils"; |
| 30 | + |
| 31 | +describe("<MatrixChat />", () => { |
| 32 | + const userId = "@alice:server.org"; |
| 33 | + const deviceId = "qwertyui"; |
| 34 | + const accessToken = "abc123"; |
| 35 | + const mockClient = getMockClientWithEventEmitter({ |
| 36 | + ...mockClientMethodsUser(userId), |
| 37 | + startClient: jest.fn(), |
| 38 | + stopClient: jest.fn(), |
| 39 | + setCanResetTimelineCallback: jest.fn(), |
| 40 | + isInitialSyncComplete: jest.fn(), |
| 41 | + getSyncState: jest.fn(), |
| 42 | + getSyncStateData: jest.fn().mockReturnValue(null), |
| 43 | + getThirdpartyProtocols: jest.fn().mockResolvedValue({}), |
| 44 | + getClientWellKnown: jest.fn().mockReturnValue({}), |
| 45 | + isVersionSupported: jest.fn().mockResolvedValue(false), |
| 46 | + isCryptoEnabled: jest.fn().mockReturnValue(false), |
| 47 | + getRoom: jest.fn(), |
| 48 | + getMediaHandler: jest.fn().mockReturnValue({ |
| 49 | + setVideoInput: jest.fn(), |
| 50 | + setAudioInput: jest.fn(), |
| 51 | + setAudioSettings: jest.fn(), |
| 52 | + stopAllStreams: jest.fn(), |
| 53 | + } as unknown as MediaHandler), |
| 54 | + setAccountData: jest.fn(), |
| 55 | + store: { |
| 56 | + destroy: jest.fn(), |
| 57 | + }, |
| 58 | + }); |
| 59 | + const serverConfig = { |
| 60 | + hsUrl: "https://test.com", |
| 61 | + hsName: "Test Server", |
| 62 | + hsNameIsDifferent: false, |
| 63 | + isUrl: "https://is.com", |
| 64 | + isDefault: true, |
| 65 | + isNameResolvable: true, |
| 66 | + warning: "", |
| 67 | + }; |
| 68 | + const defaultProps: ComponentProps<typeof MatrixChat> = { |
| 69 | + config: { |
| 70 | + brand: "Test", |
| 71 | + element_call: {}, |
| 72 | + feedback: { |
| 73 | + existing_issues_url: "https://feedback.org/existing", |
| 74 | + new_issue_url: "https://feedback.org/new", |
| 75 | + }, |
| 76 | + validated_server_config: serverConfig, |
| 77 | + }, |
| 78 | + onNewScreen: jest.fn(), |
| 79 | + onTokenLoginCompleted: jest.fn(), |
| 80 | + makeRegistrationUrl: jest.fn(), |
| 81 | + realQueryParams: {}, |
| 82 | + }; |
| 83 | + const getComponent = (props: Partial<ComponentProps<typeof MatrixChat>> = {}) => |
| 84 | + render(<MatrixChat {...defaultProps} {...props} />); |
| 85 | + const localStorageSpy = jest.spyOn(localStorage.__proto__, "getItem").mockReturnValue(undefined); |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + fetchMockJest.get("https://test.com/_matrix/client/versions", { |
| 89 | + unstable_features: {}, |
| 90 | + versions: [], |
| 91 | + }); |
| 92 | + localStorageSpy.mockClear(); |
| 93 | + jest.spyOn(StorageManager, "idbLoad").mockRestore(); |
| 94 | + jest.spyOn(StorageManager, "idbSave").mockResolvedValue(undefined); |
| 95 | + jest.spyOn(defaultDispatcher, "dispatch").mockClear(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should render spinner while app is loading", () => { |
| 99 | + const { container } = getComponent(); |
| 100 | + |
| 101 | + expect(container).toMatchSnapshot(); |
| 102 | + }); |
| 103 | + |
| 104 | + describe("with an existing session", () => { |
| 105 | + const mockidb: Record<string, Record<string, string>> = { |
| 106 | + acccount: { |
| 107 | + mx_access_token: accessToken, |
| 108 | + }, |
| 109 | + }; |
| 110 | + const mockLocalStorage: Record<string, string> = { |
| 111 | + mx_hs_url: serverConfig.hsUrl, |
| 112 | + mx_is_url: serverConfig.isUrl, |
| 113 | + mx_access_token: accessToken, |
| 114 | + mx_user_id: userId, |
| 115 | + mx_device_id: deviceId, |
| 116 | + }; |
| 117 | + |
| 118 | + beforeEach(() => { |
| 119 | + localStorageSpy.mockImplementation((key: unknown) => mockLocalStorage[key as string] || ""); |
| 120 | + |
| 121 | + jest.spyOn(StorageManager, "idbLoad").mockImplementation(async (table, key) => { |
| 122 | + const safeKey = Array.isArray(key) ? key[0] : key; |
| 123 | + return mockidb[table]?.[safeKey]; |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + const getComponentAndWaitForReady = async (): Promise<RenderResult> => { |
| 128 | + const renderResult = getComponent(); |
| 129 | + // we think we are logged in, but are still waiting for the /sync to complete |
| 130 | + await screen.findByText("Logout"); |
| 131 | + // initial sync |
| 132 | + mockClient.emit(ClientEvent.Sync, SyncState.Prepared, null); |
| 133 | + // wait for logged in view to load |
| 134 | + await screen.findByLabelText("User menu"); |
| 135 | + // let things settle |
| 136 | + await flushPromises(); |
| 137 | + // and some more for good measure |
| 138 | + // this proved to be a little flaky |
| 139 | + await flushPromises(); |
| 140 | + |
| 141 | + return renderResult; |
| 142 | + }; |
| 143 | + |
| 144 | + it("should render welcome page after login", async () => { |
| 145 | + getComponent(); |
| 146 | + |
| 147 | + // we think we are logged in, but are still waiting for the /sync to complete |
| 148 | + const logoutButton = await screen.findByText("Logout"); |
| 149 | + |
| 150 | + expect(logoutButton).toBeInTheDocument(); |
| 151 | + expect(screen.getByRole("progressbar")).toBeInTheDocument(); |
| 152 | + |
| 153 | + // initial sync |
| 154 | + mockClient.emit(ClientEvent.Sync, SyncState.Prepared, null); |
| 155 | + |
| 156 | + // wait for logged in view to load |
| 157 | + await screen.findByLabelText("User menu"); |
| 158 | + // let things settle |
| 159 | + await flushPromises(); |
| 160 | + expect(screen.queryByRole("progressbar")).not.toBeInTheDocument(); |
| 161 | + expect(screen.getByText(`Welcome ${userId}`)).toBeInTheDocument(); |
| 162 | + }); |
| 163 | + |
| 164 | + describe("onAction()", () => { |
| 165 | + it("should open user device settings", async () => { |
| 166 | + await getComponentAndWaitForReady(); |
| 167 | + |
| 168 | + defaultDispatcher.dispatch({ |
| 169 | + action: Action.ViewUserDeviceSettings, |
| 170 | + }); |
| 171 | + |
| 172 | + await flushPromises(); |
| 173 | + |
| 174 | + expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ |
| 175 | + action: Action.ViewUserSettings, |
| 176 | + initialTabId: UserTab.Security, |
| 177 | + }); |
| 178 | + }); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments