1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-31 13:44:28 +03:00

Add ESLint Jest (#10261)

This commit is contained in:
Michael Weimann
2023-03-01 16:23:35 +01:00
committed by GitHub
parent db7748b743
commit 5398db21ad
55 changed files with 336 additions and 351 deletions

View File

@ -35,7 +35,7 @@ describe("createReconnectedListener", () => {
].forEach(([from, to]) => {
it(`should invoke the callback on a transition from ${from} to ${to}`, () => {
reconnectedListener(to, from);
expect(onReconnect).toBeCalled();
expect(onReconnect).toHaveBeenCalled();
});
});
@ -46,7 +46,7 @@ describe("createReconnectedListener", () => {
].forEach(([from, to]) => {
it(`should not invoke the callback on a transition from ${from} to ${to}`, () => {
reconnectedListener(to, from);
expect(onReconnect).not.toBeCalled();
expect(onReconnect).not.toHaveBeenCalled();
});
});
});

View File

@ -130,8 +130,8 @@ describe("parseUserAgent()", () => {
const testPlatform = (platform: string, userAgents: string[], results: ExtendedDeviceInformation[]): void => {
const testCases: TestCase[] = userAgents.map((userAgent, index) => [userAgent, results[index]]);
describe(platform, () => {
it.each(testCases)("Parses user agent correctly - %s", (userAgent, expectedResult) => {
describe(`on platform ${platform}`, () => {
it.each(testCases)("should parse the user agent correctly - %s", (userAgent, expectedResult) => {
expect(parseUserAgent(userAgent)).toEqual(expectedResult);
});
});

View File

@ -290,7 +290,7 @@ describe("export", function () {
],
];
it.each(invalidExportOptions)("%s", (_d, options) => {
expect(() => new PlainTextExporter(mockRoom, ExportType.Beginning, options, setProgressText)).toThrowError(
expect(() => new PlainTextExporter(mockRoom, ExportType.Beginning, options, setProgressText)).toThrow(
"Invalid export options",
);
});

View File

@ -329,31 +329,28 @@ describe("HTMLExport", () => {
// test link to the first page
//@ts-ignore private access
exporter.wrapHTML("", 0, 3).then((res) => {
expect(res).not.toContain("Previous group of messages");
expect(res).toContain(
'<div style="text-align:center;margin:10px"><a href="./messages2.html" style="font-weight:bold">Next group of messages</a></div>',
);
});
let result = await exporter.wrapHTML("", 0, 3);
expect(result).not.toContain("Previous group of messages");
expect(result).toContain(
'<div style="text-align:center;margin:10px"><a href="./messages2.html" style="font-weight:bold">Next group of messages</a></div>',
);
// test link for a middle page
//@ts-ignore private access
exporter.wrapHTML("", 1, 3).then((res) => {
expect(res).toContain(
'<div style="text-align:center"><a href="./messages.html" style="font-weight:bold">Previous group of messages</a></div>',
);
expect(res).toContain(
'<div style="text-align:center;margin:10px"><a href="./messages3.html" style="font-weight:bold">Next group of messages</a></div>',
);
});
result = await exporter.wrapHTML("", 1, 3);
expect(result).toContain(
'<div style="text-align:center"><a href="./messages.html" style="font-weight:bold">Previous group of messages</a></div>',
);
expect(result).toContain(
'<div style="text-align:center;margin:10px"><a href="./messages3.html" style="font-weight:bold">Next group of messages</a></div>',
);
// test link for last page
//@ts-ignore private access
exporter.wrapHTML("", 2, 3).then((res) => {
expect(res).toContain(
'<div style="text-align:center"><a href="./messages2.html" style="font-weight:bold">Previous group of messages</a></div>',
);
expect(res).not.toContain("Next group of messages");
});
result = await exporter.wrapHTML("", 2, 3);
expect(result).toContain(
'<div style="text-align:center"><a href="./messages2.html" style="font-weight:bold">Previous group of messages</a></div>',
);
expect(result).not.toContain("Next group of messages");
});
});

View File

@ -120,16 +120,14 @@ describe("local-room", () => {
mocked(isRoomReady).mockReturnValue(false);
});
it("should invoke the callbacks, set the room state to created and return the actual room id", (done) => {
it("should invoke the callbacks, set the room state to created and return the actual room id", async () => {
const prom = localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
jest.advanceTimersByTime(5000);
prom.then((roomId: string) => {
expect(localRoom.state).toBe(LocalRoomState.CREATED);
expect(localRoomCallbackRoomId).toBe(room1.roomId);
expect(roomId).toBe(room1.roomId);
expect(jest.getTimerCount()).toBe(0);
done();
});
const roomId = await prom;
expect(localRoom.state).toBe(LocalRoomState.CREATED);
expect(localRoomCallbackRoomId).toBe(room1.roomId);
expect(roomId).toBe(room1.roomId);
expect(jest.getTimerCount()).toBe(0);
});
});
@ -138,17 +136,15 @@ describe("local-room", () => {
mocked(isRoomReady).mockReturnValue(false);
});
it("should invoke the callbacks, set the room state to created and return the actual room id", (done) => {
it("should invoke the callbacks, set the room state to created and return the actual room id", async () => {
const prom = localRoomModule.waitForRoomReadyAndApplyAfterCreateCallbacks(client, localRoom);
mocked(isRoomReady).mockReturnValue(true);
jest.advanceTimersByTime(500);
prom.then((roomId: string) => {
expect(localRoom.state).toBe(LocalRoomState.CREATED);
expect(localRoomCallbackRoomId).toBe(room1.roomId);
expect(roomId).toBe(room1.roomId);
expect(jest.getTimerCount()).toBe(0);
done();
});
const roomId = await prom;
expect(localRoom.state).toBe(LocalRoomState.CREATED);
expect(localRoomCallbackRoomId).toBe(room1.roomId);
expect(roomId).toBe(room1.roomId);
expect(jest.getTimerCount()).toBe(0);
});
});
});

View File

@ -51,7 +51,7 @@ describe("isRoomReady", () => {
mocked(client.getRoom).mockReturnValue(null);
});
it("it should return false", () => {
it("should return false", () => {
expect(isRoomReady(client, localRoom)).toBe(false);
});
@ -63,7 +63,7 @@ describe("isRoomReady", () => {
});
});
it("it should return false", () => {
it("should return false", () => {
expect(isRoomReady(client, localRoom)).toBe(false);
});
@ -75,7 +75,7 @@ describe("isRoomReady", () => {
]);
});
it("it should return false", () => {
it("should return false", () => {
expect(isRoomReady(client, localRoom)).toBe(false);
});
@ -92,7 +92,7 @@ describe("isRoomReady", () => {
]);
});
it("it should return true", () => {
it("should return true", () => {
expect(isRoomReady(client, localRoom)).toBe(true);
});
@ -101,7 +101,7 @@ describe("isRoomReady", () => {
localRoom.encrypted = true;
});
it("it should return false", () => {
it("should return false", () => {
expect(isRoomReady(client, localRoom)).toBe(false);
});
@ -118,7 +118,7 @@ describe("isRoomReady", () => {
]);
});
it("it should return true", () => {
it("should return true", () => {
expect(isRoomReady(client, localRoom)).toBe(true);
});
});

View File

@ -29,31 +29,32 @@ describe("waitForMember", () => {
client = new EventEmitter();
});
it("resolves with false if the timeout is reached", (done) => {
waitForMember(<MatrixClient>client, "", "", { timeout: 0 }).then((r) => {
expect(r).toBe(false);
done();
});
afterEach(() => {
jest.useRealTimers();
});
it("resolves with false if the timeout is reached, even if other RoomState.newMember events fire", (done) => {
it("resolves with false if the timeout is reached", async () => {
const result = await waitForMember(<MatrixClient>client, "", "", { timeout: 0 });
expect(result).toBe(false);
});
it("resolves with false if the timeout is reached, even if other RoomState.newMember events fire", async () => {
jest.useFakeTimers();
const roomId = "!roomId:domain";
const userId = "@clientId:domain";
waitForMember(<MatrixClient>client, roomId, userId, { timeout }).then((r) => {
expect(r).toBe(false);
done();
});
const resultProm = waitForMember(<MatrixClient>client, roomId, userId, { timeout });
jest.advanceTimersByTime(50);
expect(await resultProm).toBe(false);
client.emit("RoomState.newMember", undefined, undefined, { roomId, userId: "@anotherClient:domain" });
jest.useRealTimers();
});
it("resolves with true if RoomState.newMember fires", (done) => {
it("resolves with true if RoomState.newMember fires", async () => {
const roomId = "!roomId:domain";
const userId = "@clientId:domain";
waitForMember(<MatrixClient>client, roomId, userId, { timeout }).then((r) => {
expect(r).toBe(true);
expect((<MatrixClient>client).listeners(RoomStateEvent.NewMember).length).toBe(0);
done();
});
expect((<MatrixClient>client).listeners(RoomStateEvent.NewMember).length).toBe(0);
const resultProm = waitForMember(<MatrixClient>client, roomId, userId, { timeout });
client.emit("RoomState.newMember", undefined, undefined, { roomId, userId });
expect(await resultProm).toBe(true);
});
});

View File

@ -128,7 +128,7 @@ describe("notifications", () => {
it("sends a request even if everything has been read", () => {
clearRoomNotification(room, client);
expect(sendReadReceiptSpy).not.toBeCalled();
expect(sendReadReceiptSpy).not.toHaveBeenCalled();
});
it("marks the room as read even if the receipt failed", async () => {
@ -163,7 +163,7 @@ describe("notifications", () => {
it("does not send any requests if everything has been read", () => {
clearAllNotifications(client);
expect(sendReadReceiptSpy).not.toBeCalled();
expect(sendReadReceiptSpy).not.toHaveBeenCalled();
});
it("sends unthreaded receipt requests", () => {
@ -178,7 +178,7 @@ describe("notifications", () => {
clearAllNotifications(client);
expect(sendReadReceiptSpy).toBeCalledWith(message, ReceiptType.Read, true);
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.Read, true);
});
it("sends private read receipts", () => {
@ -195,7 +195,7 @@ describe("notifications", () => {
clearAllNotifications(client);
expect(sendReadReceiptSpy).toBeCalledWith(message, ReceiptType.ReadPrivate, true);
expect(sendReadReceiptSpy).toHaveBeenCalledWith(message, ReceiptType.ReadPrivate, true);
});
});
});