Skip to content

Adding Attributes

Daryl LaBar edited this page Nov 14, 2024 · 8 revisions

xrm-mock is opinionated and will attempt to default as much of the Xrm context as it can. For example, unless the isDirty property is explicitly set during attribute creation, it will assume that isDirty should initialise as false. The goal of this is to speed up development of unit tests.

There are three methodologies for defining how to add attributes or controls to the Xrm context. If an attribute property needs to set to a value other than what xrm-mock wants to default it to, use option 2 or 3, the component methodology. Otherwise, choose option 1.

Controls are required to test certain attributes of a field, such as visibility, or if you need test setting a notification against a control.

1. Basic methodology where only the attribute's schema name is required:

String attribute

const stringAttribute = XrmMockGenerator.Attribute.createString("firstname", "Joe");

Boolean attribute

const boolAttribute   = XrmMockGenerator.Attribute.createBool("havingFun", true);

Date attribute

const dateAttribute   = XrmMockGenerator.Attribute.createDate("birthdate", new Date(1980, 12, 25));

Number attribute

const numberAttribute = XrmMockGenerator.Attribute.createNumber("units", 2);

Lookup attribute

const lookupAttribute = XrmMockGenerator.Attribute.createLookup("primarycustomerid", {
  entityType: "contact",
  id: "{00000000-0000-0000-0000-000000000001}",
  name: "Joe Bloggs",
});

Option set attribute

const optionSetAttribute = XrmMockGenerator.Attribute.createOptionSet("countries", 0, [
    { text: "Austria", value: 0 },
    { text: "France", value: 1 },
    { text: "Spain", value: 2 },
]);

2. Component methodology, which optionally includes the control components as well:

// This example creates an attribute with the given properties, as well as two controls for it
const stringAttribute = XrmMockGenerator.Attribute.createString({
  format: "email",           // Applies to all attributes, but potential values are attribute specific
  isDirty: true,             // Applies to all standard attributes
  maxLength: 50,             // Specific to String only
  name: "emailaddress1",     // Applies to all attributes
  requiredLevel: "required", // Applies to all standard attributes
  submitMode: "always",      // Applies to all standard attributes
  value: "test@test.com",    // Applies to all standard attributes, but type is attribute specific
},
// This can be a single instance of control components, or an array of control components as it is here
[{
  disabled: true,
  label: "Email",
  name: "emailaddress1",
  visible: false,
}, {
  label: "Notification Email",
  name: "header_emailaddress1",
}]);

3. Basic Control methodology:

// This example creates an attribute with the given properties, and then creates a related control.

// Create the attribute for hideable_field and the associated control
const dependentAttribute = XrmMockGenerator.Attribute.createString({
  name: "hideable_field",
  value: "Test Value",
});

XrmMockGenerator.Control.createString({
  name: "hideable_field",
  attribute: dependentAttribute,
  visible: true, // Default to visible and expect it to be hidden in the test
});