Replace sinon fake timers with jest timers (#11275)

This commit is contained in:
Michael Telatynski 2023-07-17 13:29:51 +01:00 committed by GitHub
parent f04a0e2860
commit 8f000384fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 21 deletions

View file

@ -143,7 +143,6 @@
"@peculiar/webcrypto": "^1.4.3", "@peculiar/webcrypto": "^1.4.3",
"@percy/cli": "^1.11.0", "@percy/cli": "^1.11.0",
"@percy/cypress": "^3.1.2", "@percy/cypress": "^3.1.2",
"@sinonjs/fake-timers": "^9.1.2",
"@testing-library/cypress": "^9.0.0", "@testing-library/cypress": "^9.0.0",
"@testing-library/jest-dom": "^5.16.5", "@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^12.1.5", "@testing-library/react": "^12.1.5",

View file

@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import FakeTimers from "@sinonjs/fake-timers";
import EventEmitter from "events"; import EventEmitter from "events";
import UserActivity from "../src/UserActivity"; import UserActivity from "../src/UserActivity";
@ -33,19 +32,17 @@ describe("UserActivity", function () {
let fakeWindow: FakeDomEventEmitter; let fakeWindow: FakeDomEventEmitter;
let fakeDocument: FakeDomEventEmitter & { hasFocus?(): boolean }; let fakeDocument: FakeDomEventEmitter & { hasFocus?(): boolean };
let userActivity: UserActivity; let userActivity: UserActivity;
let clock: FakeTimers.InstalledClock;
beforeEach(function () { beforeEach(function () {
fakeWindow = new FakeDomEventEmitter(); fakeWindow = new FakeDomEventEmitter();
fakeDocument = new FakeDomEventEmitter(); fakeDocument = new FakeDomEventEmitter();
userActivity = new UserActivity(fakeWindow as unknown as Window, fakeDocument as unknown as Document); userActivity = new UserActivity(fakeWindow as unknown as Window, fakeDocument as unknown as Document);
userActivity.start(); userActivity.start();
clock = FakeTimers.install(); jest.useFakeTimers();
}); });
afterEach(function () { afterEach(function () {
userActivity.stop(); userActivity.stop();
clock.uninstall();
}); });
it("should return the same shared instance", function () { it("should return the same shared instance", function () {
@ -74,7 +71,7 @@ describe("UserActivity", function () {
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
expect(userActivity.userActiveNow()).toBe(true); expect(userActivity.userActiveNow()).toBe(true);
expect(userActivity.userActiveRecently()).toBe(true); expect(userActivity.userActiveRecently()).toBe(true);
clock.tick(200); jest.advanceTimersByTime(200);
expect(userActivity.userActiveNow()).toBe(true); expect(userActivity.userActiveNow()).toBe(true);
expect(userActivity.userActiveRecently()).toBe(true); expect(userActivity.userActiveRecently()).toBe(true);
}); });
@ -83,7 +80,7 @@ describe("UserActivity", function () {
fakeDocument.hasFocus = jest.fn().mockReturnValue(true); fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(10000); jest.advanceTimersByTime(10000);
expect(userActivity.userActiveNow()).toBe(false); expect(userActivity.userActiveNow()).toBe(false);
}); });
@ -91,7 +88,7 @@ describe("UserActivity", function () {
fakeDocument.hasFocus = jest.fn().mockReturnValue(true); fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(10000); jest.advanceTimersByTime(10000);
expect(userActivity.userActiveRecently()).toBe(true); expect(userActivity.userActiveRecently()).toBe(true);
}); });
@ -99,7 +96,7 @@ describe("UserActivity", function () {
fakeDocument.hasFocus = jest.fn().mockReturnValue(true); fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(10000); jest.advanceTimersByTime(10000);
fakeDocument.hasFocus = jest.fn().mockReturnValue(false); fakeDocument.hasFocus = jest.fn().mockReturnValue(false);
fakeWindow.emit("blur", {}); fakeWindow.emit("blur", {});
@ -111,7 +108,7 @@ describe("UserActivity", function () {
fakeDocument.hasFocus = jest.fn().mockReturnValue(true); fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(3 * 60 * 1000); jest.advanceTimersByTime(3 * 60 * 1000);
expect(userActivity.userActiveRecently()).toBe(false); expect(userActivity.userActiveRecently()).toBe(false);
}); });
@ -120,11 +117,11 @@ describe("UserActivity", function () {
fakeDocument.hasFocus = jest.fn().mockReturnValue(true); fakeDocument.hasFocus = jest.fn().mockReturnValue(true);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(1 * 60 * 1000); jest.advanceTimersByTime(1 * 60 * 1000);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(1 * 60 * 1000); jest.advanceTimersByTime(1 * 60 * 1000);
userActivity.onUserActivity({ type: "event" } as Event); userActivity.onUserActivity({ type: "event" } as Event);
clock.tick(1 * 60 * 1000); jest.advanceTimersByTime(1 * 60 * 1000);
expect(userActivity.userActiveRecently()).toBe(true); expect(userActivity.userActiveRecently()).toBe(true);
}); });

View file

@ -18,7 +18,6 @@ limitations under the License.
import React from "react"; import React from "react";
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix"; import { MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import FakeTimers from "@sinonjs/fake-timers";
import { render } from "@testing-library/react"; import { render } from "@testing-library/react";
import { Thread } from "matrix-js-sdk/src/models/thread"; import { Thread } from "matrix-js-sdk/src/models/thread";
@ -46,7 +45,6 @@ jest.mock("../../../src/utils/beacon", () => ({
const roomId = "!roomId:server_name"; const roomId = "!roomId:server_name";
describe("MessagePanel", function () { describe("MessagePanel", function () {
let clock: FakeTimers.InstalledClock;
const events = mkEvents(); const events = mkEvents();
const userId = "@me:here"; const userId = "@me:here";
const client = getMockClientWithEventEmitter({ const client = getMockClientWithEventEmitter({
@ -116,10 +114,6 @@ describe("MessagePanel", function () {
DMRoomMap.makeShared(client); DMRoomMap.makeShared(client);
}); });
afterEach(function () {
clock?.uninstall();
});
function mkEvents() { function mkEvents() {
const events: MatrixEvent[] = []; const events: MatrixEvent[] = [];
const ts0 = Date.now(); const ts0 = Date.now();
@ -406,7 +400,7 @@ describe("MessagePanel", function () {
it("shows a ghost read-marker when the read-marker moves", function () { it("shows a ghost read-marker when the read-marker moves", function () {
// fake the clock so that we can test the velocity animation. // fake the clock so that we can test the velocity animation.
clock = FakeTimers.install(); jest.useFakeTimers();
const { container, rerender } = render( const { container, rerender } = render(
<div> <div>
@ -447,7 +441,7 @@ describe("MessagePanel", function () {
expect(readMarkers[1].previousSibling).toEqual(tiles[6]); expect(readMarkers[1].previousSibling).toEqual(tiles[6]);
// advance the clock, and then let the browser run an animation frame to let the animation start // advance the clock, and then let the browser run an animation frame to let the animation start
clock.tick(1500); jest.advanceTimersByTime(1500);
expect(hr.style.opacity).toEqual("0"); expect(hr.style.opacity).toEqual("0");
}); });