|
| 1 | +/* |
| 2 | +Copyright 2024 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 { fireEvent, render, screen, within } from "@testing-library/react"; |
| 19 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 20 | + |
| 21 | +import MatrixClientContext from "../../../../src/contexts/MatrixClientContext"; |
| 22 | +import { SDKContext, SdkContextClass } from "../../../../src/contexts/SDKContext"; |
| 23 | +import SettingsStore from "../../../../src/settings/SettingsStore"; |
| 24 | +import { UIFeature } from "../../../../src/settings/UIFeature"; |
| 25 | +import { |
| 26 | + getMockClientWithEventEmitter, |
| 27 | + mockClientMethodsServer, |
| 28 | + mockClientMethodsUser, |
| 29 | + flushPromises, |
| 30 | +} from "../../../test-utils"; |
| 31 | +import SetIntegrationManager from "../../../../src/components/views/settings/SetIntegrationManager"; |
| 32 | +import { SettingLevel } from "../../../../src/settings/SettingLevel"; |
| 33 | + |
| 34 | +describe("SetIntegrationManager", () => { |
| 35 | + const userId = "@alice:server.org"; |
| 36 | + |
| 37 | + const mockClient = getMockClientWithEventEmitter({ |
| 38 | + ...mockClientMethodsUser(userId), |
| 39 | + ...mockClientMethodsServer(), |
| 40 | + getCapabilities: jest.fn(), |
| 41 | + getThreePids: jest.fn(), |
| 42 | + getIdentityServerUrl: jest.fn(), |
| 43 | + deleteThreePid: jest.fn(), |
| 44 | + }); |
| 45 | + |
| 46 | + let stores: SdkContextClass; |
| 47 | + |
| 48 | + const getComponent = () => ( |
| 49 | + <MatrixClientContext.Provider value={mockClient}> |
| 50 | + <SDKContext.Provider value={stores}> |
| 51 | + <SetIntegrationManager /> |
| 52 | + </SDKContext.Provider> |
| 53 | + </MatrixClientContext.Provider> |
| 54 | + ); |
| 55 | + |
| 56 | + it("should not render manage integrations section when widgets feature is disabled", () => { |
| 57 | + jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => settingName !== UIFeature.Widgets); |
| 58 | + render(getComponent()); |
| 59 | + |
| 60 | + expect(screen.queryByTestId("mx_SetIntegrationManager")).not.toBeInTheDocument(); |
| 61 | + expect(SettingsStore.getValue).toHaveBeenCalledWith(UIFeature.Widgets); |
| 62 | + }); |
| 63 | + it("should render manage integrations sections", () => { |
| 64 | + jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => settingName === UIFeature.Widgets); |
| 65 | + |
| 66 | + render(getComponent()); |
| 67 | + |
| 68 | + expect(screen.getByTestId("mx_SetIntegrationManager")).toMatchSnapshot(); |
| 69 | + }); |
| 70 | + it("should update integrations provisioning on toggle", () => { |
| 71 | + jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => settingName === UIFeature.Widgets); |
| 72 | + jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined); |
| 73 | + |
| 74 | + render(getComponent()); |
| 75 | + |
| 76 | + const integrationSection = screen.getByTestId("mx_SetIntegrationManager"); |
| 77 | + fireEvent.click(within(integrationSection).getByRole("switch")); |
| 78 | + |
| 79 | + expect(SettingsStore.setValue).toHaveBeenCalledWith( |
| 80 | + "integrationProvisioning", |
| 81 | + null, |
| 82 | + SettingLevel.ACCOUNT, |
| 83 | + true, |
| 84 | + ); |
| 85 | + expect(within(integrationSection).getByRole("switch")).toBeChecked(); |
| 86 | + }); |
| 87 | + it("handles error when updating setting fails", async () => { |
| 88 | + jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => settingName === UIFeature.Widgets); |
| 89 | + jest.spyOn(logger, "error").mockImplementation(() => {}); |
| 90 | + |
| 91 | + jest.spyOn(SettingsStore, "setValue").mockRejectedValue("oups"); |
| 92 | + |
| 93 | + render(getComponent()); |
| 94 | + |
| 95 | + const integrationSection = screen.getByTestId("mx_SetIntegrationManager"); |
| 96 | + fireEvent.click(within(integrationSection).getByRole("switch")); |
| 97 | + |
| 98 | + await flushPromises(); |
| 99 | + |
| 100 | + expect(logger.error).toHaveBeenCalledWith("Error changing integration manager provisioning"); |
| 101 | + expect(logger.error).toHaveBeenCalledWith("oups"); |
| 102 | + expect(within(integrationSection).getByRole("switch")).not.toBeChecked(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments