Add MatrixClientPeg::safeGet and use it in tests (#10985)

This commit is contained in:
Michael Telatynski 2023-06-05 18:12:23 +01:00 committed by GitHub
parent c47b587225
commit 6b46d6e4f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
88 changed files with 290 additions and 226 deletions

View file

@ -32,7 +32,7 @@ describe("BasicMessageComposer", () => {
TestUtils.stubClient();
const client: MatrixClient = MatrixClientPeg.get();
const client: MatrixClient = MatrixClientPeg.safeGet();
const roomId = "!1234567890:domain";
const userId = client.getSafeUserId();

View file

@ -73,7 +73,7 @@ describe("EventTile", () => {
jest.clearAllMocks();
stubClient();
client = MatrixClientPeg.get();
client = MatrixClientPeg.safeGet();
room = new Room(ROOM_ID, client, client.getSafeUserId(), {
pendingEventOrdering: PendingEventOrdering.Detached,

View file

@ -143,7 +143,7 @@ describe("MemberList", () => {
describe.each([false, true])("does order members correctly (presence %s)", (enablePresence) => {
beforeEach(function () {
TestUtils.stubClient();
client = MatrixClientPeg.get();
client = MatrixClientPeg.safeGet();
client.hasLazyLoadMembersEnabled = () => false;
// Make room

View file

@ -71,7 +71,7 @@ const startVoiceMessage = async (): Promise<void> => {
const setCurrentBroadcastRecording = (room: Room, state: VoiceBroadcastInfoState): void => {
const recording = new VoiceBroadcastRecording(
mkVoiceBroadcastInfoStateEvent(room.roomId, state, "@user:example.com", "ABC123"),
MatrixClientPeg.get(),
MatrixClientPeg.safeGet(),
state,
);
SdkContextClass.instance.voiceBroadcastRecordingsStore.setCurrent(recording);
@ -483,7 +483,7 @@ function wrapAndRender(
narrow = false,
tombstone?: MatrixEvent,
) {
const mockClient = MatrixClientPeg.get();
const mockClient = MatrixClientPeg.safeGet();
const roomId = "myroomid";
const room: any = props.room || {
currentState: undefined,

View file

@ -69,7 +69,7 @@ describe("RoomHeader", () => {
mockPlatformPeg({ supportsJitsiScreensharing: () => true });
stubClient();
client = mocked(MatrixClientPeg.get());
client = mocked(MatrixClientPeg.safeGet());
client.getUserId.mockReturnValue("@alice:example.org");
room = new Room("!1:example.org", client, "@alice:example.org", {
@ -750,7 +750,7 @@ interface IRoomCreationInfo {
function createRoom(info: IRoomCreationInfo) {
stubClient();
const client: MatrixClient = MatrixClientPeg.get();
const client: MatrixClient = MatrixClientPeg.safeGet();
const roomId = "!1234567890:domain";
const userId = client.getUserId()!;

View file

@ -47,7 +47,7 @@ DMRoomMap.sharedInstance = { getUserIdForRoomId, getDMRoomsForUserId };
describe("RoomList", () => {
stubClient();
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
const store = SpaceStore.instance;
function getComponent(props: Partial<ComponentProps<typeof RoomList>> = {}): JSX.Element {

View file

@ -112,7 +112,7 @@ describe("RoomListHeader", () => {
} as unknown as DMRoomMap;
DMRoomMap.setShared(dmRoomMap);
stubClient();
client = MatrixClientPeg.get();
client = MatrixClientPeg.safeGet();
mocked(shouldShowComponent).mockReturnValue(true); // show all UIComponents
});

View file

@ -33,7 +33,7 @@ jest.mock("../../../../src/IdentityAuthClient", () => {
jest.useRealTimers();
const createRoom = (roomId: string, userId: string): Room => {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const newRoom = new Room(roomId, cli, userId, {});
DMRoomMap.makeShared(cli).start();
return newRoom;
@ -92,7 +92,7 @@ describe("<RoomPreviewBar />", () => {
beforeEach(() => {
stubClient();
MatrixClientPeg.get().getUserId = jest.fn().mockReturnValue(userId);
MatrixClientPeg.safeGet().getUserId = jest.fn().mockReturnValue(userId);
});
afterEach(() => {
@ -118,7 +118,7 @@ describe("<RoomPreviewBar />", () => {
});
it("renders not logged in message", () => {
MatrixClientPeg.get().isGuest = jest.fn().mockReturnValue(true);
MatrixClientPeg.safeGet().isGuest = jest.fn().mockReturnValue(true);
const component = getComponent({ loading: true });
expect(isSpinnerRendered(component)).toBeFalsy();
@ -126,7 +126,7 @@ describe("<RoomPreviewBar />", () => {
});
it("should send room oob data to start login", async () => {
MatrixClientPeg.get().isGuest = jest.fn().mockReturnValue(true);
MatrixClientPeg.safeGet().isGuest = jest.fn().mockReturnValue(true);
const component = getComponent({
oobData: {
name: "Room Name",
@ -339,7 +339,7 @@ describe("<RoomPreviewBar />", () => {
describe("when client fails to get 3PIDs", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest.fn().mockRejectedValue({ errCode: "TEST_ERROR" });
MatrixClientPeg.safeGet().getThreePids = jest.fn().mockRejectedValue({ errCode: "TEST_ERROR" });
});
it("renders error message", async () => {
@ -354,7 +354,7 @@ describe("<RoomPreviewBar />", () => {
describe("when invitedEmail is not associated with current account", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest
MatrixClientPeg.safeGet().getThreePids = jest
.fn()
.mockResolvedValue({ threepids: mockThreePids.slice(1) });
});
@ -371,8 +371,8 @@ describe("<RoomPreviewBar />", () => {
describe("when client has no identity server connected", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
MatrixClientPeg.get().getIdentityServerUrl = jest.fn().mockReturnValue(false);
MatrixClientPeg.safeGet().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
MatrixClientPeg.safeGet().getIdentityServerUrl = jest.fn().mockReturnValue(false);
});
it("renders invite message with invited email", async () => {
@ -387,18 +387,18 @@ describe("<RoomPreviewBar />", () => {
describe("when client has an identity server connected", () => {
beforeEach(() => {
MatrixClientPeg.get().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
MatrixClientPeg.get().getIdentityServerUrl = jest.fn().mockReturnValue("identity.test");
MatrixClientPeg.get().lookupThreePid = jest.fn().mockResolvedValue("identity.test");
MatrixClientPeg.safeGet().getThreePids = jest.fn().mockResolvedValue({ threepids: mockThreePids });
MatrixClientPeg.safeGet().getIdentityServerUrl = jest.fn().mockReturnValue("identity.test");
MatrixClientPeg.safeGet().lookupThreePid = jest.fn().mockResolvedValue("identity.test");
});
it("renders email mismatch message when invite email mxid doesnt match", async () => {
MatrixClientPeg.get().lookupThreePid = jest.fn().mockReturnValue("not userid");
MatrixClientPeg.safeGet().lookupThreePid = jest.fn().mockReturnValue("not userid");
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);
expect(getMessage(component)).toMatchSnapshot();
expect(MatrixClientPeg.get().lookupThreePid).toHaveBeenCalledWith(
expect(MatrixClientPeg.safeGet().lookupThreePid).toHaveBeenCalledWith(
"email",
invitedEmail,
"mock-token",
@ -407,7 +407,7 @@ describe("<RoomPreviewBar />", () => {
});
it("renders invite message when invite email mxid match", async () => {
MatrixClientPeg.get().lookupThreePid = jest.fn().mockReturnValue(userId);
MatrixClientPeg.safeGet().lookupThreePid = jest.fn().mockReturnValue(userId);
const component = getComponent({ inviterName, invitedEmail });
await new Promise(setImmediate);

View file

@ -40,7 +40,7 @@ describe("RoomPreviewCard", () => {
beforeEach(() => {
stubClient();
client = mocked(MatrixClientPeg.get());
client = mocked(MatrixClientPeg.safeGet());
client.getUserId.mockReturnValue("@alice:example.org");
DMRoomMap.makeShared(client);

View file

@ -31,7 +31,7 @@ type Props = React.ComponentPropsWithoutRef<typeof SearchResultTile>;
describe("SearchResultTile", () => {
beforeAll(() => {
stubClient();
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const room = new Room(ROOM_ID, cli, "@bob:example.org");
jest.spyOn(cli, "getRoom").mockReturnValue(room);

View file

@ -56,6 +56,7 @@ describe("<VoiceRecordComposerTile/>", () => {
sendMessage: jest.fn(),
} as unknown as MatrixClient;
MatrixClientPeg.get = () => mockClient;
MatrixClientPeg.safeGet = () => mockClient;
const room = {
roomId,