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

XRMStaticMock is not assingnable to parameter of type XRMStatic #94

Open
msfmar opened this issue Jul 5, 2024 · 0 comments
Open

XRMStaticMock is not assingnable to parameter of type XRMStatic #94

msfmar opened this issue Jul 5, 2024 · 0 comments

Comments

@msfmar
Copy link

msfmar commented Jul 5, 2024

Issue:
We have a project where some classes need an instance of a Xrm.XrmStatic object for its constructor.
When trying to use a instance of XrmMock.XrmStaticMock (supplied by XrmMock.Initialize) we get the following error:

error TS2345: Argument of type 'XrmStaticMock' is not assignable to parameter of type 'XrmStatic'. The types of 'Navigation.openFile' are incompatible between these types. Type '(file: FileDetails, openFileOptions: OpenFileOptions) => void' is not assignable to type '(file: FileDetails, openFileOptions?: OpenFileOptions) => void'. Types of parameters 'openFileOptions' and 'openFileOptions' are incompatible. Type 'Xrm.Navigation.OpenFileOptions' is not assignable to type 'XrmEnum.OpenFileOptions'.

When fixing this by rewriting the modules implimentation of XrmMock.XrmStaticMock.NavigationStaticMock one gets a similar error about the sidePanes Object missing from the AppMock class compared to the App calss.

For us the fix was to simply use the static Xrm object instead of an explicitly declared variable, but I thought I would leave this here in case others would run into this issue. Also becaus it explicitly states XrmStatickMock impliments XrmStatic.

the constructor of the tested class in question looked like this:

export class TestClass{
      private xrm: Xrm.XrmStatic;
      private formContext: Xrm.FormContext;

      constructor(xrm: Xrm.XrmStatic, formContext: Xrm.FormContext){
            this.xrm = xrm;
            this.formContext = formContext;
      }
[actual functions ...]
}

the code that threw errors was similar to this:

import Sinon from "sinon";
import { XrmMockGenerator, XrmStaticMock, FormContextMock } from "xrm-mock";
import { TestClass } from "[path]";
 
xrm = XrmMockGenerator.initialize();

XrmMockGenerator.Attribute.createNumber("test",null);
const alertStub = Sinon.stub(xrm.Navigation, "openAlertDialog").resolves();

const formContext = XrmMockGenerator.getFormContext();
const testInstance = new TestClass(xrm, formContext)

await testInstance.testFunction();

[tests ...]

and this now works for us:

import Sinon from "sinon";
import { XrmMockGenerator, FormContextMock } from "xrm-mock";
import { TestClass } from "[path]";
 
XrmMockGenerator.initialize();

XrmMockGenerator.Attribute.createNumber("test",null);
const alertStub = Sinon.stub(Xrm.Navigation, "openAlertDialog").resolves();

const formContext = XrmMockGenerator.getFormContext();
const testInstance = new TestClass(Xrm, formContext)

await testInstance.testFunction();

[tests ...]
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

1 participant