Skip to content

Tutorial 4

PhuocLe edited this page Aug 26, 2018 · 12 revisions

Task

  • Create Javascript Unit Test WebResource

Prerequisites

Coding

  1. Add New Item 09. JavaScript Test to test folder of Paz.LuckeyMonkey.WebResource project
    • A popup form Add new Js Test Class opened
    • Click button >< to create/select a Dynamics 365 connection
    • After connected PL.DynamicsCrm.DevKit loaded all entities and bind to dropdown Entity
    • Select Lead in the Entity
    • Click OK
  2. PL.DynamicsCrm.DevKit created Lead.test.js file under folder test
  3. Rebuild solution to restore NuGet packages
  4. Make sure you have node_modules in the project Paz.LuckeyMonkey.WebResource, if NOT you should run npm install in the Command Windows Promt of the project folder Paz.LuckeyMonkey.WebResource
  5. Back to the Lead.js file and edit the return value of variable formLead like bellow
	return {
		OnLoad: onLoad,
		OnSave: onSave,
		__SubjectAddOnChange__: SubjectAddOnChange,
		__PhoneAddOnChange__: PhoneAddOnChange
	};
  1. Edit the Lead.test.js
///<reference path="../entities/Lead.intellisense.js" />
///<reference path="mock.intellisense.js" />
define(['xrm-mock', 'sinon'], function (xrm_mock, sinon) {
    var mock = new DevKitXrmMock(xrm_mock, "lead");
    var server = new DevKitServerMock(sinon);
    describe('Lead.js Test', function () {
        it("OnLoad - FormCreate", function () {
            //setup
            mock.FormType = OptionSet.FormType.Create;
            mock.Field("subject", "hello");
            mock.Field("telephone1", "123");
            mock.Field("mobilephone", "456");

            //execute
            formLead.OnLoad();
            formLead.__SubjectAddOnChange__();
            formLead.__PhoneAddOnChange__();

            //result
            var form = new LuckeyMonkey.FormLead();
            expect(form.Body.Subject.Disabled).toBe(false);
            expect(form.Body.Subject.Value).toBe("HELLO");
            expect(form.Body.Telephone1.RequiredLevel).toBe(OptionSet.FieldRequiredLevel.Required);
            expect(form.Body.MobilePhone.RequiredLevel).toBe(OptionSet.FieldRequiredLevel.Required);

            //setup again
            form.Body.Telephone1.Value = null;
            form.Body.MobilePhone.Value = null;
            //run again
            formLead.__PhoneAddOnChange__();
            //result again
            expect(form.Body.Telephone1.RequiredLevel).toBe(OptionSet.FieldRequiredLevel.None);
            expect(form.Body.MobilePhone.RequiredLevel).toBe(OptionSet.FieldRequiredLevel.None);
        });
        it("OnLoad - FormEdit", function () {
            //setup
            mock.Field("subject", "hello");
            mock.FormType = OptionSet.FormType.Update;

            //execute
            formLead.OnLoad();

            //result
            var form = new LuckeyMonkey.FormLead();
            expect(form.Body.Subject.Disabled).toBe(true);
        });        
    });
});
  1. Please DON'T copy/paste code. Try typing to get intellisense that PL.DynamicsCrm.DevKit support
  2. Right-click on code window Lead.test.js and select Run JS Tests menu of the Chutzpah Extensions and you get the result below
------ Test started: File: C:\src\github\Dynamics-Crm-DevKit\tutorials\Paz.LuckeyMonkey.WebResource\test\Lead.test.js ------
2 passed, 0 failed, 2 total (chutzpah).

========== Total Tests: 2 passed, 0 failed, 2 total ==========
  1. You can run view the code coverage by select menu Run Chutzpah With > Coverage
  2. Check-in all files to your source control
  3. You finished this tutorial

Summary

This tutorial, you know howto

Clone this wiki locally