mirror of
https://github.com/nasa/openmct.git
synced 2025-05-10 12:32:53 +00:00
[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:
parent
29c5a7aaba
commit
1bfc21270b
@ -8,7 +8,8 @@ define(
|
|||||||
function () {
|
function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var MATCH_ALL = /^.*$/;
|
// Default ng-pattern; any non whitespace
|
||||||
|
var NON_WHITESPACE = /\S/;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The mct-form directive allows generation of displayable
|
* The mct-form directive allows generation of displayable
|
||||||
@ -37,8 +38,7 @@ define(
|
|||||||
].join("/");
|
].join("/");
|
||||||
|
|
||||||
function controller($scope) {
|
function controller($scope) {
|
||||||
var regexps = [],
|
var regexps = [];
|
||||||
matchAll = /.*/;
|
|
||||||
|
|
||||||
// ng-pattern seems to want a RegExp, and not a
|
// ng-pattern seems to want a RegExp, and not a
|
||||||
// string (despite what documentation says) but
|
// string (despite what documentation says) but
|
||||||
@ -47,7 +47,7 @@ define(
|
|||||||
function getRegExp(pattern) {
|
function getRegExp(pattern) {
|
||||||
// If undefined, don't apply a pattern
|
// If undefined, don't apply a pattern
|
||||||
if (!pattern) {
|
if (!pattern) {
|
||||||
return MATCH_ALL;
|
return NON_WHITESPACE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Just echo if it's already a regexp
|
// Just echo if it's already a regexp
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
/*global define*/
|
/*global define,moment*/
|
||||||
|
|
||||||
define(
|
define(
|
||||||
["../../lib/moment.min"],
|
["../../lib/moment.min"],
|
||||||
function () {
|
function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
var DATE_FORMAT = "YYYY-DDD";
|
var DATE_FORMAT = "YYYY-DDD";
|
||||||
|
|
||||||
|
@ -6,6 +6,54 @@ define(
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe("The mct-control directive", function () {
|
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"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,84 @@ define(
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe("The mct-form directive", function () {
|
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/);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,35 @@ define(
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
describe("The date-time directive", function () {
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user