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

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

View file

@ -80,7 +80,7 @@ describe("MultiInviter", () => {
jest.resetAllMocks();
TestUtilsMatrix.stubClient();
client = MatrixClientPeg.get() as jest.Mocked<MatrixClient>;
client = MatrixClientPeg.safeGet() as jest.Mocked<MatrixClient>;
client.invite = jest.fn();
client.invite.mockResolvedValue({});

View file

@ -53,7 +53,7 @@ describe("export", function () {
let events: MatrixEvent[];
beforeEach(() => {
stubClient();
client = MatrixClientPeg.get();
client = MatrixClientPeg.safeGet();
client.getUserId = () => {
return MY_USER_ID;
};

View file

@ -40,7 +40,7 @@ describe("leaveRoomBehaviour", () => {
beforeEach(async () => {
stubClient();
client = mocked(MatrixClientPeg.get());
client = mocked(MatrixClientPeg.safeGet());
DMRoomMap.makeShared(client);
room = mkRoom(client, "!1:example.org");

View file

@ -119,7 +119,7 @@ describe("notifications", () => {
beforeEach(() => {
stubClient();
client = mocked(MatrixClientPeg.get());
client = mocked(MatrixClientPeg.safeGet());
room = new Room(ROOM_ID, client, USER_ID);
message = mkMessage({
event: true,
@ -172,7 +172,7 @@ describe("notifications", () => {
beforeEach(() => {
stubClient();
client = mocked(MatrixClientPeg.get());
client = mocked(MatrixClientPeg.safeGet());
room = new Room(ROOM_ID, client, USER_ID);
sendReadReceiptSpy = jest.spyOn(client, "sendReadReceipt").mockResolvedValue({});
jest.spyOn(client, "getRooms").mockReturnValue([room]);

View file

@ -36,7 +36,7 @@ describe("pillify", () => {
beforeEach(() => {
stubClient();
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
(cli.getRoom as jest.Mock).mockReturnValue(new Room(roomId, cli, cli.getUserId()!));
cli.pushRules!.global = {
override: [
@ -69,7 +69,7 @@ describe("pillify", () => {
const { container } = render(<div />);
const originalHtml = container.outerHTML;
const containers: Element[] = [];
pillifyLinks(MatrixClientPeg.get(), [container], event, containers);
pillifyLinks(MatrixClientPeg.safeGet(), [container], event, containers);
expect(containers).toHaveLength(0);
expect(container.outerHTML).toEqual(originalHtml);
});
@ -77,7 +77,7 @@ describe("pillify", () => {
it("should pillify @room", () => {
const { container } = render(<div>@room</div>);
const containers: Element[] = [];
pillifyLinks(MatrixClientPeg.get(), [container], event, containers);
pillifyLinks(MatrixClientPeg.safeGet(), [container], event, containers);
expect(containers).toHaveLength(1);
expect(container.querySelector(".mx_Pill.mx_AtRoomPill")?.textContent).toBe("!@room");
});
@ -85,10 +85,10 @@ describe("pillify", () => {
it("should not double up pillification on repeated calls", () => {
const { container } = render(<div>@room</div>);
const containers: Element[] = [];
pillifyLinks(MatrixClientPeg.get(), [container], event, containers);
pillifyLinks(MatrixClientPeg.get(), [container], event, containers);
pillifyLinks(MatrixClientPeg.get(), [container], event, containers);
pillifyLinks(MatrixClientPeg.get(), [container], event, containers);
pillifyLinks(MatrixClientPeg.safeGet(), [container], event, containers);
pillifyLinks(MatrixClientPeg.safeGet(), [container], event, containers);
pillifyLinks(MatrixClientPeg.safeGet(), [container], event, containers);
pillifyLinks(MatrixClientPeg.safeGet(), [container], event, containers);
expect(containers).toHaveLength(1);
expect(container.querySelector(".mx_Pill.mx_AtRoomPill")?.textContent).toBe("!@room");
});