From 771d7e95e75b7e887eb3e5cdf21a8dfb4401497e Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 3 Aug 2023 09:56:53 -0600 Subject: [PATCH] Support adding space-restricted joins on rooms not members of those spaces (#9017) * Support adding space-restricted joins on rooms not members of those spaces * add react dependencies * Add snapshot test * Fix snapshot stability * Update snapshot * Increase coverage --------- Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- .../ManageRestrictedJoinRuleDialog.tsx | 31 ++- src/i18n/strings/en_EN.json | 1 + .../ManageRestrictedJoinRuleDialog-test.tsx | 58 ++++ ...nageRestrictedJoinRuleDialog-test.tsx.snap | 263 ++++++++++++++++++ 4 files changed, 349 insertions(+), 4 deletions(-) create mode 100644 test/components/views/dialogs/ManageRestrictedJoinRuleDialog-test.tsx create mode 100644 test/components/views/dialogs/__snapshots__/ManageRestrictedJoinRuleDialog-test.tsx.snap diff --git a/src/components/views/dialogs/ManageRestrictedJoinRuleDialog.tsx b/src/components/views/dialogs/ManageRestrictedJoinRuleDialog.tsx index a4cefd7fbab..4bdea4a6a08 100644 --- a/src/components/views/dialogs/ManageRestrictedJoinRuleDialog.tsx +++ b/src/components/views/dialogs/ManageRestrictedJoinRuleDialog.tsx @@ -93,11 +93,13 @@ const ManageRestrictedJoinRuleDialog: React.FC = ({ room, selected = [], const [query, setQuery] = useState(""); const lcQuery = query.toLowerCase().trim(); - const [spacesContainingRoom, otherEntries] = useMemo(() => { + const [spacesContainingRoom, otherJoinedSpaces, otherEntries] = useMemo(() => { const parents = new Set(); addAllParents(parents, room); + return [ Array.from(parents), + SpaceStore.instance.spacePanelSpaces.filter((s) => !parents.has(s)), filterBoolean( selected.map((roomId) => { const room = cli.getRoom(roomId); @@ -112,12 +114,13 @@ const ManageRestrictedJoinRuleDialog: React.FC = ({ room, selected = [], ]; }, [cli, selected, room]); - const [filteredSpacesContainingRoom, filteredOtherEntries] = useMemo( + const [filteredSpacesContainingRoom, filteredOtherJoinedSpaces, filteredOtherEntries] = useMemo( () => [ spacesContainingRoom.filter((r) => r.name.toLowerCase().includes(lcQuery)), + otherJoinedSpaces.filter((r) => r.name.toLowerCase().includes(lcQuery)), otherEntries.filter((r) => r.name.toLowerCase().includes(lcQuery)), ], - [spacesContainingRoom, otherEntries, lcQuery], + [spacesContainingRoom, otherJoinedSpaces, otherEntries, lcQuery], ); const onChange = (checked: boolean, room: Room): void => { @@ -138,6 +141,8 @@ const ManageRestrictedJoinRuleDialog: React.FC = ({ room, selected = [], ); } + const totalResults = + filteredSpacesContainingRoom.length + filteredOtherJoinedSpaces.length + filteredOtherEntries.length; return ( = ({ room, selected = [], ) : null} - {filteredSpacesContainingRoom.length + filteredOtherEntries.length < 1 ? ( + {filteredOtherJoinedSpaces.length > 0 ? ( +
+

{_t("Other spaces you know")}

+ {filteredOtherJoinedSpaces.map((space) => { + return ( + { + onChange(checked, space); + }} + /> + ); + })} +
+ ) : null} + + {totalResults < 1 ? ( {_t("No results")} ) : undefined} diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 06caa0c7236..cc822c90913 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -2970,6 +2970,7 @@ "Spaces you know that contain this room": "Spaces you know that contain this room", "Other spaces or rooms you might not know": "Other spaces or rooms you might not know", "These are likely ones other room admins are a part of.": "These are likely ones other room admins are a part of.", + "Other spaces you know": "Other spaces you know", "Confirm by comparing the following with the User Settings in your other session:": "Confirm by comparing the following with the User Settings in your other session:", "Confirm this user's session by comparing the following with their User Settings:": "Confirm this user's session by comparing the following with their User Settings:", "Session name": "Session name", diff --git a/test/components/views/dialogs/ManageRestrictedJoinRuleDialog-test.tsx b/test/components/views/dialogs/ManageRestrictedJoinRuleDialog-test.tsx new file mode 100644 index 00000000000..01ae855ac61 --- /dev/null +++ b/test/components/views/dialogs/ManageRestrictedJoinRuleDialog-test.tsx @@ -0,0 +1,58 @@ +/* +Copyright 2023 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. +*/ + +import React from "react"; +import { render } from "@testing-library/react"; +import { Room } from "matrix-js-sdk/src/matrix"; + +import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils"; +import ManageRestrictedJoinRuleDialog from "../../../../src/components/views/dialogs/ManageRestrictedJoinRuleDialog"; +import SpaceStore from "../../../../src/stores/spaces/SpaceStore"; +import DMRoomMap from "../../../../src/utils/DMRoomMap"; + +// Fake random strings to give a predictable snapshot +jest.mock("matrix-js-sdk/src/randomstring", () => { + return { + randomString: () => "abdefghi", + }; +}); + +describe("", () => { + const userId = "@alice:server.org"; + const mockClient = getMockClientWithEventEmitter({ + ...mockClientMethodsUser(userId), + getRoom: jest.fn(), + }); + const room = new Room("!roomId:server", mockClient, userId); + mockClient.getRoom.mockReturnValue(room); + DMRoomMap.makeShared(mockClient); + + const onFinished = jest.fn(); + const getComponent = (props = {}) => + render(); + + it("should render empty state", () => { + expect(getComponent().asFragment()).toMatchSnapshot(); + }); + + it("should list spaces which are not parents of the room", () => { + const space1 = new Room("!space:server", mockClient, userId); + space1.name = "Other Space"; + jest.spyOn(SpaceStore.instance, "spacePanelSpaces", "get").mockReturnValue([space1]); + + expect(getComponent().asFragment()).toMatchSnapshot(); + }); +}); diff --git a/test/components/views/dialogs/__snapshots__/ManageRestrictedJoinRuleDialog-test.tsx.snap b/test/components/views/dialogs/__snapshots__/ManageRestrictedJoinRuleDialog-test.tsx.snap new file mode 100644 index 00000000000..61ef721c313 --- /dev/null +++ b/test/components/views/dialogs/__snapshots__/ManageRestrictedJoinRuleDialog-test.tsx.snap @@ -0,0 +1,263 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` should list spaces which are not parents of the room 1`] = ` + +
+