web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Dynamics CRM 2011 Unit Test Part 11: QUnit with client side JavaScript

Zhongchen Zhou Profile Picture Zhongchen Zhou 681

Dynamics CRM 2011 Unit Test Part 1: Introduction and Series Contents

The complete sample code using QUnit can be downloaded from MSDN sample gallery: http://code.msdn.microsoft.com/Dynamics-CRM-2011-client-9f189663

This post describe how to use QUnit and Xrm.Page Script Library Template (Microsoft Visual Studio extension) to unit test Dynamics CRM 2011 client side jQuery or JavaScript code.

More information on Xrm.Page Script Library Templatecan be found: http://msdn.microsoft.com/en-us/library/gg328261.aspx#BKMK_DevelopmentTools

More information on QUnit can be found: http://qunitjs.com/

Suppose we have one JavaScript web resource which will run when account form load. The client side JavaScript code will update the “Account Name” label to “Organisation Name” and set Preferred User look up field value to the Preferred User of the Primary Contact of the current account.

Code Under Test


/// <reference path="XrmPageTemplate.js" />

//This Library uses a namespace naming strategy to help prevent duplication function names
if (typeof (Zzhou) == "undefined")
{ Zzhou = { __namespace: true }; }
if (typeof (Zzhou.Qunit) == "undefined")
{ Zzhou.Qunit = { __namespace: true }; }
// Namespace container for functions in this library.
Zzhou.Qunit.Account = {
 onload: function () {
 var attrName = 'name',
 label = 'Organisation Name';
 this._changeControlLabel(attrName, label);
 this._updatePreferredUser();
 },
 _changeControlLabel: function (attrName, label) {
 var control = Xrm.Page.getControl(attrName);
 if (control) {
 control.setLabel(label);
 }
 },
 _updatePreferredUser: function () {
 // primary contact look up field name
 var attrName = 'primarycontactid';
 // get primary contact attribute
 var attribute = Xrm.Page.getAttribute(attrName);
 if (attribute) {
 // get primary contact attribute value
 var attrValue = attribute.getValue();
 if (attrValue && attrValue[0] && attrValue[0].entityType === 'contact') {
 // get primary contact id
 var primaryContactId = attrValue[0].id;
 // retrieve primary contact.PreferredSystemUserId
 SDK.JQuery.retrieveRecord(
 primaryContactId,
 "Contact",
 "PreferredSystemUserId",
 null,
 // success call back
 function (user) {
 if (user && user.PreferredSystemUserId) {
 var attrName = 'preferredsystemuserid';
 var attribute = Xrm.Page.getAttribute(attrName);
 if (attribute) {
 var lookupReference = [];
 lookupReference[0] = {};
 lookupReference[0].id = user.PreferredSystemUserId.Id;
 lookupReference[0].entityType = user.PreferredSystemUserId.LogicalName;
 lookupReference[0].name = user.PreferredSystemUserId.Name;

attribute.setValue(lookupReference);
 }
 }
 },
 function (error) {
 alert(error.message);
 }
 );
 }
 }
 },
 __namespace: true
};

Unit Test


/// <reference path="XrmPageTemplate.js" />
module(
 'Account Form Library Test',
 {
 setup: function () {
 this.savedAjax = $.ajax;
 },
 teardown: function () {
 $.ajax = this.savedAjax;
 }
 }
 );
test("Zzhou.Qunit.Account._changeControlLabel", function () {
 //
 // Arrange
 //
 var attrName = 'name',
 expectedLabel = 'Organisation Name';
 //
 // Act
 //
 Zzhou.Qunit.Account._changeControlLabel(attrName, expectedLabel);

//
 // Assert
 //
 var actualLabel, control = Xrm.Page.getControl(attrName);
 if (control) {
 actualLabel = control.getLabel();
 }
 strictEqual(actualLabel, expectedLabel, "actualLabel and expectedLabel are the same value and type");
});
test("Zzhou.Qunit.Account._updatePreferredUser", function () {
 expect(3);

//
 // Arrange
 //
 var data = {
 d: {
 PreferredSystemUserId: {
 Id: '5bcf7e47-9993-e111-88fa-00155d4c5b01',
 LogicalName: 'systemuser',
 Name: 'First name Last name'
 }
 }
 };
 $.ajax = function (options) {
 options.success(data, "success", { responseText: JSON.stringify(data) });
 };

//
 // Act
 //
 Zzhou.Qunit.Account._updatePreferredUser();

//
 // Assert
 //
 var attrName = 'preferredsystemuserid';
 var attribute = Xrm.Page.getAttribute(attrName);
 if (attribute) {
 var attrValue = attribute.getValue();
 if (attrValue && attrValue[0]) {
 strictEqual(attrValue[0].entityType, data.d.PreferredSystemUserId.LogicalName);
 strictEqual(attrValue[0].id, data.d.PreferredSystemUserId.Id);
 strictEqual(attrValue[0].name, data.d.PreferredSystemUserId.Name);
 }
 }
});


This was originally posted here.

Comments

*This post is locked for comments