|
| 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 { mount } from "enzyme"; |
| 19 | + |
| 20 | +import { stubClient, mkRoom } from "../../../test-utils"; |
| 21 | +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; |
| 22 | +import DMRoomMap from "../../../../src/utils/DMRoomMap"; |
| 23 | +import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; |
| 24 | +import { Action } from "../../../../src/dispatcher/actions"; |
| 25 | +import { SpaceButton } from "../../../../src/components/views/spaces/SpaceTreeLevel"; |
| 26 | +import { MetaSpace, SpaceKey } from "../../../../src/stores/spaces"; |
| 27 | +import SpaceStore from "../../../../src/stores/spaces/SpaceStore"; |
| 28 | + |
| 29 | +jest.mock("../../../../src/stores/spaces/SpaceStore", () => { |
| 30 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 31 | + const EventEmitter = require("events"); |
| 32 | + class MockSpaceStore extends EventEmitter { |
| 33 | + activeSpace: SpaceKey = "!space1"; |
| 34 | + setActiveSpace = jest.fn(); |
| 35 | + } |
| 36 | + |
| 37 | + return { instance: new MockSpaceStore() }; |
| 38 | +}); |
| 39 | + |
| 40 | +describe("SpaceButton", () => { |
| 41 | + stubClient(); |
| 42 | + const space = mkRoom(MatrixClientPeg.get(), "!1:example.org"); |
| 43 | + DMRoomMap.makeShared(); |
| 44 | + |
| 45 | + const dispatchSpy = jest.spyOn(defaultDispatcher, "dispatch"); |
| 46 | + |
| 47 | + afterEach(jest.clearAllMocks); |
| 48 | + |
| 49 | + describe("real space", () => { |
| 50 | + it("activates the space on click", () => { |
| 51 | + const button = mount(<SpaceButton space={space} selected={false} label="My space" />); |
| 52 | + |
| 53 | + expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled(); |
| 54 | + button.simulate("click"); |
| 55 | + expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith("!1:example.org"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("navigates to the space home on click if already active", () => { |
| 59 | + const button = mount(<SpaceButton space={space} selected={true} label="My space" />); |
| 60 | + |
| 61 | + expect(dispatchSpy).not.toHaveBeenCalled(); |
| 62 | + button.simulate("click"); |
| 63 | + expect(dispatchSpy).toHaveBeenCalledWith({ action: Action.ViewRoom, room_id: "!1:example.org" }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("metaspace", () => { |
| 68 | + it("activates the metaspace on click", () => { |
| 69 | + const button = mount(<SpaceButton spaceKey={MetaSpace.People} selected={false} label="People" />); |
| 70 | + |
| 71 | + expect(SpaceStore.instance.setActiveSpace).not.toHaveBeenCalled(); |
| 72 | + button.simulate("click"); |
| 73 | + expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People); |
| 74 | + }); |
| 75 | + |
| 76 | + it("does nothing on click if already active", () => { |
| 77 | + const button = mount(<SpaceButton spaceKey={MetaSpace.People} selected={true} label="People" />); |
| 78 | + |
| 79 | + button.simulate("click"); |
| 80 | + expect(dispatchSpy).not.toHaveBeenCalled(); |
| 81 | + // Re-activating the metaspace is a no-op |
| 82 | + expect(SpaceStore.instance.setActiveSpace).toHaveBeenCalledWith(MetaSpace.People); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments