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

Error: removeOption function #35

Open
gabrieljunckes opened this issue Apr 11, 2019 · 3 comments
Open

Error: removeOption function #35

gabrieljunckes opened this issue Apr 11, 2019 · 3 comments

Comments

@gabrieljunckes
Copy link

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

@iggsn
Copy link
Contributor

iggsn commented Feb 9, 2021

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
Copy link
Contributor

iggsn commented Feb 10, 2021

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
Copy link
Contributor

iggsn commented Feb 11, 2021

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

@iggsn iggsn mentioned this issue Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants