Skip to content

Commit

Permalink
Silence warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeckem committed Dec 1, 2023
1 parent 41b43e4 commit e7422f9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
30 changes: 30 additions & 0 deletions src/packages/runtime/CustomElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ interface InternalElementType extends ApplicationElement {
$inspectElementState?(): any;
}

afterEach(() => {
vi.restoreAllMocks();
});

describe("simple rendering", function () {
const SIMPLE_STYLE = ".test { color: red }";
const SIMPLE_ELEM = createCustomElement({
Expand Down Expand Up @@ -346,6 +350,8 @@ describe("application lifecycle events", function () {
});

it("does not signal 'before-stop' when start fails", async function () {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined);

const events: string[] = [];
class Listener implements ApplicationLifecycleListener {
afterApplicationStart() {
Expand Down Expand Up @@ -391,6 +397,30 @@ describe("application lifecycle events", function () {
});

expect(events).toEqual([]);
expect(errorSpy).toMatchInlineSnapshot(`
[MockFunction error] {
"calls": [
[
"#1",
[Error: help!],
],
[
"#2",
[Error: runtime:config-resolution-failed: Failed to resolve application properties.],
],
],
"results": [
{
"type": "return",
"value": undefined,
},
{
"type": "return",
"value": undefined,
},
],
}
`);
});
});

Expand Down
44 changes: 34 additions & 10 deletions src/packages/runtime/react-integration/ReactIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
/**
* @vitest-environment happy-dom
*/
import { createElement } from "react";
import { beforeEach, expect, it } from "vitest";
import { usePropertiesInternal, useServiceInternal, useServicesInternal } from "./hooks";
import { findByTestId, findByText } from "@testing-library/dom";
import { act } from "@testing-library/react";
import { createElement } from "react";
import { beforeEach, expect, it, SpyInstance, afterEach, vi } from "vitest";
import { Service, ServiceConstructor } from "../Service";
// eslint-disable-next-line import/no-relative-packages
import { UIWithProperties, UIWithService, UIWithServices } from "./test-data/test-package/UI";
import { usePropertiesInternal, useServiceInternal, useServicesInternal } from "./hooks";
import { useTheme } from "@open-pioneer/chakra-integration";
import { PackageIntl, createEmptyI18n } from "../i18n";
import { InterfaceSpec, ReferenceSpec } from "../service-layer/InterfaceSpec";
import { PackageRepr } from "../service-layer/PackageRepr";
import { ServiceLayer } from "../service-layer/ServiceLayer";
import { ServiceRepr, createConstructorFactory } from "../service-layer/ServiceRepr";
import { ReactIntegration } from "./ReactIntegration";
import { PackageRepr } from "../service-layer/PackageRepr";
import { createConstructorFactory, ServiceRepr } from "../service-layer/ServiceRepr";
import { InterfaceSpec, ReferenceSpec } from "../service-layer/InterfaceSpec";
import { createEmptyI18n, PackageIntl } from "../i18n";
import { useTheme } from "@open-pioneer/chakra-integration";

// eslint-disable-next-line import/no-relative-packages
import { UIWithProperties, UIWithService, UIWithServices } from "./test-data/test-package/UI";

interface TestProvider {
value: string;
Expand All @@ -27,6 +28,15 @@ beforeEach(() => {
document.body.innerHTML = "";
});

let errorSpy!: SpyInstance;
beforeEach(() => {
errorSpy = vi.spyOn(console, "error");
});

afterEach(() => {
vi.restoreAllMocks();
});

it("should allow access to service via react hook", async () => {
function TestComponent() {
const service = useServiceInternal<unknown>("test", "test.Provider") as TestProvider;
Expand Down Expand Up @@ -55,6 +65,8 @@ it("should allow access to service via react hook", async () => {
});

it("should get error when using undefined service", async () => {
errorSpy.mockImplementation(doNothing);

function TestComponent() {
const service = useServiceInternal<unknown>("test", "test.Provider") as TestProvider;
return createElement("span", undefined, `Hello ${service.value}`);
Expand All @@ -69,6 +81,7 @@ it("should get error when using undefined service", async () => {
integration.render(TestComponent);
});
}).toThrowErrorMatchingSnapshot();
expect(errorSpy).toHaveBeenCalledOnce();
});

it("should allow access to service with qualifier via react hook", async () => {
Expand Down Expand Up @@ -101,6 +114,8 @@ it("should allow access to service with qualifier via react hook", async () => {
});

it("should deny access to service when the qualifier does not match", async () => {
errorSpy.mockImplementation(doNothing);

function TestComponent() {
const service = useServiceInternal<unknown>("test", "test.Provider", {
qualifier: "bar"
Expand All @@ -126,6 +141,7 @@ it("should deny access to service when the qualifier does not match", async () =
integration.render(TestComponent);
});
}).toThrowErrorMatchingSnapshot();
expect(errorSpy).toHaveBeenCalledOnce();
});

it("should allow access to all services via react hook", async () => {
Expand Down Expand Up @@ -174,6 +190,8 @@ it("should allow access to all services via react hook", async () => {
});

it("should deny access to all services if declaration is missing", async () => {
errorSpy.mockImplementation(doNothing);

function TestComponent() {
const services = useServicesInternal<unknown>("test", "test.Provider") as TestProvider[];
return createElement(
Expand All @@ -192,6 +210,7 @@ it("should deny access to all services if declaration is missing", async () => {
integration.render(TestComponent);
});
}).toThrowErrorMatchingSnapshot();
expect(errorSpy).toHaveBeenCalledOnce();
});

it("should be able to read properties from react component", async () => {
Expand Down Expand Up @@ -287,6 +306,8 @@ it("should provide the autogenerated useProperties hook", async () => {
});

it("should throw error when requesting properties from an unknown package", async () => {
errorSpy.mockImplementation(doNothing);

const { integration } = createIntegration({
disablePackage: true
});
Expand All @@ -301,6 +322,7 @@ it("should throw error when requesting properties from an unknown package", asyn
integration.render(TestComponent);
});
}).toThrowErrorMatchingSnapshot();
expect(errorSpy).toHaveBeenCalledOnce();
});

it("should apply the configured chakra theme", async () => {
Expand Down Expand Up @@ -393,3 +415,5 @@ function createIntegration(options?: {
});
return { integration, wrapper };
}

function doNothing() {}

0 comments on commit e7422f9

Please sign in to comment.