瀏覽代碼

add object-typed value test

Leopoldthecoder 8 年之前
父節點
當前提交
daa4f83e4f
共有 3 個文件被更改,包括 49 次插入3 次删除
  1. 1 1
      packages/select/src/option.vue
  2. 2 2
      packages/select/src/select.vue
  3. 46 0
      test/unit/specs/select.spec.js

+ 1 - 1
packages/select/src/option.vue

@@ -50,7 +50,7 @@
     computed: {
       isObject() {
         const type = typeof this.value;
-        return type !== 'string' && type !== 'number';
+        return type !== 'string' && type !== 'number' && type !== 'boolean';
       },
 
       currentLabel() {

+ 2 - 2
packages/select/src/select.vue

@@ -366,7 +366,7 @@
       getOption(value) {
         let option;
         const type = typeof value;
-        const isObject = type !== 'string' && type !== 'number';
+        const isObject = type !== 'string' && type !== 'number' && type !== 'boolean';
         for (let i = this.cachedOptions.length - 1; i >= 0; i--) {
           const cachedOption = this.cachedOptions[i];
           const isEqual = isObject
@@ -529,7 +529,7 @@
 
       getValueIndex(arr = [], value) {
         const type = typeof value;
-        const isObject = type !== 'string' && type !== 'number';
+        const isObject = type !== 'string' && type !== 'number' && type !== 'boolean';
         if (!isObject) {
           return arr.indexOf(value);
         } else {

+ 46 - 0
test/unit/specs/select.spec.js

@@ -113,6 +113,7 @@ describe('Select', () => {
             <el-option
               v-for="item in options"
               :label="item.label"
+              :key="item.value"
               :value="item.value">
             </el-option>
           </el-select>
@@ -147,6 +148,7 @@ describe('Select', () => {
             <el-option
               v-for="item in options"
               :label="item.label"
+              :key="item.value"
               :value="item.value">
               <p>{{item.label}} {{item.value}}</p>
             </el-option>
@@ -226,6 +228,7 @@ describe('Select', () => {
             <el-option
               v-for="item in options"
               :label="item.label"
+              :key="item.value"
               :value="item.value">
             </el-option>
           </el-select>
@@ -285,6 +288,44 @@ describe('Select', () => {
     }, 100);
   });
 
+  it('object typed value', done => {
+    vm = createVue({
+      template: `
+        <div>
+          <el-select v-model="value" value-key="id">
+            <el-option
+              v-for="item in options"
+              :label="item.label"
+              :key="item.id"
+              :value="item">
+            </el-option>
+          </el-select>
+        </div>
+      `,
+
+      data() {
+        return {
+          options: [{
+            id: 1,
+            label: 'label1'
+          }, {
+            id: 2,
+            label: 'label2'
+          }],
+          value: {
+            id: 1,
+            label: 'label1'
+          }
+        };
+      }
+    }, true);
+    setTimeout(() => {
+      expect(vm.$el.querySelector('.el-input__inner').value).to.equal('label1');
+      expect(vm.$el.querySelector('.el-select-dropdown__item').classList.contains('selected'));
+      done();
+    }, 100);
+  });
+
   it('custom el-option template', () => {
     vm = createVue({
       template: `
@@ -293,6 +334,7 @@ describe('Select', () => {
             <el-option
               v-for="item in options"
               :label="item.label"
+              :key="item.value"
               :value="item.value">
               <p>{{item.label}} {{item.value}}</p>
             </el-option>
@@ -320,11 +362,13 @@ describe('Select', () => {
           <el-select v-model="value">
             <el-option-group
               v-for="group in options"
+              :key="group.label"
               :disabled="group.disabled"
               :label="group.label">
               <el-option
                 v-for="item in group.options"
                 :label="item.label"
+                :key="item.value"
                 :value="item.value">
               </el-option>
             </el-option-group>
@@ -416,6 +460,7 @@ describe('Select', () => {
             <el-option
               v-for="item in options"
               :label="item"
+              :key="item.value"
               :value="item"
             />
           </el-select>
@@ -504,6 +549,7 @@ describe('Select', () => {
             <el-option
               v-for="item in options"
               :label="item.label"
+              :key="item.value"
               :value="item.value">
               <p>{{item.label}} {{item.value}}</p>
             </el-option>