Skip to content

Commit

Permalink
ESM tests (#755)
Browse files Browse the repository at this point in the history
* ESM tests

* fix snapshot file paths

* update snapshot

* Configure react testing library to always use strict mode. Use individual `configure` calls to specify otherwise.

* lint
  • Loading branch information
grigasp authored Oct 29, 2024
1 parent 3c3e3af commit 74f7f40
Show file tree
Hide file tree
Showing 131 changed files with 2,929 additions and 3,005 deletions.
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
],
"env": {
"NODE_ENV": "development",
"NODE_OPTIONS": "--enable-source-maps"
"NODE_OPTIONS": "--enable-source-maps --import presentation-test-utilities/node-hooks/ignore-styles"
}
},
{
Expand Down Expand Up @@ -233,7 +233,7 @@
],
"env": {
"NODE_ENV": "development",
"NODE_OPTIONS": "--enable-source-maps"
"NODE_OPTIONS": "--enable-source-maps --import presentation-test-utilities/node-hooks/ignore-styles"
}
},
{
Expand Down Expand Up @@ -273,7 +273,7 @@
],
"env": {
"NODE_ENV": "development",
"NODE_OPTIONS": "--enable-source-maps"
"NODE_OPTIONS": "--enable-source-maps --import presentation-test-utilities/node-hooks/ignore-styles"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion apps/full-stack-tests/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"require": ["ignore-styles", "./lib/setup.js"],
"checkLeaks": true,
"globals": ["requestAnimationFrame", "IS_REACT_ACT_ENVIRONMENT"],
"globals": ["requestAnimationFrame", "IS_REACT_ACT_ENVIRONMENT", "__iui"],
"timeout": 60000,
"slow": 500,
"spec": ["./lib/**/*.test.js"]
Expand Down
4 changes: 2 additions & 2 deletions apps/full-stack-tests/eslint.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ module.exports = [
...iTwinPlugin.configs.iTwinjsRecommendedConfig,
},
{
files: ["src/{components,hierarchies-react}/**/*.{ts,tsx}"],
files: ["src/{components,hierarchies-react}/**/*.{ts,tsx}", "src/**/*.tsx"],
...iTwinPlugin.configs.uiConfig,
},
{
files: ["src/{components,hierarchies-react}/**/*.{ts,tsx}"],
files: ["src/{components,hierarchies-react}/**/*.{ts,tsx}", "src/**/*.tsx"],
rules: {
...reactPlugin.configs["jsx-runtime"].rules,
},
Expand Down
6 changes: 4 additions & 2 deletions apps/full-stack-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"version": "0.0.0",
"description": "Presentation integration tests",
"private": true,
"type": "module",
"scripts": {
"build": "node ./scripts/setup-tests.cjs && tsc",
"clean": "rimraf lib build",
"cover": "npm run -s test",
"lint": "eslint ./src/**/*.{ts,tsx}",
"test": "mocha --enable-source-maps --parallel --config ./.mocharc.json",
"test:dev": "mocha --enable-source-maps --config ./.mocharc.json",
"test:dev": "cross-env NODE_OPTIONS=\"--import presentation-test-utilities/node-hooks/ignore-styles\" mocha --enable-source-maps --config ./.mocharc.json",
"test": "npm run test:dev -- --parallel --jobs=4",
"docs": "betools extract --fileExt=ts,tsx --extractFrom=./src --recursive --out=./build/docs/extract"
},
"dependencies": {
Expand Down Expand Up @@ -64,6 +65,7 @@
"chai-jest-snapshot": "catalog:test-tools",
"chai-subset": "catalog:test-tools",
"cpx2": "catalog:build-tools",
"cross-env": "catalog:build-tools",
"eslint": "catalog:build-tools",
"fast-xml-parser": "^4.5.0",
"global-jsdom": "catalog:test-tools",
Expand Down
2 changes: 1 addition & 1 deletion apps/full-stack-tests/src/IntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function initialize(props?: { backendTimeout?: number }) {
const presentationTestingInitProps: PresentationTestingInitProps = {
rpcs: [SnapshotIModelRpcInterface, IModelReadRpcInterface, PresentationRpcInterface, ECSchemaRpcInterface],
backendProps: backendInitProps,
backendHostProps: { cacheDir: path.join(__dirname, ".cache", `${process.pid}`) },
backendHostProps: { cacheDir: path.join(import.meta.dirname, ".cache", `${process.pid}`) },
frontendProps: frontendInitProps,
frontendApp: IntegrationTestsApp,
frontendAppOptions,
Expand Down
49 changes: 49 additions & 0 deletions apps/full-stack-tests/src/RenderUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { createElement, PropsWithChildren, ReactElement } from "react";
import { ThemeProvider } from "@itwin/itwinui-react";
import { RenderOptions, RenderResult, render as renderRTL } from "@testing-library/react";
import { userEvent, UserEvent } from "@testing-library/user-event";

function createWrapper(Outer: React.JSXElementConstructor<{ children: React.ReactNode }>) {
return (Inner?: React.JSXElementConstructor<{ children: React.ReactNode }>) => {
return Inner ? ({ children }: PropsWithChildren<unknown>) => createElement(Outer, undefined, createElement(Inner, undefined, children)) : Outer;
};
}

function combineWrappers(wraps: Array<ReturnType<typeof createWrapper>>, wrapper?: React.JSXElementConstructor<{ children: React.ReactNode }>) {
let currWrapper = wrapper;
for (const wrap of wraps) {
currWrapper = wrap(currWrapper);
}
return currWrapper;
}

function createDefaultWrappers(addThemeProvider?: boolean) {
const wrappers: Array<ReturnType<typeof createWrapper>> = [];
if (addThemeProvider) {
wrappers.push(createWrapper(ThemeProvider));
}
return wrappers;
}

/**
* Custom render function that wraps around `render` function from `@testing-library/react` and additionally
* setup `userEvent` from `@testing-library/user-event`.
*
* It should be used when test need to do interactions with rendered components.
*/
function customRender(ui: ReactElement, options?: RenderOptions & { addThemeProvider?: boolean }): RenderResult & { user: UserEvent } {
const wrappers = createDefaultWrappers(options?.addThemeProvider);
const wrapper = combineWrappers(wrappers, options?.wrapper);
return {
...renderRTL(ui, { ...options, wrapper }),
user: userEvent.setup(),
};
}

export * from "@testing-library/react";
export { customRender as render };
2 changes: 1 addition & 1 deletion apps/full-stack-tests/src/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { Component } from "react";
import { getByText, waitFor } from "@testing-library/react";
import { getByText, waitFor } from "../RenderUtils.js";

// __PUBLISH_EXTRACT_START__ Presentation.Components.SampleErrorBoundary
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { InstanceKey, KeySet, PresentationRpcInterface } from "@itwin/presentati
import { PresentationPropertyDataProvider } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { buildTestIModel } from "@itwin/presentation-testing";
import { render } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { render } from "../../RenderUtils.js";
import { useOptionalDisposable } from "../../UseOptionalDisposable.js";
import { ensureHasError, ErrorBoundary } from "../ErrorBoundary.js";
import { ensurePropertyGridHasPropertyRecord } from "../PropertyGridUtils.js";
Expand All @@ -27,6 +27,7 @@ describe("Learning snippets", () => {
});

after(async () => {
UiComponents.terminate();
await terminate();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import { expect } from "chai";
import { insertPhysicalElement, insertPhysicalModelWithPartition, insertSpatialCategory, waitFor } from "presentation-test-utilities";
import sinon from "sinon";
import { UiComponents } from "@itwin/components-react";
import { EmptyLocalization } from "@itwin/core-common";
import { IModelApp } from "@itwin/core-frontend";
import { ClassInfo, DefaultContentDisplayTypes, KeySet } from "@itwin/presentation-common";
import { PresentationFilterBuilderValueRenderer } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { queryByText, render } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { buildIModel, importSchema } from "../../IModelUtils.js";
import { initialize, terminate } from "../../IntegrationTests.js";
import { queryByText, render } from "../../RenderUtils.js";

describe("Presentation filter builder value renderer", () => {
before(async () => {
await initialize();
await UiComponents.initialize(new EmptyLocalization());
await UiComponents.initialize(IModelApp.localization);

sinon.stub(window.Element.prototype, "getBoundingClientRect").returns({
height: 20,
Expand All @@ -35,6 +34,7 @@ describe("Presentation filter builder value renderer", () => {

after(async () => {
sinon.restore();
UiComponents.terminate();
await terminate();
});

Expand Down Expand Up @@ -133,7 +133,7 @@ describe("Presentation filter builder value renderer", () => {
},
];

const { baseElement, getByRole } = render(
const { baseElement, getByRole, user } = render(
<PresentationFilterBuilderValueRenderer
property={testProperty}
onChange={() => {}}
Expand All @@ -146,7 +146,6 @@ describe("Presentation filter builder value renderer", () => {
);

// trigger loadTargets function
const user = userEvent.setup();
const combobox = await waitFor(() => getByRole("combobox"));
await user.click(combobox);
await waitFor(async () => {
Expand Down Expand Up @@ -250,7 +249,7 @@ describe("Presentation filter builder value renderer", () => {
},
];

const { baseElement, getByRole } = render(
const { baseElement, getByRole, user } = render(
<PresentationFilterBuilderValueRenderer
property={testProperty}
onChange={() => {}}
Expand All @@ -262,7 +261,6 @@ describe("Presentation filter builder value renderer", () => {
);

// trigger loadTargets function
const user = userEvent.setup();
const combobox = await waitFor(() => getByRole("combobox"));
await user.click(combobox);
await waitFor(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { InstanceKey, KeySet, Ruleset } from "@itwin/presentation-common";
import { TableColumnDefinition, TableRowDefinition, usePresentationTable } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { buildTestIModel } from "@itwin/presentation-testing";
import { render } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { render } from "../../RenderUtils.js";
import { ensureHasError, ErrorBoundary } from "../ErrorBoundary.js";
import { ensureTableHasRowsWithCellValues } from "../TableUtils.js";

Expand All @@ -26,6 +26,7 @@ describe("Learning snippets", () => {
});

after(async () => {
UiComponents.terminate();
await terminate();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { Ruleset } from "@itwin/presentation-common";
import { PresentationTree, PresentationTreeRenderer, usePresentationTreeState } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { buildTestIModel } from "@itwin/presentation-testing";
import { getByRole, render, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { getByRole, render, waitFor } from "../../RenderUtils.js";
import { getNodeByLabel, toggleExpandNode } from "../TreeUtils.js";

describe("Learning snippets", () => {
Expand All @@ -28,6 +28,7 @@ describe("Learning snippets", () => {

after(async () => {
delete (HTMLElement.prototype as any).scrollIntoView;
UiComponents.terminate();
await terminate();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import { IModelApp, IModelConnection } from "@itwin/core-frontend";
import { Ruleset } from "@itwin/presentation-common";
import { PresentationTree, PresentationTreeRenderer, usePresentationTreeState } from "@itwin/presentation-components";
import { buildTestIModel } from "@itwin/presentation-testing";
import { getByPlaceholderText, getByRole, getByTitle, render, waitFor } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { initialize, terminate } from "../../IntegrationTests.js";
import { getByPlaceholderText, getByRole, getByTitle, render, waitFor } from "../../RenderUtils.js";
import { stubGetBoundingClientRect } from "../../Utils.js";
import { getNodeByLabel, toggleExpandNode } from "../TreeUtils.js";

Expand All @@ -31,6 +30,7 @@ describe("Learning snippets", () => {

after(async () => {
delete (HTMLElement.prototype as any).scrollIntoView;
UiComponents.terminate();
await terminate();
});

Expand Down Expand Up @@ -74,8 +74,7 @@ describe("Learning snippets", () => {
});

// render the component
const user = userEvent.setup();
const { container, baseElement } = render(<MyTree imodel={imodel} />);
const { container, baseElement, user } = render(<MyTree imodel={imodel} />, { addThemeProvider: true });
await waitFor(() => getByRole(container, "tree"));

// find & expand the model node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { IModelApp, IModelConnection } from "@itwin/core-frontend";
import { PresentationRpcInterface, Ruleset } from "@itwin/presentation-common";
import { PresentationTree, PresentationTreeRenderer, usePresentationTreeState } from "@itwin/presentation-components";
import { buildTestIModel } from "@itwin/presentation-testing";
import { getByRole, render, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { getByRole, render, waitFor } from "../../RenderUtils.js";
import { getNodeByLabel, toggleExpandNode } from "../TreeUtils.js";

describe("Learning snippets", () => {
Expand All @@ -25,6 +25,7 @@ describe("Learning snippets", () => {

after(async () => {
delete (HTMLElement.prototype as any).scrollIntoView;
UiComponents.terminate();
await terminate();
});

Expand Down Expand Up @@ -77,7 +78,7 @@ describe("Learning snippets", () => {
});

// render the component
const { container, getByText } = render(<MyTree imodel={imodel} />);
const { container, getByText } = render(<MyTree imodel={imodel} />, { addThemeProvider: true });
await waitFor(() => getByRole(container, "tree"));

// find & expand both model nodes
Expand Down
2 changes: 1 addition & 1 deletion apps/full-stack-tests/src/components/tree/Update.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { IModelApp, IModelConnection, SnapshotConnection } from "@itwin/core-fro
import { ChildNodeSpecificationTypes, Ruleset, RuleTypes } from "@itwin/presentation-common";
import { IPresentationTreeDataProvider, usePresentationTreeState, UsePresentationTreeStateProps } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { renderHook, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { renderHook, waitFor } from "../../RenderUtils.js";

describe("Tree update", () => {
let imodel: IModelConnection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { InstanceKey, KeySet } from "@itwin/presentation-common";
import { viewWithUnifiedSelection } from "@itwin/presentation-components";
import { Presentation, TRANSIENT_ELEMENT_CLASSNAME } from "@itwin/presentation-frontend";
import { buildTestIModel as buildTestIModel } from "@itwin/presentation-testing";
import { render, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { render, waitFor } from "../../RenderUtils.js";

describe("Unified Selection", () => {
before(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { InstanceKey, KeySet } from "@itwin/presentation-common";
import { PresentationPropertyDataProvider, usePropertyDataProviderWithUnifiedSelection } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { buildTestIModel } from "@itwin/presentation-testing";
import { act, getByText, render, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { act, getByText, render, waitFor } from "../../RenderUtils.js";
import { useOptionalDisposable } from "../../UseOptionalDisposable.js";
import { ensurePropertyGridHasPropertyRecord } from "../PropertyGridUtils.js";

Expand All @@ -24,6 +24,7 @@ describe("Learning snippets", async () => {
});

after(async () => {
UiComponents.terminate();
await terminate();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
} from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { buildTestIModel } from "@itwin/presentation-testing";
import { act, getByText, render, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { act, getByText, render, waitFor } from "../../RenderUtils.js";
import { ensureTableHasRowsWithCellValues } from "../TableUtils.js";

describe("Learning snippets", async () => {
Expand All @@ -28,6 +28,7 @@ describe("Learning snippets", async () => {
});

after(async () => {
UiComponents.terminate();
await terminate();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
} from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
import { buildTestIModel } from "@itwin/presentation-testing";
import { act, fireEvent, render, waitFor } from "@testing-library/react";
import { initialize, terminate } from "../../IntegrationTests.js";
import { act, fireEvent, render, waitFor } from "../../RenderUtils.js";
import { getNodeByLabel, isNodeSelectedInTree, toggleExpandNode } from "../TreeUtils.js";

describe("Learning snippets", async () => {
Expand All @@ -29,6 +29,7 @@ describe("Learning snippets", async () => {
});

after(async () => {
UiComponents.terminate();
await terminate();
});

Expand Down
Loading

0 comments on commit 74f7f40

Please sign in to comment.