2022-02-09 16:21:40 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2022-10-17 16:42:04 +00:00
|
|
|
import { render, screen, fireEvent } from "@testing-library/react";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2022-12-27 07:39:26 +00:00
|
|
|
import UnwrappedSpacePanel from "../../../../src/components/views/spaces/SpacePanel";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import { SpaceKey } from "../../../../src/stores/spaces";
|
|
|
|
import { shouldShowComponent } from "../../../../src/customisations/helpers/UIComponents";
|
|
|
|
import { UIComponent } from "../../../../src/settings/UIFeature";
|
2022-12-27 07:39:26 +00:00
|
|
|
import { wrapInSdkContext } from "../../../test-utils";
|
|
|
|
import { SdkContextClass } from "../../../../src/contexts/SDKContext";
|
2022-02-09 16:21:40 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/stores/spaces/SpaceStore", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
const EventEmitter = require("events");
|
|
|
|
class MockSpaceStore extends EventEmitter {
|
|
|
|
invitedSpaces = [];
|
|
|
|
enabledMetaSpaces = [];
|
|
|
|
spacePanelSpaces = [];
|
2022-12-12 11:24:14 +00:00
|
|
|
activeSpace: SpaceKey = "!space1";
|
2022-02-09 16:21:40 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
instance: new MockSpaceStore(),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/customisations/helpers/UIComponents", () => ({
|
2022-02-09 16:21:40 +00:00
|
|
|
shouldShowComponent: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<SpacePanel />", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
const mockClient = {
|
2022-12-12 11:24:14 +00:00
|
|
|
getUserId: jest.fn().mockReturnValue("@test:test"),
|
2022-02-09 16:21:40 +00:00
|
|
|
isGuest: jest.fn(),
|
|
|
|
getAccountData: jest.fn(),
|
|
|
|
} as unknown as MatrixClient;
|
2022-12-27 07:39:26 +00:00
|
|
|
const SpacePanel = wrapInSdkContext(UnwrappedSpacePanel, SdkContextClass.instance);
|
2022-02-09 16:21:40 +00:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(shouldShowComponent).mockClear().mockReturnValue(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("create new space button", () => {
|
|
|
|
it("renders create space button when UIComponent.CreateSpaces component should be shown", () => {
|
2022-10-17 16:42:04 +00:00
|
|
|
render(<SpacePanel />);
|
|
|
|
screen.getByTestId("create-space-button");
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render create space button when UIComponent.CreateSpaces component should not be shown", () => {
|
2022-02-09 16:21:40 +00:00
|
|
|
mocked(shouldShowComponent).mockReturnValue(false);
|
2022-10-17 16:42:04 +00:00
|
|
|
render(<SpacePanel />);
|
2022-02-09 16:21:40 +00:00
|
|
|
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.CreateSpaces);
|
2022-10-17 16:42:04 +00:00
|
|
|
expect(screen.queryByTestId("create-space-button")).toBeFalsy();
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("opens context menu on create space button click", () => {
|
2022-10-17 16:42:04 +00:00
|
|
|
render(<SpacePanel />);
|
|
|
|
fireEvent.click(screen.getByTestId("create-space-button"));
|
|
|
|
screen.getByTestId("create-space-button");
|
2022-02-09 16:21:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|