1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-09 08:42:50 +03:00

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
88 changed files with 290 additions and 226 deletions

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);