Skip to content

Commit c8877d0

Browse files
author
perploug
committed
Fixes failing dropdown prop editor tests
1 parent 891810b commit c8877d0

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/Umbraco.Web.UI.Client/src/views/propertyeditors/dropdown/dropdown.controller.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,53 @@ angular.module("umbraco").controller("Umbraco.PropertyEditors.DropdownController
99

1010
//map the user config
1111
angular.extend(config, $scope.model.config);
12+
1213
//map back to the model
1314
$scope.model.config = config;
1415

15-
if (angular.isArray($scope.model.config.items)) {
16-
17-
//ensure the items are sorted by the provided sort order
18-
$scope.model.config.items.sort(function (a, b) { return (a.sortOrder > b.sortOrder) ? 1 : ((b.sortOrder > a.sortOrder) ? -1 : 0); });
16+
function convertArrayToDictionaryArray(model){
17+
//now we need to format the items in the dictionary because we always want to have an array
18+
var newItems = [];
19+
for (var i = 0; i < model.length; i++) {
20+
newItems.push({ id: model[i], sortOrder: 0, value: model[i] });
21+
}
1922

23+
return newItems;
2024
}
21-
else if (angular.isObject($scope.model.config.items)) {
2225

26+
27+
function convertObjectToDictionaryArray(model){
2328
//now we need to format the items in the dictionary because we always want to have an array
2429
var newItems = [];
2530
var vals = _.values($scope.model.config.items);
2631
var keys = _.keys($scope.model.config.items);
32+
2733
for (var i = 0; i < vals.length; i++) {
2834
var label = vals[i].value ? vals[i].value : vals[i];
2935
newItems.push({ id: keys[i], sortOrder: vals[i].sortOrder, value: label });
3036
}
3137

32-
//ensure the items are sorted by the provided sort order
33-
newItems.sort(function (a, b) { return (a.sortOrder > b.sortOrder) ? 1 : ((b.sortOrder > a.sortOrder) ? -1 : 0); });
38+
return newItems;
39+
}
3440

35-
//re-assign
36-
$scope.model.config.items = newItems;
41+
if (angular.isArray($scope.model.config.items)) {
42+
//PP: I dont think this will happen, but we have tests that expect it to happen..
43+
//if array is simple values, convert to array of objects
44+
if(!angular.isObject($scope.model.config.items[0])){
45+
$scope.model.config.items = convertArrayToDictionaryArray($scope.model.config.items);
46+
}
47+
}
48+
else if (angular.isObject($scope.model.config.items)) {
49+
$scope.model.config.items = convertObjectToDictionaryArray($scope.model.config.items);
3750
}
3851
else {
3952
throw "The items property must be either an array or a dictionary";
4053
}
4154

55+
56+
//sort the values
57+
$scope.model.config.items.sort(function (a, b) { return (a.sortOrder > b.sortOrder) ? 1 : ((b.sortOrder > a.sortOrder) ? -1 : 0); });
58+
4259
//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
4360
// to "" gets selected by default
4461
if ($scope.model.value === null || $scope.model.value === undefined) {

src/Umbraco.Web.UI.Client/test/unit/app/propertyeditors/dropdown-controller.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Drop down controller tests', function () {
2323
expect(scope.model.config.items).toBeDefined();
2424
expect(scope.model.config.multiple).toBeDefined();
2525
});
26-
26+
2727
it("should convert simple array to dictionary", function () {
2828

2929
scope.model = {
@@ -37,12 +37,13 @@ describe('Drop down controller tests', function () {
3737
$routeParams: routeParams
3838
});
3939

40-
expect(scope.model.config.items["value0"]).toBe("value0");
41-
expect(scope.model.config.items["value1"]).toBe("value1");
42-
expect(scope.model.config.items["value2"]).toBe("value2");
43-
40+
//this should be the expected format based on the changes made to the sortable prevalues
41+
expect(scope.model.config.items[0].value).toBe("value0");
42+
expect(scope.model.config.items[1].value).toBe("value1");
43+
expect(scope.model.config.items[2].value).toBe("value2");
4444
});
4545

46+
4647
it("should allow an existing valid dictionary", function () {
4748

4849
scope.model = {

0 commit comments

Comments
 (0)