From a21c973e538d330db55b6d832e683312874d6c66 Mon Sep 17 00:00:00 2001 From: schelmo Date: Thu, 25 Jun 2020 19:22:29 +0200 Subject: [PATCH] fix(reduce): do not set $data._value in updateValue if reduce option is set Closes #1210 --- src/components/Select.vue | 3 +-- tests/unit/Reduce.spec.js | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/components/Select.vue b/src/components/Select.vue index 6286c45..2f77325 100644 --- a/src/components/Select.vue +++ b/src/components/Select.vue @@ -713,7 +713,7 @@ * @param value */ updateValue (value) { - if (this.isTrackingValues) { + if (typeof this.value === 'undefined') { // Vue select has to manage value this.$data._value = value; } @@ -989,7 +989,6 @@ */ selectedValue () { let value = this.value; - if (this.isTrackingValues) { // Vue select has to manage value internally value = this.$data._value; diff --git a/tests/unit/Reduce.spec.js b/tests/unit/Reduce.spec.js index d656716..de1a24d 100755 --- a/tests/unit/Reduce.spec.js +++ b/tests/unit/Reduce.spec.js @@ -100,19 +100,36 @@ describe("When reduce prop is defined", () => { expect(Select.vm.selectedValue).toEqual([]); }); - it("can use v-model syntax for a two way binding to a parent component", () => { + it("can use v-model syntax for a two way binding to a parent component", async () => { const Parent = mount({ data: () => ({ reduce: option => option.value, - value: "foo", + current: "foo", options: [ { label: "This is Foo", value: "foo" }, { label: "This is Bar", value: "bar" }, { label: "This is Baz", value: "baz" } ] }), - template: `
`, - components: { "v-select": VueSelect } + components: { "v-select": VueSelect }, + computed: { + value: { + get() { + return this.current; + }, + set(value) { + if (value == 'baz') return; + this.current = value; + } + } + }, + template: ` + + ` }); const Select = Parent.vm.$children[0]; @@ -120,7 +137,15 @@ describe("When reduce prop is defined", () => { expect(Select.selectedValue).toEqual([{ label: "This is Foo", value: "foo" }]); Select.select({ label: "This is Bar", value: "bar" }); + await Select.$nextTick(); expect(Parent.vm.value).toEqual("bar"); + expect(Select.selectedValue).toEqual([{ label: "This is Bar", value: "bar" }]); + + // Parent denies to set baz + Select.select({ label: "This is Baz", value: "baz" }); + await Select.$nextTick(); + expect(Select.selectedValue).toEqual([{ label: "This is Bar", value: "bar" }]); + expect(Parent.vm.value).toEqual('bar'); }); it("can generate labels using a custom label key", () => {