xrm-mock icon indicating copy to clipboard operation
xrm-mock copied to clipboard

Error: removeOption function

Open gabrieljunckes opened this issue 5 years ago • 3 comments

Hi.

In the code below, I am trying to remove an option from an option set field but when I call getOptions() the option still there.

Code

import { } from "mocha";
import { expect } from "chai";
import { XrmMockGenerator } from "xrm-mock";

describe("On Load Form", () => {
    beforeEach(() => {
        XrmMockGenerator.initialise();
        XrmMockGenerator.Attribute.createOptionSet("countries", 0, [
            { text: "Austria", value: 0 },
            { text: "France", value: 1 },
            { text: "Spain", value: 2 }
        ]);
    });

    it("Filter optionset", () => {
        let formContext = XrmMockGenerator.eventContext.getFormContext();
        let control = formContext.getControl<Xrm.Controls.OptionSetControl>("countries");

        control.removeOption(2);

        let length = formContext.getAttribute<Xrm.Attributes.OptionSetAttribute>("countries").getOptions().length;

        expect(length).to.equal(2);
    });
});

Result image

gabrieljunckes avatar Apr 11 '19 03:04 gabrieljunckes

Pushing this again. I have the same error.

import { XrmMockGenerator } from "xrm-mock";
import AccountEU from "../../../crmp_/js/entity/accountEU"

describe("RemoveEuOptionSets Test", () => {
    beforeEach((): void => {
        XrmMockGenerator.initialise();
        XrmMockGenerator.Attribute.createOptionSet("xyz_accountlevel", 0, [
            { text: "Large Customer", value: 1 },
            { text: "Minor Customer", value: 2 },
            { text: "Small Customer", value: 3 },
            { text: "Top Customer", value: 4 },
            { text: "Special Customer", value: 777620000 }
        ])
    });

    it("Options are gone", () => {
        AccountEU.removeOptionSetValuesEU(XrmMockGenerator.eventContext);
        const formContext = XrmMockGenerator.eventContext.getFormContext();
        const accountLevelAttribute = formContext.getAttribute<Xrm.Attributes.OptionSetAttribute>("xyz_accountlevel");
        expect(accountLevelAttribute.getOptions()).toContainEqual({ text: "Large Customer", value: 1 });
        expect(accountLevelAttribute.getOptions()).toContainEqual({ text: "Minor Customer", value: 2 });
        expect(accountLevelAttribute.getOptions()).toContainEqual({ text: "Small Customer", value: 3 });
        expect(accountLevelAttribute.getOptions()).toContainEqual({ text: "Top Customer", value: 4 });
        expect(accountLevelAttribute.getOptions()).not.toContainEqual({ text: "Special Customer", value: 777620000 });
        accountLevelAttribute.setValue(777620000);
    })

and the snippet to remove the options:

export default class AccountEU {
public static removeOptionSetValuesEU(executionContext: Xrm.Events.EventContext): void {
        const formContext = executionContext.getFormContext();

        const accountLevelControl = formContext.getControl<Xrm.Controls.OptionSetControl>("xyz_accountlevel");
        if (accountLevelControl) {
            accountLevelControl.removeOption(777620000);
        }
}

I have already had a quick look into the code, but couldn't find it quickly.

iggsn avatar Feb 09 '21 11:02 iggsn

I digged deeper into this and I can see, after calling removeOption and getting the Attribute. On Attribute level, the debugger shows still the full list of options. Stepping into the related control, I can see the options to be limited as expected. So I guess something might be missing in the implementation of the removeOption Mock function or in the getOptions implementation on the Attribute Mock.

iggsn avatar Feb 10 '21 17:02 iggsn

linking potential fix here: #60 This will only remove optionset also from attribute linked to the control, when calling removeOption(x: number).

iggsn avatar Feb 11 '21 07:02 iggsn