Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test.helper.js 1.76 KiB
import {act} from "react-dom/test-utils";
import {fireEvent, render, screen,waitFor} from "@testing-library/react";
import {BrowserRouter as Router} from "react-router-dom";

const TIMEOUT = 300

export async function clickItem(item, timeout = TIMEOUT) {
    await act(async () => {
        fireEvent.click(item);
        await new Promise((r) => setTimeout(r, timeout));
    });
}

export async function setMultiSelectValue(pageContent, multiSelectTestId, multiSelectValue, timeout = TIMEOUT) {
    await waitFor(() => expect(pageContent.queryByTestId(multiSelectTestId)).toBeInTheDocument()); // give rendering the time to have it.
    await clickItem(pageContent.queryByTestId(multiSelectTestId));

    let items = []
    try {
        items = screen.getAllByText(multiSelectValue);
    } catch (error) {
        throw new ReferenceError("No items found to select for multi selection element with test id '"
            + multiSelectTestId + "' and value: " + multiSelectValue)
    }

    for (let item of items) {
        await clickItem(item, timeout);
    }
}

export async function renderPage(pageComponent) {
    let content;
    await act(async () => {
        content = render(<Router>{pageComponent}</Router>);
        await new Promise((r) => setTimeout(r, 1000));
    });
    return content;
}

/**
 * Since the current version is React 17 but it has been patched to React 18, the behaviour is still the older version.
 * Therefore these specific warnings should be ignored until the app has been fully upgraded
 */
export function removeReact18ConsoleErrors() {
    jest.spyOn(global.console, 'error').mockImplementationOnce((message) => {
        if (!message.includes(' ReactDOM.render is no longer supported in React 18')) {
            global.console.error(message);
        }
    });
}