[Forms] Fill in specs

Fill in specs for scripts which support the mct-form
and mct-control directives. WTD-530.
This commit is contained in:
Victor Woeltjen 2014-11-28 15:33:40 -08:00
parent 29c5a7aaba
commit 1bfc21270b
5 changed files with 161 additions and 5 deletions

View File

@ -8,7 +8,8 @@ define(
function () {
"use strict";
var MATCH_ALL = /^.*$/;
// Default ng-pattern; any non whitespace
var NON_WHITESPACE = /\S/;
/**
* The mct-form directive allows generation of displayable
@ -37,8 +38,7 @@ define(
].join("/");
function controller($scope) {
var regexps = [],
matchAll = /.*/;
var regexps = [];
// ng-pattern seems to want a RegExp, and not a
// string (despite what documentation says) but
@ -47,7 +47,7 @@ define(
function getRegExp(pattern) {
// If undefined, don't apply a pattern
if (!pattern) {
return MATCH_ALL;
return NON_WHITESPACE;
}
// Just echo if it's already a regexp

View File

@ -1,8 +1,9 @@
/*global define*/
/*global define,moment*/
define(
["../../lib/moment.min"],
function () {
"use strict";
var DATE_FORMAT = "YYYY-DDD";

View File

@ -6,6 +6,54 @@ define(
"use strict";
describe("The mct-control directive", function () {
var testControls,
mockScope,
mctControl;
beforeEach(function () {
testControls = [
{
key: "abc",
bundle: { path: "a", resources: "b" },
templateUrl: "c/template.html"
},
{
key: "xyz",
bundle: { path: "x", resources: "y" },
templateUrl: "z/template.html"
}
];
mockScope = jasmine.createSpyObj("$scope", [ "$watch" ]);
mctControl = new MCTControl(testControls);
});
it("is restricted to the element level", function () {
expect(mctControl.restrict).toEqual("E");
});
it("watches its passed key to choose a template", function () {
mctControl.controller(mockScope);
expect(mockScope.$watch).toHaveBeenCalledWith(
"key",
jasmine.any(Function)
);
});
it("changes its template dynamically", function () {
mctControl.controller(mockScope);
mockScope.key = "xyz";
mockScope.$watch.mostRecentCall.args[1]("xyz");
// Should have communicated the template path to
// ng-include via the "inclusion" field in scope
expect(mockScope.inclusion).toEqual(
"x/y/z/template.html"
);
});
});
}

View File

@ -6,6 +6,84 @@ define(
"use strict";
describe("The mct-form directive", function () {
var mockScope,
mctForm;
beforeEach(function () {
mockScope = jasmine.createSpyObj("$scope", [ "$watch" ]);
mockScope.$parent = {};
mctForm = new MCTForm();
});
it("is restricted to elements", function () {
expect(mctForm.restrict).toEqual("E");
});
it("watches for changes in form by name", function () {
// mct-form needs to watch for the form by name
// in order to convey changes in $valid, $dirty, etc
// up to the parent scope.
mctForm.controller(mockScope);
expect(mockScope.$watch).toHaveBeenCalledWith(
"mctForm",
jasmine.any(Function)
);
});
it("conveys form status to parent scope", function () {
var someState = { someKey: "some value" };
mockScope.name = "someName";
mctForm.controller(mockScope);
mockScope.$watch.mostRecentCall.args[1](someState);
expect(mockScope.$parent.someName).toBe(someState);
});
it("allows strings to be converted to RegExps", function () {
// This is needed to support ng-pattern in the template
mctForm.controller(mockScope);
// Should have added getRegExp to the scope,
// to convert strings to regular expressions
expect(mockScope.getRegExp("^\\d+$")).toEqual(/^\d+$/);
});
it("returns the same regexp instance for the same string", function () {
// Don't want new instances each digest cycle, for performance
var strRegExp = "^[a-z]\\d+$",
regExp;
// Add getRegExp to scope
mctForm.controller(mockScope);
regExp = mockScope.getRegExp(strRegExp);
// Same object instance each time...
expect(mockScope.getRegExp(strRegExp)).toBe(regExp);
expect(mockScope.getRegExp(strRegExp)).toBe(regExp);
});
it("passes RegExp objects through untouched", function () {
// Permit using forms to simply provide their own RegExp object
var regExp = /^\d+[a-d]$/;
// Add getRegExp to scope
mctForm.controller(mockScope);
// Should have added getRegExp to the scope,
// to convert strings to regular expressions
expect(mockScope.getRegExp(regExp)).toBe(regExp);
});
it("passes a non-whitespace regexp when no pattern is defined", function () {
// If no pattern is supplied, ng-pattern should match anything
mctForm.controller(mockScope);
expect(mockScope.getRegExp()).toEqual(/\S/);
expect(mockScope.getRegExp(undefined)).toEqual(/\S/);
});
});
}

View File

@ -6,6 +6,35 @@ define(
"use strict";
describe("The date-time directive", function () {
var mockScope,
controller;
beforeEach(function () {
mockScope = jasmine.createSpyObj("$scope", [ "$watch" ]);
controller = new DateTimeController(mockScope);
});
it("watches for changes in fields", function () {
["date", "hour", "min", "sec"].forEach(function (fieldName) {
expect(mockScope.$watch).toHaveBeenCalledWith(
"datetime." + fieldName,
jasmine.any(Function)
);
});
});
it("converts date-time input into a timestamp", function () {
mockScope.ngModel = {};
mockScope.field = "test";
mockScope.datetime.date = "2014-332";
mockScope.datetime.hour = 22;
mockScope.datetime.min = 55;
mockScope.datetime.sec = 13;
mockScope.$watch.mostRecentCall.args[1]();
expect(mockScope.ngModel.test).toEqual(1417215313000);
});
});
}