Fix stuff happening based on timers not being run correctly in unit tests (#5764)

* Introduce helper for advancing fake time

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce reactive now to kludge around global shared state in library

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Replace all usages of "now" from mobx-utils with our own kludge to get rid of shared global state between unit tests

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Consolidate all usages of advanceTimersByTime to make sure things happening based on timers are run correctly

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix incorrect expect in test

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Enable skipped unit test since prerequisites are done

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen authored and GitHub committed 2022-07-01 19:40:34 +03:00
1 parent 3480b517c1
commit 5f57213179
13 files changed
+190 -30

No files matched your search

@@ -18,6 +18,7 @@ import downloadPlatformUpdateInjectable from "../../main/application-update/down
import quitAndInstallUpdateInjectable from "../../main/application-update/quit-and-install-update.injectable";
import appVersionInjectable from "../../common/get-configuration-file-model/app-version/app-version.injectable";
import periodicalCheckForUpdatesInjectable from "../../main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
describe("analytics for installing update", () => {
let applicationBuilder: ApplicationBuilder;
@@ -27,9 +28,7 @@ describe("analytics for installing update", () => {
let mainDi: DiContainer;
beforeEach(async () => {
jest.useFakeTimers();
global.Date.now = () => new Date("2015-10-21T07:28:00Z").getTime();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder();
@@ -84,14 +83,14 @@ describe("analytics for installing update", () => {
it("when enough time passes to check for updates again, sends event to analytics for being checked periodically", () => {
analyticsListenerMock.mockClear();
jest.advanceTimersByTime(1000 * 60 * 60 * 2);
advanceFakeTime(1000 * 60 * 60 * 2);
expect(analyticsListenerMock).toHaveBeenCalledWith({
name: "app",
action: "checking-for-updates",
params: {
currentDateTime: "2015-10-21T07:28:00Z",
currentDateTime: "2015-10-21T09:28:00Z",
source: "periodic",
},
});
@@ -16,6 +16,7 @@ import type { ApplicationBuilder } from "../../renderer/components/test-utils/ge
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import processCheckingForUpdatesInjectable from "../../main/application-update/check-for-updates/process-checking-for-updates.injectable";
import quitAndInstallUpdateInjectable from "../../main/application-update/quit-and-install-update.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
function daysToMilliseconds(days: number) {
return Math.round(days * 24 * 60 * 60 * 1000);
@@ -28,7 +29,7 @@ describe("encourage user to update when sufficient time passed since update was
let quitAndInstallUpdateMock: jest.MockedFunction<() => void>;
beforeEach(() => {
jest.useFakeTimers();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder();
@@ -128,13 +129,13 @@ describe("encourage user to update when sufficient time passed since update was
});
it("given just enough time passes for medium update encouragement, has medium emotional indication in the button", () => {
jest.advanceTimersByTime(daysToMilliseconds(22));
advanceFakeTime(daysToMilliseconds(22));
expect(button).toHaveAttribute("data-warning-level", "medium");
});
it("given just enough time passes for severe update encouragement, has severe emotional indication in the button", () => {
jest.advanceTimersByTime(daysToMilliseconds(26));
advanceFakeTime(daysToMilliseconds(26));
expect(button).toHaveAttribute("data-warning-level", "high");
});
@@ -16,6 +16,7 @@ import type { DownloadPlatformUpdate } from "../../main/application-update/downl
import downloadPlatformUpdateInjectable from "../../main/application-update/download-platform-update/download-platform-update.injectable";
import setUpdateOnQuitInjectable from "../../main/electron-app/features/set-update-on-quit.injectable";
import processCheckingForUpdatesInjectable from "../../main/application-update/check-for-updates/process-checking-for-updates.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
describe("installing update", () => {
let applicationBuilder: ApplicationBuilder;
@@ -25,7 +26,7 @@ describe("installing update", () => {
let setUpdateOnQuitMock: jest.Mock;
beforeEach(() => {
jest.useFakeTimers();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder();
@@ -114,8 +115,8 @@ describe("installing update", () => {
expect(rendered.baseElement).toMatchSnapshot();
});
it.skip("when 5 seconds elapses, clears the notification to the user", () => {
jest.advanceTimersByTime(6000);
it("when 5 seconds elapses, clears the notification to the user", () => {
advanceFakeTime(6000);
expect(rendered.getByTestId("app-update-idle")).toBeInTheDocument();
});
@@ -11,6 +11,7 @@ import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import processCheckingForUpdatesInjectable from "../../main/application-update/check-for-updates/process-checking-for-updates.injectable";
import periodicalCheckForUpdatesInjectable from "../../main/application-update/periodical-check-for-updates/periodical-check-for-updates.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
const ENOUGH_TIME = 1000 * 60 * 60 * 2;
@@ -19,7 +20,7 @@ describe("periodical checking of updates", () => {
let processCheckingForUpdatesMock: AsyncFnMock<() => Promise<void>>;
beforeEach(() => {
jest.useFakeTimers();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder();
@@ -59,7 +60,7 @@ describe("periodical checking of updates", () => {
it("when just not enough time passes, does not check for updates again automatically yet", () => {
processCheckingForUpdatesMock.mockClear();
jest.advanceTimersByTime(ENOUGH_TIME - 1);
advanceFakeTime(ENOUGH_TIME - 1);
expect(processCheckingForUpdatesMock).not.toHaveBeenCalled();
});
@@ -67,7 +68,7 @@ describe("periodical checking of updates", () => {
it("when just enough time passes, checks for updates again automatically", () => {
processCheckingForUpdatesMock.mockClear();
jest.advanceTimersByTime(ENOUGH_TIME);
advanceFakeTime(ENOUGH_TIME);
expect(processCheckingForUpdatesMock).toHaveBeenCalled();
});
@@ -22,6 +22,7 @@ import pathExistsInjectable from "../../common/fs/path-exists.injectable";
import readJsonFileInjectable from "../../common/fs/read-json-file.injectable";
import { navigateToRouteInjectionToken } from "../../common/front-end-routing/navigate-to-route-injection-token";
import sidebarStorageInjectable from "../../renderer/components/layout/sidebar-storage/sidebar-storage.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
describe("cluster - sidebar and tab navigation for core", () => {
let applicationBuilder: ApplicationBuilder;
@@ -29,7 +30,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
let rendered: RenderResult;
beforeEach(() => {
jest.useFakeTimers();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder();
rendererDi = applicationBuilder.dis.rendererDi;
@@ -262,7 +263,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
});
it("when not enough time passes, does not store state for expanded sidebar items to file system yet", async () => {
jest.advanceTimersByTime(250 - 1);
advanceFakeTime(250 - 1);
const pathExistsFake = rendererDi.inject(pathExistsInjectable);
@@ -274,7 +275,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
});
it("when enough time passes, stores state for expanded sidebar items to file system", async () => {
jest.advanceTimersByTime(250);
advanceFakeTime(250);
const readJsonFileFake = rendererDi.inject(readJsonFileInjectable);
@@ -18,6 +18,7 @@ import { navigateToRouteInjectionToken } from "../../common/front-end-routing/na
import assert from "assert";
import type { FakeExtensionData } from "../../renderer/components/test-utils/get-renderer-extension-fake";
import { getRendererExtensionFakeFor } from "../../renderer/components/test-utils/get-renderer-extension-fake";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
describe("cluster - sidebar and tab navigation for extensions", () => {
let applicationBuilder: ApplicationBuilder;
@@ -25,7 +26,7 @@ describe("cluster - sidebar and tab navigation for extensions", () => {
let rendered: RenderResult;
beforeEach(() => {
jest.useFakeTimers();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder();
rendererDi = applicationBuilder.dis.rendererDi;
@@ -278,7 +279,7 @@ describe("cluster - sidebar and tab navigation for extensions", () => {
});
it("when not enough time passes, does not store state for expanded sidebar items to file system yet", async () => {
jest.advanceTimersByTime(250 - 1);
advanceFakeTime(250 - 1);
const pathExistsFake = rendererDi.inject(pathExistsInjectable);
@@ -290,7 +291,7 @@ describe("cluster - sidebar and tab navigation for extensions", () => {
});
it("when enough time passes, stores state for expanded sidebar items to file system", async () => {
jest.advanceTimersByTime(250);
advanceFakeTime(250);
const readJsonFileFake = rendererDi.inject(readJsonFileInjectable);
@@ -10,6 +10,7 @@ import { lensWindowInjectionToken } from "../../main/start-main-application/lens
import exitAppInjectable from "../../main/electron-app/features/exit-app.injectable";
import clusterManagerInjectable from "../../main/cluster-manager.injectable";
import stopServicesAndExitAppInjectable from "../../main/stop-services-and-exit-app.injectable";
import { advanceFakeTime, useFakeTime } from "../../common/test-utils/use-fake-time";
describe("quitting the app using application menu", () => {
describe("given application has started", () => {
@@ -18,7 +19,7 @@ describe("quitting the app using application menu", () => {
let exitAppMock: jest.Mock;
beforeEach(async () => {
jest.useFakeTimers();
useFakeTime("2015-10-21T07:28:00Z");
applicationBuilder = getApplicationBuilder().beforeApplicationStart(
({ mainDi }) => {
@@ -71,14 +72,14 @@ describe("quitting the app using application menu", () => {
});
it("after insufficient time passes, does not terminate application yet", () => {
jest.advanceTimersByTime(999);
advanceFakeTime(999);
expect(exitAppMock).not.toHaveBeenCalled();
});
describe("after sufficient time passes", () => {
beforeEach(() => {
jest.advanceTimersByTime(1000);
advanceFakeTime(1000);
});
it("terminates application", () => {