From c81be3165e9b069b826bf6b378fda33b89ebf335 Mon Sep 17 00:00:00 2001 From: Cr <631807682@qq.com> Date: Tue, 27 Aug 2019 10:31:33 +0800 Subject: [PATCH] Table: not trigger sort-change event when mounted (#17113) --- packages/table/src/store/index.js | 6 ++-- test/unit/specs/table.spec.js | 53 +++++++++++++++++++------------ 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/packages/table/src/store/index.js b/packages/table/src/store/index.js index 7c55d1f87f..4d78108eaa 100644 --- a/packages/table/src/store/index.js +++ b/packages/table/src/store/index.js @@ -68,13 +68,13 @@ Watcher.prototype.mutations = { }, sort(states, options) { - const { prop, order } = options; + const { prop, order, init } = options; if (prop) { const column = arrayFind(states.columns, column => column.property === prop); if (column) { column.order = order; this.updateSort(column, prop, order); - this.commit('changeSortCondition'); + this.commit('changeSortCondition', { init }); } } }, @@ -89,7 +89,7 @@ Watcher.prototype.mutations = { const ingore = { filter: true }; this.execQuery(ingore); - if (!options || !options.silent) { + if (!options || !(options.silent || options.init)) { this.table.$emit('sort-change', { column, prop, diff --git a/test/unit/specs/table.spec.js b/test/unit/specs/table.spec.js index 6725198284..adaa965ba4 100644 --- a/test/unit/specs/table.spec.js +++ b/test/unit/specs/table.spec.js @@ -555,6 +555,38 @@ describe('Table', () => { done(); }, DELAY); }); + + it('sort-change', async() => { + const vm = createVue({ + template: ` + + + + + + + `, + + created() { + this.testData = getTestData(); + }, + + data() { + return { testData: this.testData }; + } + }); + + const spy = sinon.spy(); + vm.$refs.table.$on('sort-change', spy); + await waitImmediate(); + expect(spy.notCalled).to.be.true;// not emit when mounted + + const elm = vm.$el.querySelector('.caret-wrapper'); + elm.click(); + await waitImmediate(); + expect(spy.calledOnce).to.be.true; + destroyVM(vm); + }); }); describe('column attributes', () => { @@ -1144,27 +1176,6 @@ describe('Table', () => { }, DELAY); }, DELAY); }); - - it('sort-change', done => { - let result; - const vm = createTable('sortable="custom"', '', '', '', { - methods: { - sortChange(...args) { - result = args; - } - } - }, '@sort-change="sortChange"'); - setTimeout(_ => { - const elm = vm.$el.querySelector('.caret-wrapper'); - - elm.click(); - setTimeout(_ => { - expect(result).to.exist; - destroyVM(vm); - done(); - }, DELAY); - }, DELAY); - }); }); describe('click sortable column', () => {