Skip to content

Commit da3edbc

Browse files
committed
Ensure original options list is not mutated
1 parent c21bfc5 commit da3edbc

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# install as dev by default
22
save-dev = true
3+
registry=https://registry.npmjs.org

src/components/AdvancedSelect.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ export default {
377377
f.push({
378378
header: o.label,
379379
});
380-
f.push(...o.options.map(opt => Object.assign(opt, { parentHeader: o.label, selected: this.valueIsSelected(o.value) })));
380+
f.push(...o.options.map(opt => Object.assign({}, opt, { parentHeader: o.label, selected: this.valueIsSelected(o.value) })));
381381
} else {
382382
// it's an item without group, push it to the list
383-
f.push(Object.assign(o, { selected: this.valueIsSelected(o.value) }));
383+
f.push(Object.assign({}, o, { selected: this.valueIsSelected(o.value) }));
384384
}
385385
return f;
386386
}, []);
@@ -501,7 +501,7 @@ export default {
501501
selectAll() {
502502
// when selecting all, concatenate the exiting selected values
503503
// with the currently filtered ones
504-
this.myValue = [... new Set([].concat(
504+
this.myValue = [...new Set([].concat(
505505
this.myValue || [],
506506
this.filtered.filter(o => !o.header && !o.disabled).map(o => o.value)
507507
))];

tests/unit/AdvancedSelect.spec.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ describe('AdvancedSelect.vue', () => {
145145
links.at(1).trigger('click');
146146
expect(wrapper.vm.myValue).to.equal(2);
147147
});
148+
it('Original options list is not mutated', () => {
149+
const options = [{ text: '1', value: 1 }, { text: '2', value: 2 }];
150+
const wrapper = shallowMount(Select, {
151+
propsData: {
152+
options,
153+
},
154+
});
155+
const links = wrapper.findAll('div.btn-group > ul > li > ul > li > a');
156+
links.at(0).trigger('click');
157+
links.at(1).trigger('click');
158+
expect(options).to.deep.eq([{ text: '1', value: 1 }, { text: '2', value: 2 }]);
159+
});
148160
it('Value is set on grouped items', () => {
149161
const wrapper = shallowMount(Select, {
150162
propsData: {
@@ -318,7 +330,7 @@ describe('AdvancedSelect.vue', () => {
318330
});
319331
expect(wrapper.vm.filtered).to.deep.equal([
320332
{ text: 'Option 1', value: 1, subtext: 'Option 1 subtext', selected: false },
321-
{ text: 'Option 2', value: 2, selected: false},
333+
{ text: 'Option 2', value: 2, selected: false },
322334
]);
323335
expect(wrapper.findAll('div.btn-group > ul > li > ul > li > a')).to.have.lengthOf(2);
324336
wrapper.setData({

0 commit comments

Comments
 (0)