Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: address Jest test timeout issues #4956

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe("<ACLExpressionBuilder/>", () => {

it("should be able to add a sub group for ACLExpression", async () => {
const expectedResult =
"U:20483af2-fe56-4499-a54b-8d7452156895 U:eb75a832-6533-4d72-93f4-2b7a1b108951 U:1c2ff1d0-9040-4985-a450-0ff6422ba5ef AND OR";
"U:20483af2-fe56-4499-a54b-8d7452156895 U:f9ec8b09-cf64-44ff-8a0a-08a8f2f9272a U:1c2ff1d0-9040-4985-a450-0ff6422ba5ef AND U:eb75a832-6533-4d72-93f4-2b7a1b108951 AND OR";
const onFinish = jest.fn();
const { getByLabelText, container } = renderACLExpressionBuilder({
...defaultACLExpressionBuilderProps,
Expand All @@ -171,14 +171,10 @@ describe("<ACLExpressionBuilder/>", () => {
// change operator type (if operator of sub-group is same with the root it will be combined after return)
await selectOperatorForNode(container, 1, "AND");

// add user300 and user400 to new group (if group is empty it will be removed when return, and
// Add all the users to new group (if group is empty it will be removed when return, and
// if group only have one recipient it will be combined into root group).
await searchUser(container, "user");
const result = await selectAndFinished(
container,
["user300", "user400"],
onFinish,
);
const result = await selectAndFinished(container, onFinish);

expect(result).toEqual(expectedResult);
});
Expand Down Expand Up @@ -299,7 +295,6 @@ describe("<ACLExpressionBuilder/>", () => {
string,
string,
string,
string[],
(dialog: HTMLElement, queryValue: string) => Promise<void>,
string,
]
Expand All @@ -308,23 +303,20 @@ describe("<ACLExpressionBuilder/>", () => {
"users",
usersRadioLabel,
"user",
["user300", "user400"],
searchUser,
"U:20483af2-fe56-4499-a54b-8d7452156895 U:f9ec8b09-cf64-44ff-8a0a-08a8f2f9272a R:TLE_GUEST_USER_ROLE AND U:eb75a832-6533-4d72-93f4-2b7a1b108951 AND U:1c2ff1d0-9040-4985-a450-0ff6422ba5ef AND OR",
"U:20483af2-fe56-4499-a54b-8d7452156895 U:f9ec8b09-cf64-44ff-8a0a-08a8f2f9272a R:TLE_GUEST_USER_ROLE AND U:1c2ff1d0-9040-4985-a450-0ff6422ba5ef AND U:eb75a832-6533-4d72-93f4-2b7a1b108951 AND OR",
],
[
"groups",
groupsRadioLabel,
"group",
["group100", "group200", "group300", "group400"],
searchGroup,
"U:20483af2-fe56-4499-a54b-8d7452156895 U:f9ec8b09-cf64-44ff-8a0a-08a8f2f9272a R:TLE_GUEST_USER_ROLE AND G:303e758c-0051-4aea-9a8e-421f93ed9d1a AND G:d7dd1907-5731-4244-9a65-e0e847f68604 AND G:f921a6e3-69a6-4ec4-8cf8-bc193beda5f6 AND G:a2576dea-bd5c-490b-a065-637068e1a4fb AND OR",
],
[
"roles",
rolesRadioLabel,
"role",
["role100", "role200"],
searchRole,
"U:20483af2-fe56-4499-a54b-8d7452156895 U:f9ec8b09-cf64-44ff-8a0a-08a8f2f9272a R:TLE_GUEST_USER_ROLE AND R:fda99983-9eda-440a-ac68-0f746173fdcb AND R:1de3a6df-dc81-4a26-b69e-e61f8474594a AND OR",
],
Expand All @@ -334,7 +326,6 @@ describe("<ACLExpressionBuilder/>", () => {
_,
entityRadioLabel,
searchFor,
selectEntitiesName,
searchEntity,
expectedACLExpressionResult,
) => {
Expand All @@ -345,19 +336,15 @@ describe("<ACLExpressionBuilder/>", () => {
onFinish: onFinish,
});

// select entity search radio
// Select entity search radio.
await userEvent.click(getByText(entityRadioLabel));
// Attempt search for a specific entity
await searchEntity(container, searchFor);

// select the child node
// Select the child node.
await selectOperatorNode(container, 1);

const result = await selectAndFinished(
container,
selectEntitiesName,
onFinish,
);
// Select all the entities.
const result = await selectAndFinished(container, onFinish);

expect(result).toEqual(expectedACLExpressionResult);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getByText, render, RenderResult } from "@testing-library/react";
import {
findByText,
getByText,
render,
RenderResult,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import * as React from "react";
import { defaultACLEntityResolvers } from "../../../../__mocks__/ACLExpressionBuilder.mock";
Expand All @@ -31,7 +36,6 @@ import ACLExpressionBuilder, {
import type { ReferrerType } from "../../../../tsrc/components/aclexpressionbuilder/ACLHTTPReferrerInput";
import { languageStrings } from "../../../../tsrc/util/langstrings";
import { selectOption } from "../../MuiTestHelpers";
import { selectEntitiesInOneClickMode } from "../securityentitysearch/BaseSearchTestHelper";

const { ok: okLabel } = languageStrings.common.action;

Expand Down Expand Up @@ -60,16 +64,15 @@ export const renderACLExpressionBuilder = (
): RenderResult => render(<ACLExpressionBuilder {...props} />);

/**
* Attempt to select the entities show in the `EntitySearch` (User/Group/RoleSearch) result list.
* It then clicks `select` and then `ok` button.
* Select all the entities shown in the `EntitySearch` (User/Group/RoleSearch) result list by
* clicking the `Select all` button and then `ok` button.
*/
export const selectAndConfirm = async (
container: HTMLElement,
selectNames: string[],
) => {
// wait for the results, and then click all entities
await selectEntitiesInOneClickMode(container, selectNames);
// click ok button
edalex-yinzi marked this conversation as resolved.
Show resolved Hide resolved
export const selectAllAndConfirm = async (container: HTMLElement) => {
// Select all entities.
const selectAllButton = await findByText(container, "Select all", {
selector: "button",
});
await userEvent.click(selectAllButton);
await userEvent.click(getByText(container, okLabel));
};

Expand All @@ -79,10 +82,9 @@ export const selectAndConfirm = async (
*/
export const selectAndFinished = async (
container: HTMLElement,
selectNames: string[],
onFinish = jest.fn(),
): Promise<string> => {
await selectAndConfirm(container, selectNames);
await selectAllAndConfirm(container);
// get the result of ACLExpression
return onFinish.mock.lastCall[0];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("SelectGroupDialog", () => {

const result = onClose.mock.lastCall[0];
expect(result).toBeUndefined();
});
}, 15000);

it("Should be able to remove all selections by clicking the remove all button", async () => {
const result = await testRemoveAll((onClose: jest.Mock) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("SelectRoleDialog", () => {

const result = onClose.mock.lastCall[0];
expect(result).toBeUndefined();
});
}, 15000);

it("Should be able to remove all selections by clicking the remove all button", async () => {
const result = await testRemoveAll((onClose: jest.Mock) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
import * as OEQ from "@openequella/rest-api-client";
import "@testing-library/jest-dom";
import type { RenderResult } from "@testing-library/react";
import * as A from "fp-ts/Array";
import { pipe } from "fp-ts/function";
import * as SET from "fp-ts/Set";
Expand Down Expand Up @@ -54,73 +55,132 @@ const {
describe("CreateLti13Platform", () => {
const errorOutlineClass = "Mui-error";

it("creates a new platform when provided with valid platform details", async () => {
const usableByUserId = users[0].id;
const usableByUser = users[0].username;
const unknownUserDefaultGroupId = groups[0].id;
const unknownUserDefaultGroup = groups[0].name;
const defaultRoleId = roles[0].id;
const defaultRole = roles[0].name;
const expectedResult: OEQ.LtiPlatform.LtiPlatform = {
platformId: "www.test.com",
name: "test",
clientId: "client name",
keysetUrl: "http://www.platformKeyset.com",
authUrl: "https://www.test.com",
usernamePrefix: "prefix",
usernameSuffix: "suffix",
unknownUserHandling: "CREATE",
allowExpression: `* U:${usableByUserId} OR`,
instructorRoles: SET.singleton(defaultRoleId),
unknownRoles: SET.singleton(defaultRoleId),
customRoles: new Map([
[
"http://purl.imsglobal.org/vocab/lis/v2/institution/person#Guest",
SET.singleton(defaultRoleId),
],
]),
unknownUserDefaultGroups: SET.singleton(unknownUserDefaultGroupId),
enabled: true,
};
const createPlatform = jest.fn();
const usableByUserId = users[0].id;
const usableByUser = users[0].username;
const unknownUserDefaultGroupId = groups[0].id;
const unknownUserDefaultGroup = groups[0].name;
const defaultRoleId = roles[0].id;
const defaultRole = roles[0].name;

const renderResult = await renderCreateLti13Platform({
...commonCreateLti13PlatformProps,
createPlatformProvider: createPlatform,
});
const { container } = renderResult;

await configureGeneralDetails(
container,
new Map([
[platformIdLabel, expectedResult.platformId],
[nameLabel, expectedResult.name],
[clientIdLabel, expectedResult.clientId],
[platformKeysetURLLabel, expectedResult.keysetUrl],
[platformAuthenticationRequestURLLabel, expectedResult.authUrl],
[usernamePrefixLabel, expectedResult.usernamePrefix!],
[usernameSuffixLabel, expectedResult.usernameSuffix!],
]),
);
const defaultPlatform: OEQ.LtiPlatform.LtiPlatform = {
platformId: "www.test.com",
name: "test",
clientId: "client name",
keysetUrl: "http://www.platformKeyset.com",
authUrl: "https://www.test.com",
usernamePrefix: "prefix",
usernameSuffix: "suffix",
unknownUserHandling: "ERROR",
allowExpression: "*",
instructorRoles: SET.empty,
unknownRoles: SET.empty,
customRoles: new Map(),
unknownUserDefaultGroups: SET.empty,
enabled: true,
};

const usableBy = async (renderResult: RenderResult) =>
await configureUsableBy(renderResult, usableByUser);

const unknownUserHandling = async (renderResult: RenderResult) =>
await configureUnknownUserHandling(
renderResult,
unknownUserHandlingCreateLabel,
unknownUserDefaultGroup,
);

const instructorRoles = async (renderResult: RenderResult) =>
await configureInstructorRoles(renderResult, defaultRole);

const customRoles = async (renderResult: RenderResult) =>
await configureCustomRoles(
renderResult,
ltiRoles.institution.Guest,
defaultRole,
);

const unknownRoles = async (renderResult: RenderResult) =>
await configureUnknownRoles(renderResult, defaultRole);

await savePlatform(container);
it.each<
[
string,
(renderResult: RenderResult) => Promise<void>,
OEQ.LtiPlatform.LtiPlatform,
]
>([
[
"usable by",
usableBy,
{ ...defaultPlatform, allowExpression: `* U:${usableByUserId} OR` },
],
[
"unknown user handling",
unknownUserHandling,
{
...defaultPlatform,
unknownUserHandling: "CREATE",
unknownUserDefaultGroups: SET.singleton(unknownUserDefaultGroupId),
},
],
[
"instructor roles",
instructorRoles,
{
...defaultPlatform,
instructorRoles: SET.singleton(defaultRoleId),
},
],
[
"custom roles",
customRoles,
{
...defaultPlatform,
customRoles: new Map([
[
"http://purl.imsglobal.org/vocab/lis/v2/institution/person#Guest",
SET.singleton(defaultRoleId),
],
]),
},
],
[
"unknown roles",
unknownRoles,
{ ...defaultPlatform, unknownRoles: SET.singleton(defaultRoleId) },
],
])(
"supports the configuration of %s when creating a new platform",
async (_: string, configuration, expectedResult) => {
const createPlatform = jest.fn();
const renderResult = await renderCreateLti13Platform({
...commonCreateLti13PlatformProps,
createPlatformProvider: createPlatform,
});
const { container } = renderResult;

// General details are always required.
await configureGeneralDetails(
container,
new Map([
[platformIdLabel, expectedResult.platformId],
[nameLabel, expectedResult.name],
[clientIdLabel, expectedResult.clientId],
[platformKeysetURLLabel, expectedResult.keysetUrl],
[platformAuthenticationRequestURLLabel, expectedResult.authUrl],
[usernamePrefixLabel, expectedResult.usernamePrefix!],
[usernameSuffixLabel, expectedResult.usernameSuffix!],
]),
);

await configuration(renderResult);
await savePlatform(container);

const result = createPlatform.mock.lastCall[0];
expect(result).toEqual(expectedResult);
}, 45000);
const result = createPlatform.mock.lastCall[0];
expect(result).toEqual(expectedResult);
},
20000,
);

it("highlights any required fields whose value is empty", async () => {
const createPlatform = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import CreateLti13Platform, {
CreateLti13PlatformProps,
} from "../../../../../tsrc/settings/Integrations/lti13platforms/CreateLti13Platform";
import { languageStrings } from "../../../../../tsrc/util/langstrings";
import { selectAndConfirm } from "../../../components/aclexpressionbuilder/ACLExpressionBuilderTestHelper";
import { selectAllAndConfirm } from "../../../components/aclexpressionbuilder/ACLExpressionBuilderTestHelper";
import { clickOkButton } from "../../../components/securityentitydialog/SelectEntityDialogTestHelper";
import { searchAndSelectGroup } from "../../../components/securityentitydialog/SelectGroupDialogTestHelper";
import { searchAndSelectRole } from "../../../components/securityentitydialog/SelectRoleDialogTestHelper";
Expand Down Expand Up @@ -156,7 +156,7 @@ export const configureUsableBy = async (
// add a new user in acl expression builder
const aclExpressionBuilderDialog = renderResult.getByRole("dialog");
await searchUser(aclExpressionBuilderDialog, username);
await selectAndConfirm(aclExpressionBuilderDialog, [username]);
await selectAllAndConfirm(aclExpressionBuilderDialog);
};

/**
Expand Down