Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix flake unit test (#67942)

Egor Grushin43ad099236e3
diff --git a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts
index c808e9f3a46..419c45f64b8 100644
--- a/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/api/ai-streaming/test-utils.ts
@@ -1,11 +1,5 @@
import { defer } from "metabase/lib/promise";
-async function delay(timeout: number) {
- return new Promise((resolve) => {
- setTimeout(() => resolve(undefined), timeout);
- });
-}
-
export function createPauses<Count extends number>(count: Count) {
const pauses = new Array(count).fill(null).map(() => defer());
return pauses as ReturnType<typeof defer>[] & { length: Count };
@@ -65,20 +59,22 @@ export type MockStreamedEndpointParams =
| {
textChunks: string[] | undefined;
stream?: undefined;
- initialDelay?: number;
+ waitForResponse?: boolean;
}
| {
textChunks?: undefined;
stream: ReadableStream<any>;
- initialDelay?: number;
+ waitForResponse?: boolean;
};
export function mockStreamedEndpoint(
url: string,
- { textChunks, stream, initialDelay = 0 }: MockStreamedEndpointParams,
+ { textChunks, stream, waitForResponse = false }: MockStreamedEndpointParams,
) {
- return mockEndpoint(url, async (init) => {
- await delay(initialDelay);
+ const responseGate = waitForResponse ? defer() : null;
+
+ const mock = mockEndpoint(url, async (init) => {
+ await responseGate?.promise;
const body =
stream ||
(textChunks && createMockReadableStream(textChunks)) ||
@@ -111,4 +107,8 @@ export function mockStreamedEndpoint(
return { status: 202, ok: true, body } as any;
});
+
+ return Object.assign(mock, {
+ sendResponse: () => responseGate?.resolve(),
+ });
}
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/history.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/history.unit.spec.tsx
index d30fb64eba8..22a88c8cc35 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/tests/history.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/history.unit.spec.tsx
@@ -28,11 +28,12 @@ describe("metabot > history", () => {
it("should send conversation history along with future messages", async () => {
setup();
- mockAgentEndpoint({
+ const { sendResponse } = mockAgentEndpoint({
textChunks: whoIsYourFavoriteResponse,
- initialDelay: 50,
+ waitForResponse: true,
});
await enterChatMessage("Who is your favorite?");
+ sendResponse();
expect(
await screen.findByText("You, but don't tell anyone."),
).toBeInTheDocument();
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/message.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/message.unit.spec.tsx
index e4a64bef4c4..d9c0ab4cd5e 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/tests/message.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/message.unit.spec.tsx
@@ -17,9 +17,9 @@ describe("metabot > message", () => {
it("should properly send chat messages", async () => {
setup();
- mockAgentEndpoint({
+ const { sendResponse } = mockAgentEndpoint({
textChunks: whoIsYourFavoriteResponse,
- initialDelay: 50,
+ waitForResponse: true,
});
await enterChatMessage("Who is your favorite?", false);
@@ -27,10 +27,12 @@ describe("metabot > message", () => {
await enterChatMessage("Who is your favorite?");
expect(await responseLoader()).toBeInTheDocument();
+
+ sendResponse();
+
expect(
await screen.findByText("You, but don't tell anyone."),
).toBeInTheDocument();
-
expect(await input()).toHaveTextContent("");
expect(await input()).toHaveFocus();
});