Fixes failing dropdown prop editor tests

This commit is contained in:
perploug
2014-04-02 11:35:23 +02:00
parent 891810b045
commit c8877d0a2c
2 changed files with 32 additions and 14 deletions

View File

@@ -9,36 +9,53 @@ angular.module("umbraco").controller("Umbraco.PropertyEditors.DropdownController
//map the user config
angular.extend(config, $scope.model.config);
//map back to the model
$scope.model.config = config;
if (angular.isArray($scope.model.config.items)) {
//ensure the items are sorted by the provided sort order
$scope.model.config.items.sort(function (a, b) { return (a.sortOrder > b.sortOrder) ? 1 : ((b.sortOrder > a.sortOrder) ? -1 : 0); });
function convertArrayToDictionaryArray(model){
//now we need to format the items in the dictionary because we always want to have an array
var newItems = [];
for (var i = 0; i < model.length; i++) {
newItems.push({ id: model[i], sortOrder: 0, value: model[i] });
}
return newItems;
}
else if (angular.isObject($scope.model.config.items)) {
function convertObjectToDictionaryArray(model){
//now we need to format the items in the dictionary because we always want to have an array
var newItems = [];
var vals = _.values($scope.model.config.items);
var keys = _.keys($scope.model.config.items);
for (var i = 0; i < vals.length; i++) {
var label = vals[i].value ? vals[i].value : vals[i];
newItems.push({ id: keys[i], sortOrder: vals[i].sortOrder, value: label });
}
//ensure the items are sorted by the provided sort order
newItems.sort(function (a, b) { return (a.sortOrder > b.sortOrder) ? 1 : ((b.sortOrder > a.sortOrder) ? -1 : 0); });
return newItems;
}
//re-assign
$scope.model.config.items = newItems;
if (angular.isArray($scope.model.config.items)) {
//PP: I dont think this will happen, but we have tests that expect it to happen..
//if array is simple values, convert to array of objects
if(!angular.isObject($scope.model.config.items[0])){
$scope.model.config.items = convertArrayToDictionaryArray($scope.model.config.items);
}
}
else if (angular.isObject($scope.model.config.items)) {
$scope.model.config.items = convertObjectToDictionaryArray($scope.model.config.items);
}
else {
throw "The items property must be either an array or a dictionary";
}
//sort the values
$scope.model.config.items.sort(function (a, b) { return (a.sortOrder > b.sortOrder) ? 1 : ((b.sortOrder > a.sortOrder) ? -1 : 0); });
//now we need to check if the value is null/undefined, if it is we need to set it to "" so that any value that is set
// to "" gets selected by default
if ($scope.model.value === null || $scope.model.value === undefined) {

View File

@@ -23,7 +23,7 @@ describe('Drop down controller tests', function () {
expect(scope.model.config.items).toBeDefined();
expect(scope.model.config.multiple).toBeDefined();
});
it("should convert simple array to dictionary", function () {
scope.model = {
@@ -37,12 +37,13 @@ describe('Drop down controller tests', function () {
$routeParams: routeParams
});
expect(scope.model.config.items["value0"]).toBe("value0");
expect(scope.model.config.items["value1"]).toBe("value1");
expect(scope.model.config.items["value2"]).toBe("value2");
//this should be the expected format based on the changes made to the sortable prevalues
expect(scope.model.config.items[0].value).toBe("value0");
expect(scope.model.config.items[1].value).toBe("value1");
expect(scope.model.config.items[2].value).toBe("value2");
});
it("should allow an existing valid dictionary", function () {
scope.model = {