From 16b91773ee94b361e24ea7564fbc1ddf1a1e306f Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Wed, 4 May 2022 12:38:33 +0300 Subject: [PATCH] Extract helpers for testing promises to test-utils Co-authored-by: Mikko Aspiala Signed-off-by: Janne Savolainen --- src/common/runnable/run-many-for.test.ts | 21 +++++-------------- src/common/test-utils/flush-promises.ts | 5 +++++ src/common/test-utils/get-promise-status.ts | 17 +++++++++++++++ .../test-utils/get-application-builder.tsx | 3 +-- 4 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 src/common/test-utils/flush-promises.ts create mode 100644 src/common/test-utils/get-promise-status.ts diff --git a/src/common/runnable/run-many-for.test.ts b/src/common/runnable/run-many-for.test.ts index 57c31f8bbd..e8473ddca1 100644 --- a/src/common/runnable/run-many-for.test.ts +++ b/src/common/runnable/run-many-for.test.ts @@ -2,7 +2,8 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import asyncFn, { AsyncFnMock } from "@async-fn/jest"; +import type { AsyncFnMock } from "@async-fn/jest"; +import asyncFn from "@async-fn/jest"; import { createContainer, @@ -10,7 +11,9 @@ import { getInjectionToken, } from "@ogre-tools/injectable"; -import { runManyFor, Runnable } from "./run-many-for"; +import type { Runnable } from "./run-many-for"; +import { runManyFor } from "./run-many-for"; +import { getPromiseStatus } from "../test-utils/get-promise-status"; describe("runManyFor", () => { describe("given no hierarchy, when running many", () => { @@ -220,17 +223,3 @@ describe("runManyFor", () => { }); }); }); - -const flushPromises = () => new Promise(setImmediate); - -const getPromiseStatus = async (promise: Promise) => { - const status = { fulfilled: false }; - - promise.finally(() => { - status.fulfilled = true; - }); - - await flushPromises(); - - return status; -}; diff --git a/src/common/test-utils/flush-promises.ts b/src/common/test-utils/flush-promises.ts new file mode 100644 index 0000000000..55335fe445 --- /dev/null +++ b/src/common/test-utils/flush-promises.ts @@ -0,0 +1,5 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +export const flushPromises = () => new Promise(setImmediate); diff --git a/src/common/test-utils/get-promise-status.ts b/src/common/test-utils/get-promise-status.ts new file mode 100644 index 0000000000..8c171fbe54 --- /dev/null +++ b/src/common/test-utils/get-promise-status.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { flushPromises } from "./flush-promises"; + +export const getPromiseStatus = async (promise: Promise) => { + const status = { fulfilled: false }; + + promise.finally(() => { + status.fulfilled = true; + }); + + await flushPromises(); + + return status; +}; diff --git a/src/renderer/components/test-utils/get-application-builder.tsx b/src/renderer/components/test-utils/get-application-builder.tsx index 9242bad321..8501da729a 100644 --- a/src/renderer/components/test-utils/get-application-builder.tsx +++ b/src/renderer/components/test-utils/get-application-builder.tsx @@ -42,6 +42,7 @@ import namespaceStoreInjectable from "../+namespaces/namespace-store/namespace-s import clusterFrameContextInjectable from "../../cluster-frame-context/cluster-frame-context.injectable"; import startMainApplicationInjectable from "../../../main/start-main-application/start-main-application.injectable"; import startFrameInjectable from "../../start-frame/start-frame.injectable"; +import { flushPromises } from "../../../common/test-utils/flush-promises"; type Callback = (dis: DiContainers) => void | Promise; @@ -167,8 +168,6 @@ export const getApplicationBuilder = () => { menuItem.click(undefined, undefined, undefined); - const flushPromises = () => new Promise(setImmediate); - await flushPromises(); }, },