Browse Source

fix(shared): check dates in looseEqual (#7940)

Fix #7928
thanks to @w3cj for the initial version. This one is using getTime instead of toUTCString because it
is much faster to compare
Eduardo San Martin Morote 7 years ago
parent
commit
db7287c23b
2 changed files with 29 additions and 0 deletions
  1. 2 0
      src/shared/util.js
  2. 27 0
      test/unit/features/directives/model-select.spec.js

+ 2 - 0
src/shared/util.js

@@ -286,6 +286,8 @@ export function looseEqual (a: any, b: any): boolean {
         return a.length === b.length && a.every((e, i) => {
           return looseEqual(e, b[i])
         })
+      } else if (a instanceof Date && b instanceof Date) {
+        return a.getTime() === b.getTime()
       } else if (!isArrayA && !isArrayB) {
         const keysA = Object.keys(a)
         const keysB = Object.keys(b)

+ 27 - 0
test/unit/features/directives/model-select.spec.js

@@ -588,4 +588,31 @@ describe('Directive v-model select', () => {
       }).then(done)
     })
   })
+
+  // #7928
+  it('should correctly handle option with date value', done => {
+    const vm = new Vue({
+      data: {
+        dates: [
+          new Date(1520000000000),
+          new Date(1522000000000),
+          new Date(1516000000000)
+        ],
+        selectedDate: null
+      },
+      template:
+        '<div>' +
+          '<select v-model="selectedDate">' +
+            '<option v-for="(date, i) in dates" :key="i" :value="date">' +
+              '{{date}}' +
+            '</option>' +
+          '</select>' +
+        '</div>'
+    }).$mount()
+
+    vm.selectedDate = vm.dates[2]
+    waitForUpdate(() => {
+      expect(vm.$el.firstChild.selectedIndex).toBe(2)
+    }).then(done)
+  })
 })