2
0
mirror of https://github.com/tenrok/vue-select.git synced 2026-06-22 10:30:34 +03:00

more tests

This commit is contained in:
Jeff Sagal
2016-03-08 14:10:43 -08:00
parent 2a5616ee3a
commit d182caa6b1
4 changed files with 97 additions and 72 deletions
+28 -24
View File
File diff suppressed because one or more lines are too long
+12 -19
View File
@@ -66,23 +66,16 @@
<hr> <hr>
<h3>Build Setup</h3> <h3>Build Setup</h3>
<h5>install dependencies</h5>
<p><code>npm install</code></p>
<h5>install dependencies</h5> <h5>serve with hot reload at localhost:8080</h5>
<p><code>npm install</code></p> <p><code>npm run dev</code></p>
<h5>build for production with minification</h5>
<h5>serve with hot reload at localhost:8080</h5> <p><code>npm run build</code></p>
<p><code>npm run dev</code></p> <h5>lint all *.js and *.vue files</h5>
<p><code>npm run lint</code></p>
<h5>build for production with minification</h5> <h5>run unit tests</h5>
<p><code>npm run build</code></p> <p><code>npm test</code></p>
<h5>lint all *.js and *.vue files</h5>
<p><code>npm run lint</code></p>
<h5>run unit tests</h5>
<p><code>npm test</code></p>
</div> </div>
</template> </template>
@@ -99,9 +92,9 @@ export default {
data() { data() {
return { return {
// select: [{label: 'This is Foo', value: 'foo'}, {label: 'This is Bar', value: 'bar'}], // select: [{label: 'This is Foo', value: 'foo'}, {label: 'This is Bar', value: 'bar'}],
select: ['one'], select: null,
placeholder: 'Choose a Country', placeholder: 'Choose a Country',
multiple: true, multiple: false,
maxHeight: '400px', maxHeight: '400px',
// options: require('./countries.js') // options: require('./countries.js')
options: ['one','two','three'] options: ['one','two','three']
+26 -21
View File
@@ -113,7 +113,7 @@
<template> <template>
<div class="dropdown" :class="{open: open, }"> <div class="dropdown" :class="{open: open, }">
<div v-el:toggle @click="toggleDropdown" class="btn dropdown-toggle clearfix" type="button"> <div v-el:toggle @click="toggleDropdown" class="btn dropdown-toggle clearfix" type="button">
<span class="form-control" v-if="! searchable && placeholder && ! value.length"> <span class="form-control" v-if="! searchable && placeholder && isValueEmpty">
{{ placeholder }} {{ placeholder }}
</span> </span>
@@ -137,13 +137,12 @@
@blur="open = false" @blur="open = false"
type="search" type="search"
class="form-control" class="form-control"
:placeholder="searchPlaceholder" :placeholder="searchPlaceholder"
> >
</div> </div>
<ul v-show="open" :transition="transition" :style="{ 'max-height': maxHeight }" class="dropdown-menu animated"> <ul v-show="open" :transition="transition" :style="{ 'max-height': maxHeight }" class="dropdown-menu animated">
<li v-for="option in filteredOptions" :class="{ active: value.indexOf(option) !== -1, highlight: $index === typeAheadPointer }"> <li v-for="option in filteredOptions" :class="{ active: isOptionSelected(option), highlight: $index === typeAheadPointer }">
<a @mousedown.prevent="select(option)"> <a @mousedown.prevent="select(option)">
{{ getOptionLabel(option) }} {{ getOptionLabel(option) }}
</a> </a>
@@ -160,7 +159,7 @@
props: { props: {
value: { value: {
type: Array, // type: Array,
twoway: true, twoway: true,
required: true required: true
}, },
@@ -213,6 +212,12 @@
}); });
}, },
watch: {
value (val, oldVal) {
// return this.$set('value', ['test'])
}
},
methods: { methods: {
select(v) { select(v) {
if (this.value.indexOf(v) === -1) { if (this.value.indexOf(v) === -1) {
@@ -238,6 +243,14 @@
// } // }
}, },
isOptionSelected( option ) {
if( this.multiple ) {
return this.value.indexOf(option) !== -1
}
return this.value === option;
},
getOptionValue( option ) { getOptionValue( option ) {
if( typeof option === 'object' && option.value ) { if( typeof option === 'object' && option.value ) {
return option.value; return option.value;
@@ -285,30 +298,22 @@
}, },
computed: { computed: {
// selected() {
// let foundItems = []
// if (this.value && this.value.length) {
// for (let item in this.value) {
// if (typeof this.value[item] === "string") {
// foundItems.push(this.value[item])
// }
//
// console.log(this.value[item])
// // this.$log('value')
// }
// }
//
// return foundItems
// },
searchPlaceholder() { searchPlaceholder() {
if( ! this.value.length && this.placeholder ) { if( this.isValueEmpty && this.placeholder ) {
return this.placeholder; return this.placeholder;
} }
}, },
filteredOptions() { filteredOptions() {
return this.$options.filters.filterBy(this.options, this.search) return this.$options.filters.filterBy(this.options, this.search)
},
isValueEmpty() {
if( this.value ) {
return ! this.value.length
}
return true;
} }
} }
+31 -8
View File
@@ -17,21 +17,44 @@ describe('Select.vue', () => {
expect(vm.$children[0].value).toEqual(['one']) expect(vm.$children[0].value).toEqual(['one'])
}) })
/** /**
* TODO: Right now this only works for arrays of strings.. But the same method * TODO: Right now this only works for arrays of strings.. But the same method
* should apply to arrays of objects. * should apply to arrays of objects.
*/ */
it('can accept an array of objects and pre-selected values', () => { // it('can accept an array of objects and pre-selected values', () => {
// const vm = new Vue({
// template: '<div><v-select :value.sync="value"></v-select></div>',
// components: { vSelect },
// data: {
// value: [{label: 'This is Foo', value: 'foo'}],
// options: [{label: 'This is Foo', value: 'foo'}, {label: 'This is Bar', value: 'bar'}]
// }
// }).$mount()
//
// expect(vm.$children[0].$get('value')).toEqual({label: 'This is Foo', value: 'foo'})
// })
it('can determine if the value prop is empty', () => {
const vm = new Vue({ const vm = new Vue({
template: '<div><v-select :value.sync="value"></v-select></div>', template: '<div><v-select :value.sync="value"></v-select></div>',
components: { vSelect }, components: { vSelect },
data: { data: {
value: [{label: 'This is Foo', value: 'foo'}], value: [],
options: [{label: 'This is Foo', value: 'foo'}, {label: 'This is Bar', value: 'bar'}] options: ['one','two','three']
} }
}).$mount() }).$mount()
expect(vm.$children[0].$get('value')).toEqual({label: 'This is Foo', value: 'foo'}) var select = vm.$children[0]
expect(select.isValueEmpty).toEqual(true)
select.$set('value', ['one'])
expect(select.isValueEmpty).toEqual(false)
select.$set('value', 'one')
select.$set('multiple', false)
expect(select.isValueEmpty).toEqual(false)
select.$set('value', '')
expect(select.isValueEmpty).toEqual(true)
select.$set('value', null)
expect(select.isValueEmpty).toEqual(true)
}) })
}) })