Skip to content

Commit

Permalink
#159 Fix customLabel function from multi-select field to work with ob…
Browse files Browse the repository at this point in the history
…ject values
  • Loading branch information
cristian.jora committed Mar 20, 2017
1 parent 16c256e commit 84d3e41
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/fields/optional/fieldVueMultiSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@
}
},
customLabel(){
if (typeof this.schema.selectOptions !== "undefined" && typeof this.schema.selectOptions.customLabel !== "undefined" && this.schema.selectOptions.customLabel === "function") {
if (typeof this.schema.selectOptions !== "undefined" && typeof this.schema.selectOptions.customLabel !== "undefined" && typeof this.schema.selectOptions.customLabel === "function") {
return this.schema.selectOptions.customLabel;
} else {
return function(currentLabel){return currentLabel;};
//this will let the multiselect library use the default behavior if customLabel is not specified
return undefined;
}
}
},
Expand Down
62 changes: 62 additions & 0 deletions test/unit/specs/fields/fieldVueMultiSelect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,67 @@ describe("fieldVueMultiSelect.vue", function() {
});

});

describe("with objects", () => {
const option = {
name: "Vue.js",
language: "JavaScript"
};
let schema = {...schema};
let model = {
city: [option]
};
schema.values = [
{
name: "Vue.js",
language: "JavaScript"
},
{
name: "Rails",
language: "Ruby"
},
{
name: "Sinatra",
language: "Ruby"
}];
schema.selectOptions = {};
before(() => {
createField(this, schema, model, false);
input = el.querySelector(".multiselect");
});

it("model value should work with objects", (done) => {
schema.selectOptions = {label: "name", trackBy: "name"};
vm.$nextTick(() => {
expect(model.city.length).to.be.equal(1);
expect(model.city[0]).to.be.eql(schema.values[0]);
done();
});
});

it("options should contain only text specified in label", (done) => {
schema.selectOptions = {label: "language", trackBy: "language"};
vm.$nextTick(() => {
let options = input.querySelectorAll("li .multiselect__option");
expect(options[0].querySelector("span").textContent).to.be.equal("JavaScript");
done();
});
});

it("options should contain custom text specified in customLabel", (done) => {
schema.selectOptions = {
label: "name",
trackBy: "name",
customLabel: ({name, language}) => {
return `${name}-${language}`;
}
};
vm.$nextTick(() => {
let options = input.querySelectorAll("li .multiselect__option");
expect(options[0].querySelector("span").textContent).to.be.equal("Vue.js-JavaScript");
done();
});
});
});
});
});

0 comments on commit 84d3e41

Please sign in to comment.