Skip to content

Commit

Permalink
Fix for XrmMockGenerator.Control doesn't expose static methods #9
Browse files Browse the repository at this point in the history
AutoLookupControl.Mock.ts
- Simplified Constructor/Property defintion
- Made uiKeyPressableOptional

UiStandardElement.Mock.ts
- Added factory create method to simplify creation.

Attribute.ts
- Reordered the properties of createString.  This is a breaking change, but it allows for just being able to specify Attribute.createString("lastname"), rather than being forced to populated all of the parameters.  The order of the parameters have been updated to be in the most like order of setting the value.  (i.e. many unit tests may be setting a control to visible, or enabling it.  Very few will care about the format, and even fewer will be worry about the label or setting the maxLength.

control.ts
- The xrm-mock-generator has exposed an instance of Control, making the static methods impossible to call.  Ideally, it makes more sense for the class to be static methods, but the attribute class was setup as an instance, and I elected to leave it.  Not sure if it should be changed or not.

Added test for the control being added with a label
  • Loading branch information
daryllabar committed Apr 3, 2018
1 parent 778dbca commit 1ea0608
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 33 deletions.
3 changes: 2 additions & 1 deletion dist/xrm-mock-generator/attribute.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/// <reference types="xrm" />
import * as XrmMock from "../xrm-mock/index";
export default class Attribute {
private Control;
createBool(name: string, value: boolean): Xrm.Page.BooleanAttribute;
createDate(name: string, value: Date, includeTime: boolean): Xrm.Page.DateAttribute;
createLookup(name: string, lookup: Xrm.Page.LookupValue): Xrm.Page.LookupAttribute;
createNumber(name: string, value: number, min?: number, max?: number, precision?: number): Xrm.Page.NumberAttribute;
createOptionSet(name: string, options: Xrm.Page.OptionSetValue[]): Xrm.Page.OptionSetAttribute;
createOptionSetOption(option: Xrm.Page.OptionSetValue): XrmMock.OptionSetValueMock;
createString(name: string, value: string, label: string, format: Xrm.Page.StringAttributeFormat, maxLength: number, isVisible: boolean, isDisabled: boolean): XrmMock.StringAttributeMock;
createString(name: string, value?: string, isVisible?: boolean, isDisabled?: boolean, format?: Xrm.Page.StringAttributeFormat, maxLength?: number, label?: string): XrmMock.StringAttributeMock;
private createAttribute(name, value);
private addAttribute(attribute);
}
10 changes: 8 additions & 2 deletions dist/xrm-mock-generator/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var XrmMock = require("../xrm-mock/index");
var control_1 = require("./control");
var Attribute = /** @class */ (function () {
function Attribute() {
this.Control = new control_1.default();
}
Attribute.prototype.createBool = function (name, value) {
var attribute = this.createAttribute(name, value || false);
Expand Down Expand Up @@ -48,11 +49,16 @@ var Attribute = /** @class */ (function () {
Attribute.prototype.createOptionSetOption = function (option) {
return new XrmMock.OptionSetValueMock(option.text, option.value);
};
Attribute.prototype.createString = function (name, value, label, format, maxLength, isVisible, isDisabled) {
Attribute.prototype.createString = function (name, value, isVisible, isDisabled, format, maxLength, label) {
if (value === void 0) { value = ""; }
if (isVisible === void 0) { isVisible = true; }
if (isDisabled === void 0) { isDisabled = false; }
if (format === void 0) { format = "text"; }
if (maxLength === void 0) { maxLength = 100; }
var attribute = this.createAttribute(name, value);
var stringAttribute = new XrmMock.StringAttributeMock(attribute, format || "text", maxLength || 100);
this.addAttribute(stringAttribute);
control_1.default.createStringControl(name, label || "", isVisible || true, isDisabled || false, stringAttribute);
this.Control.createString(stringAttribute, name, isVisible, isDisabled, label);
return stringAttribute;
};
Attribute.prototype.createAttribute = function (name, value) {
Expand Down
6 changes: 3 additions & 3 deletions dist/xrm-mock-generator/control.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as XrmMock from "../xrm-mock/index";
export default class Control {
static createStringControl(name: string, label: string, isVisible: boolean, isDisabled: boolean, attribute: XrmMock.StringAttributeMock): void;
private static createControl(name, label, isVisible?, controlType?);
private static addControl(control);
createString(attribute: XrmMock.StringAttributeMock, name?: string, isVisible?: boolean, isDisabled?: boolean, label?: string): void;
private createControl(name, label, isVisible?, controlType?);
private addControl(control);
}
16 changes: 10 additions & 6 deletions dist/xrm-mock-generator/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ var ui_1 = require("./ui");
var Control = /** @class */ (function () {
function Control() {
}
Control.createStringControl = function (name, label, isVisible, isDisabled, attribute) {
Control.prototype.createString = function (attribute, name, isVisible, isDisabled, label) {
if (isVisible === void 0) { isVisible = true; }
if (isDisabled === void 0) { isDisabled = false; }
var stringControl = new XrmMock.StringControlMock(new XrmMock.AutoLookupControlMock(new XrmMock.StandardControlMock({
attribute: attribute,
control: this.createControl(name, label, isVisible, "standard"),
}), new XrmMock.UiKeyPressableMock()));
control: this.createControl(name, label, isVisible),
uiStandardElement: XrmMock.UiStandardElementMock.create(label, isVisible)
})));
this.addControl(stringControl);
};
Control.createControl = function (name, label, isVisible, controlType) {
Control.prototype.createControl = function (name, label, isVisible, controlType) {
if (controlType === void 0) { controlType = "standard"; }
var control = new XrmMock.ControlMock({
controlType: controlType || "standard",
name: name,
uiCanGetVisibleElement: ui_1.default.createCanGetVisibleElement(isVisible || true),
uiLabelElement: ui_1.default.createLabelElement(label),
uiLabelElement: ui_1.default.createLabelElement(label || name),
});
return control;
};
Control.addControl = function (control) {
Control.prototype.addControl = function (control) {
Xrm.Page.ui.controls.push(control);
};
return Control;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UiKeyPressableMock } from "../uikeypressable/uikeypressable.mock";
export declare class AutoLookupControlMock implements Xrm.Page.AutoLookupControl {
standardControl: StandardControlMock;
uiKeyPressable: UiKeyPressableMock;
constructor(standardControl: StandardControlMock, uiKeyPressable: UiKeyPressableMock);
constructor(standardControl: StandardControlMock, uiKeyPressable?: UiKeyPressableMock);
getValue(): string;
hideAutoComplete(): void;
showAutoComplete(resultSet: Xrm.Page.AutoCompleteResultSet): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var uikeypressable_mock_1 = require("../uikeypressable/uikeypressable.mock");
var AutoLookupControlMock = /** @class */ (function () {
function AutoLookupControlMock(standardControl, uiKeyPressable) {
this.standardControl = standardControl;
this.uiKeyPressable = uiKeyPressable;
uiKeyPressable = uiKeyPressable || new uikeypressable_mock_1.UiKeyPressableMock();
}
AutoLookupControlMock.prototype.getValue = function () {
return this.standardControl.attribute.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export declare class UiStandardElementMock implements Xrm.Page.UiStandardElement {
uiLabelElement: Xrm.Page.UiLabelElement;
uiCanGetVisibleElement: Xrm.Page.UiCanGetVisibleElement;
static create(label: string, visible?: boolean): UiStandardElementMock;
constructor(uiLabelElement: Xrm.Page.UiLabelElement, uiCanGetVisibleElement: Xrm.Page.UiCanGetVisibleElement);
setVisible(visible: boolean): void;
getVisible(): boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var UiLabelElement_mock_1 = require("../UiLabelElement/UiLabelElement.mock");
var UiCanGetVisibleElement_mock_1 = require("../UiCanGetVisibleElement/UiCanGetVisibleElement.mock");
var UiStandardElementMock = /** @class */ (function () {
function UiStandardElementMock(uiLabelElement, uiCanGetVisibleElement) {
this.uiLabelElement = uiLabelElement;
this.uiCanGetVisibleElement = uiCanGetVisibleElement;
}
UiStandardElementMock.create = function (label, visible) {
if (visible === void 0) { visible = true; }
return new UiStandardElementMock(new UiLabelElement_mock_1.UiLabelElementMock(label), new UiCanGetVisibleElement_mock_1.UiCanGetVisibleElementMock(visible));
};
UiStandardElementMock.prototype.setVisible = function (visible) {
this.uiCanGetVisibleElement.getVisible = function () { return visible; };
};
Expand Down
8 changes: 5 additions & 3 deletions src/xrm-mock-generator/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as XrmMock from "../xrm-mock/index";
import Control from "./control";

export default class Attribute {
private Control = new Control();

public createBool(name: string, value: boolean): Xrm.Page.BooleanAttribute {
const attribute = this.createAttribute(name, value || false);
const boolAttribute = new XrmMock.BooleanAttributeMock(new XrmMock.EnumAttributeMock(attribute));
Expand Down Expand Up @@ -58,13 +60,13 @@ export default class Attribute {
return new XrmMock.OptionSetValueMock(option.text, option.value);
}

public createString(name: string, value: string, label: string, format: Xrm.Page.StringAttributeFormat,
maxLength: number, isVisible: boolean, isDisabled: boolean) {
public createString(name: string, value: string= "", isVisible: boolean = true, isDisabled: boolean = false,
format: Xrm.Page.StringAttributeFormat = "text", maxLength: number = 100, label?: string) {
const attribute = this.createAttribute(name, value);
const stringAttribute = new XrmMock.StringAttributeMock(attribute, format || "text", maxLength || 100);

this.addAttribute(stringAttribute);
Control.createStringControl(name, label || "", isVisible || true, isDisabled || false, stringAttribute);
this.Control.createString(stringAttribute, name, isVisible, isDisabled, label);

return stringAttribute;
}
Expand Down
18 changes: 8 additions & 10 deletions src/xrm-mock-generator/control.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import * as XrmMock from "../xrm-mock/index";

import Ui from "./ui";

export default class Control {
public static createStringControl(name: string, label: string, isVisible: boolean, isDisabled: boolean,
attribute: XrmMock.StringAttributeMock): void {
public createString(attribute: XrmMock.StringAttributeMock, name?: string, isVisible: boolean = true, isDisabled: boolean = false, label?: string): void {
const stringControl = new XrmMock.StringControlMock(
new XrmMock.AutoLookupControlMock(
new XrmMock.StandardControlMock({
attribute,
control: this.createControl(name, label, isVisible, "standard"),
}),
new XrmMock.UiKeyPressableMock()));
control: this.createControl(name, label, isVisible),
uiStandardElement: XrmMock.UiStandardElementMock.create(label,isVisible)
})));

this.addControl(stringControl);
}
private static createControl(name: string, label: string, isVisible?: boolean,
controlType?: Xrm.Page.ControlType): XrmMock.ControlMock {
private createControl(name: string, label: string, isVisible?: boolean,
controlType: Xrm.Page.ControlType = "standard"): XrmMock.ControlMock {
const control = new XrmMock.ControlMock({
controlType: controlType || "standard",
name,
uiCanGetVisibleElement: Ui.createCanGetVisibleElement(isVisible || true),
uiLabelElement: Ui.createLabelElement(label),
uiLabelElement: Ui.createLabelElement(label || name),
});

return control;
}

private static addControl(control: Xrm.Page.Control): void {
private addControl(control: Xrm.Page.Control): void {
(Xrm.Page.ui.controls as any).push(control);
}
}
8 changes: 2 additions & 6 deletions src/xrm-mock/page/autolookupcontrol/autolookupcontrol.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { StandardControlMock } from "../standardcontrol/standardcontrol.mock";
import { UiKeyPressableMock } from "../uikeypressable/uikeypressable.mock";

export class AutoLookupControlMock implements Xrm.Page.AutoLookupControl {
public standardControl: StandardControlMock;
public uiKeyPressable: UiKeyPressableMock;

constructor(standardControl: StandardControlMock, uiKeyPressable: UiKeyPressableMock) {
this.standardControl = standardControl;
this.uiKeyPressable = uiKeyPressable;
constructor(public standardControl: StandardControlMock, public uiKeyPressable?: UiKeyPressableMock) {
uiKeyPressable = uiKeyPressable || new UiKeyPressableMock();
}

public getValue(): string {
Expand Down
7 changes: 7 additions & 0 deletions src/xrm-mock/page/uistandardelement/uistandardelement.mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { UiLabelElementMock } from "../UiLabelElement/UiLabelElement.mock";
import { UiCanGetVisibleElementMock } from "../UiCanGetVisibleElement/UiCanGetVisibleElement.mock";

export class UiStandardElementMock implements Xrm.Page.UiStandardElement {
public uiLabelElement: Xrm.Page.UiLabelElement;
public uiCanGetVisibleElement: Xrm.Page.UiCanGetVisibleElement;

public static create(label: string, visible: boolean = true) : UiStandardElementMock {
return new UiStandardElementMock(new UiLabelElementMock(label), new UiCanGetVisibleElementMock(visible));
}

constructor(uiLabelElement: Xrm.Page.UiLabelElement, uiCanGetVisibleElement: Xrm.Page.UiCanGetVisibleElement) {
this.uiLabelElement = uiLabelElement;
this.uiCanGetVisibleElement = uiCanGetVisibleElement;
Expand Down
7 changes: 6 additions & 1 deletion test/xrm-mock-generator/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ describe("XrmMockGenerator", () => {
});

it("should create a string attribute", () => {
XrmMockGenerator.Attribute.createString("firstname", "Joe", "First Name", "text", 100, true, false);
XrmMockGenerator.Attribute.createString("firstname", "Joe", true, false, "text", 100, "First Name");
expect(Xrm.Page.getAttribute("firstname").getValue()).toBe("Joe");
});

it("should create a string control with Label", () => {
XrmMockGenerator.Control.createString(undefined, "firstname", true, false, "First Name");
expect(Xrm.Page.getControl("firstname").getLabel()).toBe("First Name");
});
});

0 comments on commit 1ea0608

Please sign in to comment.