Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

4.1 KB
Raw Blame
import { {{name}} } from "../{{name}}";
import { IInputs } from "../{{name}}/generated/ManifestTypes";
import { mockContext, mockProperty } from "../test/pcf-context";

function mount(parameters: Partial<IInputs>, disabled = false) {
    const control = new {{name}}();
    const notify = jest.fn();
    const container = document.createElement("div");
    const context = mockContext<IInputs>(parameters, { disabled });

    control.init(context, notify, {}, container);

    return {
        control,
        notify,
        container,
        context,
        input: () => container.querySelector("input") as HTMLInputElement,
    };
}

function mountWithStrings(parameters: Partial<IInputs>) {
    const mounted = mount(parameters);
    (mounted.context as ComponentFramework.Context<IInputs> & { resources: ComponentFramework.Resources }).resources = {
        getResource: (_id: string, success: (data: string) => void) => success(""),
        getString: (key: string) => key === "{{name}}_PermissionDenied"
            ? "Localized permission denied"
            : key,
    } as ComponentFramework.Resources;
    mounted.control.updateView(mounted.context);
    return mounted;
}

test("null raw value renders an empty field", () => {
    const { input } = mount({ sampleProperty: mockProperty<string>(null) as IInputs["sampleProperty"] });

    expect(input().value).toBe("");
});

test("missing parameter object does not crash", () => {
    const { input } = mount({});

    expect(input().value).toBe("");
});

test("disabled mode disables the input", () => {
    const { input } = mount({ sampleProperty: mockProperty("Existing") as IInputs["sampleProperty"] }, true);

    expect(input().disabled).toBe(true);
});

test("non-editable security renders read-only", () => {
    const { input } = mount({
        sampleProperty: mockProperty("Existing", undefined, { editable: false }) as IInputs["sampleProperty"],
    });

    expect(input().readOnly).toBe(true);
});

test("unreadable security hides the field value", () => {
    const { container, input } = mount({
        sampleProperty: mockProperty("Hidden", undefined, { readable: false }) as IInputs["sampleProperty"],
    });

    expect(input().hidden).toBe(true);
    expect(container.textContent).not.toContain("Hidden");
});

test("unreadable security uses the localized permission string", () => {
    const { container } = mountWithStrings({
        sampleProperty: mockProperty("Hidden", undefined, { readable: false }) as IInputs["sampleProperty"],
    });

    expect(container.textContent).toContain("Localized permission denied");
});

test("change notifies exactly once and getOutputs returns the edited value", () => {
    const { control, input, notify } = mount({ sampleProperty: mockProperty("Old") as IInputs["sampleProperty"] });

    input().value = "New";
    input().dispatchEvent(new Event("input"));
    input().dispatchEvent(new Event("input"));

    expect(notify).toHaveBeenCalledTimes(1);
    expect(control.getOutputs()).toEqual({ sampleProperty: "New" });
});

test("two instances keep independent state and DOM", () => {
    const first = mount({ sampleProperty: mockProperty("First") as IInputs["sampleProperty"] });
    const second = mount({ sampleProperty: mockProperty("Second") as IInputs["sampleProperty"] });

    first.input().value = "Changed";
    first.input().dispatchEvent(new Event("input"));

    expect(first.control.getOutputs()).toEqual({ sampleProperty: "Changed" });
    expect(second.control.getOutputs()).toEqual({ sampleProperty: "Second" });
    expect(first.container.querySelector(".{{cssClass}}")).not.toBe(second.container.querySelector(".{{cssClass}}"));
});

test("destroy removes input listeners", () => {
    const { control, input, notify } = mount({ sampleProperty: mockProperty("Old") as IInputs["sampleProperty"] });
    const originalInput = input();

    control.destroy();
    originalInput.value = "Changed after destroy";
    originalInput.dispatchEvent(new Event("input"));

    expect(notify).not.toHaveBeenCalled();
});