A fake implementation of the Xrm object model. Used for testing Dynamics 365 client-side scripts.
npm install xrm-mock



bash
npm install xrm-mock -D
`
Usage
Import XrmMockGenerator in your unit test file
`typescript
import { XrmMockGenerator } from "xrm-mock";
`
Initialise a global Xrm object
`typescript
XrmMockGenerator.initialise();
`
Customise your form by adding attributes
`typescript
XrmMockGenerator.Attribute.createBool("new_havingfun", true);
`
Invoke your code and make your assertions
`typescript
Contact.onLoad();
expect(Xrm.Page.getAttribute("new_havingfun").getValue()).toBe(true);
`
Example
This example demonstrates a script with an onLoad event handler registered on a contact form. When invoked, it changes the firstname attribute's value to Bob. See the Wiki for a visual demo.
#### src/contact.ts
`typescript
export default class Contact {
public static onLoad() {
Xrm.Page.getAttribute("firstname").setValue("Bob");
}
}
`
#### test/contact.test.ts
`typescript
import Contact from "../src/contact";
import { XrmMockGenerator } from "xrm-mock";
describe("Contact", () => {
beforeEach(() => {
XrmMockGenerator.initialise();
XrmMockGenerator.Attribute.createString("firstname", "Joe");
});
it("should initially be called Joe", () => {
let name = Xrm.Page.getAttribute("firstname").getValue();
expect(name).toBe("Joe"); // Pass
});
it("should change name to Bob onLoad", () => {
Contact.onLoad();
let name = Xrm.Page.getAttribute("firstname").getValue();
expect(name).toBe("Bob"); // Pass
});
});
`
Contribute
- Submit bugs
- Implement a new function by inheriting @types/Xrm
- Test your code using npm run test
- Lint your code using npm run lint
- Build your code using npm run build
Roadmap
- Increased test coverage
- Increased implementation against different versions of @types/Xrm` (8.2 and 9)