/xrm-mock

:books: A mock implementation of the Xrm.Page object model. Written in TypeScript against @types/xrm definitions.

Primary LanguageTypeScriptMIT LicenseMIT

xrm-mock

A mock implementation of the Xrm.Page object model. Written in TypeScript against @types/xrm definitions.

Build Chat NPM Coverage Vulnerabilities Climate
Build Status Join the chat at https://gitter.im/xrm-mock/Lobby NPM Coverage Status Known Vulnerabilities Code Climate

📚 Usage:

  • Clone, Fork or install the repository via npm install xrm-mock

  • Create a .ts file for your entity form:

export namespace Company.Contact {
   export namespace MainForm {
       let contact: Contact;

       export function onLoad(xrm?: Xrm.XrmStatic): void {
           contact = new Contact(xrm || Xrm);
           contact.changeFirstName("Joe");
       }
   }

   class Contact {
       constructor(xrm?: Xrm.XrmStatic) {
           Xrm = xrm || Xrm;
       }

       changeFirstName(newName: string): void {
           Xrm.Page.getAttribute("firstname").setValue(newName);
       }
   }
}
  • Create a .ts file to test your entity form:
import * as XrmMock from "xrm-mock";
import { Company } from "../src/contact";

describe("contact", () => {
    beforeEach(() => {
        let attributes: Xrm.Collection.ItemCollection<Xrm.Page.Attribute> = new XrmMock.ItemCollectionMock([
            new XrmMock.AttributeMock("firstname", "Phil", false, "required")
        ]);

        this.Xrm = new XrmMock.XrmStaticMock(
            new XrmMock.PageMock(
                new XrmMock.DataMock(
                    new XrmMock.EntityMock(
                        attributes
                    )
                )
            ));
    });
    it("works", () => {
        Company.Contact.MainForm.onLoad(this.Xrm);
        let firstName: string = Xrm.Page.getAttribute("firstname").getValue();

        expect(firstName).toBe("Joe");
    });
});

❤️  Contributing and Project Roadmap:

  • Increase test coverage
  • Increase implementation
  • Include a project like rewire so that non-exported classes can be tested