Skip to content

Commit

Permalink
(fix) Fix flaky test failing in CI (#537)
Browse files Browse the repository at this point in the history
Fixes a flaky test by adding `waitFor` helpers to expectations.
  • Loading branch information
denniskigen authored Sep 20, 2022
1 parent 3dd07e8 commit 11331de
Showing 1 changed file with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import React from "react";
import { act, render, screen, waitFor } from "@testing-library/react";
import { Person } from "@openmrs/esm-api";
import { mockSessionStore } from "../../mock";
import {
attach,
registerExtension,
Expand All @@ -19,9 +22,6 @@ import {
configInternalStore,
getExtensionSlotsConfigStore,
} from "../../../esm-config/src";
import { act, render, screen, waitFor } from "@testing-library/react";
import { Person } from "@openmrs/esm-api";
import { mockSessionStore } from "../../mock";

jest.mock("@openmrs/esm-api", () => {
const original = jest.requireActual("@openmrs/esm-api");
Expand Down Expand Up @@ -71,10 +71,13 @@ describe("Interaction between configuration and extension systems", () => {

const slot = screen.getByTestId("slot");
const extensions = slot.childNodes;
expect(extensions[0]).toHaveTextContent("Betty");
expect(extensions[1]).toHaveTextContent("Wilma");
expect(extensions[2]).toHaveTextContent("Barney");
expect(screen.queryByText("Fred")).not.toBeInTheDocument();

await waitFor(() => {
expect(extensions[0]).toHaveTextContent("Betty");
expect(extensions[1]).toHaveTextContent("Wilma");
expect(extensions[2]).toHaveTextContent("Barney");
expect(screen.queryByText("Fred")).not.toBeInTheDocument();
});
});

test("Extensions should recieve config from module and from 'configure' key", async () => {
Expand Down Expand Up @@ -114,9 +117,12 @@ describe("Interaction between configuration and extension systems", () => {
await screen.findAllByText(/.*Pebbles.*/);

const flintstonePebbles = screen.getByTestId("flintstone-slot");
expect(flintstonePebbles).toHaveTextContent(/Pebbles:.*Springfield/);
const futurePebbles = screen.getByTestId("future-slot");
expect(futurePebbles).toHaveTextContent(/Pebbles:.*New New York/);

await waitFor(() => {
expect(flintstonePebbles).toHaveTextContent(/Pebbles:.*Springfield/);
expect(futurePebbles).toHaveTextContent(/Pebbles:.*New New York/);
});
});

test("Should be possible to attach the same extension twice with different configurations", async () => {
Expand Down Expand Up @@ -157,8 +163,11 @@ describe("Interaction between configuration and extension systems", () => {
await screen.findAllByText(/.*Dino.*/);

const slot = screen.getByTestId("flintstone-slot");
expect(slot.firstChild).toHaveTextContent(/Dino/);
expect(slot.lastChild).toHaveTextContent(/Baby Puss/);

await waitFor(() => {
expect(slot.firstChild).toHaveTextContent(/Dino/);
expect(slot.lastChild).toHaveTextContent(/Baby Puss/);
});
});

test("Slot config should update with temporary config", async () => {
Expand Down Expand Up @@ -189,7 +198,9 @@ describe("Interaction between configuration and extension systems", () => {
});
});

expect(screen.queryByText("Pearl")).not.toBeInTheDocument();
await waitFor(() =>
expect(screen.queryByText("Pearl")).not.toBeInTheDocument()
);
});

test("Extension config should update with temporary config", async () => {
Expand Down Expand Up @@ -226,10 +237,10 @@ describe("Interaction between configuration and extension systems", () => {
});
});

expect(screen.queryByText("green")).not.toBeInTheDocument();
waitFor(() =>
expect(screen.getByTestId("slot")).toHaveTextContent(/black/)
);
await waitFor(() => {
expect(screen.queryByText("green")).not.toBeInTheDocument();
expect(screen.getByTestId("slot")).toHaveTextContent(/black/);
});
});

test("Extension config should be available in extension store", async () => {
Expand Down Expand Up @@ -441,12 +452,14 @@ describe("Interaction between configuration and extension systems", () => {

render(<App />);

await waitFor(() => expect(screen.getByTestId(/slot/)).toBeInTheDocument());
expect(screen.getByTestId("slot").firstChild).toHaveAttribute(
"data-extension-id",
"Wilma"
);
expect(screen.queryAllByText(/\bSchmoo\b/)).toHaveLength(0);
await waitFor(() => {
expect(screen.getByTestId(/slot/)).toBeInTheDocument();
expect(screen.getByTestId("slot").firstChild).toHaveAttribute(
"data-extension-id",
"Wilma"
);
expect(screen.queryAllByText(/\bSchmoo\b/)).toHaveLength(0);
});
});
});

Expand Down

0 comments on commit 11331de

Please sign in to comment.