Skip to content

Commit

Permalink
added tests, new feature flag for hiding "recents" in invite dialog s…
Browse files Browse the repository at this point in the history
…uggestions
  • Loading branch information
John Tore Simonsen committed Sep 20, 2024
1 parent a250f1f commit b966fe6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/components/views/dialogs/InviteDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
// Verji
private spaceMembers = [] as RoomMember[];
private spaceMemberIds = [] as string[];
private searchResults = [] as any;
// Verji End

public constructor(props: Props) {
Expand Down Expand Up @@ -535,8 +534,12 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
externals.forEach((id) => excludedTargetIds.add(id));
}

// VERJI added param activeSpaceMembers - used to fileter the recents based on membership in space
// VERJI added param activeSpaceMembers - used to filter the recents based on membership in space
public static buildRecents(excludedTargetIds: Set<string>, activeSpaceMembers: string[]): Result[] {
// Verji - If we don't want to see the Recents-suggestions(featureflag), we just return an empty array
if(!SettingsStore.getValue(UIFeature.ShowRecentsInSuggestions)){
return [] as Result[]
}
const rooms = DMRoomMap.shared().getUniqueRoomsWithIndividuals(); // map of userId => js-sdk Room

// Also pull in all the rooms tagged as DefaultTagID.DM so we don't miss anything. Sometimes the
Expand Down Expand Up @@ -660,7 +663,6 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
)
.then(async (r) => {
this.setState({ busy: false });
this.searchResults = r.results;
if (r.results.find((e) => e.user_id == this.state.filterText.trim())) {
foundUser = true;
}
Expand All @@ -677,18 +679,20 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial

if (foundUser == false) {
// Look in other stores for user if search might have failed unexpectedly
// VERJI - Add feature flag ShowRoomMembersInSuggestions, if false, only show searchResults, and Recents
let possibleMembers = [];
// VERJI - Add feature flag ShowRoomMembersInSuggestions, if false, only show Recents
let possibleMembers = [] as Result[];
if (SettingsStore.getValue(UIFeature.ShowRoomMembersInSuggestions)) {
possibleMembers = [
...this.state.recents,
...this.state.suggestions,
...this.state.serverResultsMixin,
...this.state.threepidResultsMixin,
...this.searchResults,
];
} else {
possibleMembers = [...this.state.recents, ...this.searchResults];
}
else if(SettingsStore.getValue(UIFeature.ShowRecentsInSuggestions)){
possibleMembers = [
...this.state.recents
];
}
const toAdd = [];
const potentialAddresses = this.state.filterText
Expand Down
4 changes: 4 additions & 0 deletions src/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,10 @@ export const SETTINGS: { [setting: string]: ISetting } = {
supportedLevels: LEVELS_UI_FEATURE,
default: true,
},
[UIFeature.ShowRecentsInSuggestions]: {
supportedLevels: LEVELS_UI_FEATURE,
default: true,
},

// Electron-specific settings, they are stored by Electron and set/read over an IPC.
// We store them over there are they are necessary to know before the renderer process launches.
Expand Down
1 change: 1 addition & 0 deletions src/settings/UIFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const enum UIFeature {
ShowInviteToSpaceFromPeoplePlus = "UIFeature.showInviteToSpaceFromPeoplePlus",
SettingShowMessageSearch = "UIFeature.settingShowMessageSearch",
ShowRoomMembersInSuggestions = "UIFeature.showRoomMembersInSuggestions",
ShowRecentsInSuggestions = "UIFeature.showRecentsInSuggestions",
}

export enum UIComponent {
Expand Down
77 changes: 76 additions & 1 deletion test/components/views/dialogs/InviteDialog-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { RoomType, MatrixClient, MatrixError, Room } from "matrix-js-sdk/src/mat
import { KnownMembership } from "matrix-js-sdk/src/types";
import { sleep } from "matrix-js-sdk/src/utils";
import { mocked, Mocked } from "jest-mock";

Check failure on line 23 in test/components/views/dialogs/InviteDialog-test.tsx

View workflow job for this annotation

GitHub Actions / ESLint

There should be at least one empty line between import groups

import InviteDialog from "../../../../src/components/views/dialogs/InviteDialog";
import { InviteKind } from "../../../../src/components/views/dialogs/InviteDialogTypes";
import {
Expand All @@ -43,6 +42,8 @@ import SettingsStore from "../../../../src/settings/SettingsStore";
import Modal from "../../../../src/Modal";
import HomePage from "../../../../src/components/structures/HomePage";
import { UIFeature } from "../../../../src/settings/UIFeature";
import * as SortMembers from "../../../../src/utils/SortMembers";
import SpaceStore from "../../../../src/stores/spaces/SpaceStore";

const mockGetAccessToken = jest.fn().mockResolvedValue("getAccessToken");
jest.mock("../../../../src/IdentityAuthClient", () =>
Expand Down Expand Up @@ -189,7 +190,81 @@ describe("InviteDialog", () => {
afterAll(() => {
jest.restoreAllMocks();
});
describe("UIFeature.ShowRoomMembersInSuggestions", () => {
const testUser = {
_userId: "@suggestedMember:verji.app",
displayName: "Suggested Member",
getMxcAvatarUrl: jest.fn().mockReturnValue(aliceProfileInfo.avatar_url),
};
const mockSpaceMembers = [
{
userId: testUser._userId,
user: {
_userId: "@suggestedMember:verji.app",
displayName: "Suggested Member",
getMxcAvatarUrl: jest.fn().mockReturnValue(aliceProfileInfo.avatar_url),
},
},
];

const memberScores: { [userId: string]: any } = {
[testUser._userId]: {
member: testUser,
score: 0.92,
numRooms: 2,
},
};
beforeEach(() => {
// Mock activeSpaceRoom to be an object with getJoinedMembers method
const roomMock: Partial<Room> = {
getJoinedMembers: jest
.fn()
.mockReturnValue([mockSpaceMembers/* mock RoomMember[] */]),
};
const mockBuildMembers = jest.spyOn(SortMembers, "buildMemberScores");
mockBuildMembers.mockImplementation(() => {
return memberScores;
});

// Mock the SpaceStore instance and activeSpaceRoom
jest.spyOn(SpaceStore.instance, "activeSpaceRoom", "get").mockReturnValue(roomMock as Room);
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

it("Should render suggestions when UIFeature.ShowRoomMembersInSuggestions is TRUE(default)", async () => {
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
if (name == UIFeature.ShowRoomMembersInSuggestions) return true;
});

render(<InviteDialog kind={InviteKind.Dm} onFinished={jest.fn()} initialText="" />);

expect(screen.queryAllByText("Suggestions").length).toBe(1);

// The "presentation" is used by the rendered tiles of members in the suggestion.
const suggestionTile = screen.getAllByRole("presentation");

expect(suggestionTile).toBeTruthy();
});

it("should NOT render suggestions when UIFeature.ShowRoomMembersInSuggestions is FALSE", async () => {
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
if (name == UIFeature.ShowRoomMembersInSuggestions) return false;
});

render(<InviteDialog kind={InviteKind.Dm} onFinished={jest.fn()} initialText="" />);

expect(screen.queryAllByText("Suggestions").length).toBe(0);

// The "presentation" is used by the rendered tiles of members in the suggestion.
const suggestionTile = screen.queryByRole("presentation");

expect(suggestionTile).toBeFalsy();
});
});
it("should label with space name", () => {
room.isSpaceRoom = jest.fn().mockReturnValue(true);
room.getType = jest.fn().mockReturnValue(RoomType.Space);
Expand Down

0 comments on commit b966fe6

Please sign in to comment.