Skip to content

Commit e0c0804

Browse files
authored
Update table-sort.spec.js
1 parent 615e5b1 commit e0c0804

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

src/components/table/table-sort.spec.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,117 @@ describe('table sorting', () => {
144144
expect(columnA[1]).toBe('2')
145145
expect(columnA[2]).toBe('3')
146146
})
147+
148+
it('should sort columns when clicking headers', async () => {
149+
const wrapper = mount(Table, {
150+
propsData: {
151+
fields: testFields,
152+
items: testItems
153+
}
154+
})
155+
expect(wrapper).toBeDefined()
156+
expect(wrapper.findAll('tbody > tr').exists()).toBe(true)
157+
expect(wrapper.findAll('tbody > tr').length).toBe(3)
158+
let $rows
159+
let columnA
160+
let columnB
161+
162+
// Should not be sorted
163+
await wrapper.vm.$nextTick()
164+
expect(wrapper.emitted('input')).toBeDefined()
165+
expect(wrapper.emitted('input').length).toBe(1)
166+
$rows = wrapper.findAll('tbody > tr').wrappers
167+
expect($rows.length).toBe(3)
168+
// Map the rows to the first column text value
169+
columnA = $rows.map(row => {
170+
return row
171+
.findAll('td')
172+
.at(0)
173+
.text()
174+
})
175+
expect(columnA[0]).toBe('3')
176+
expect(columnA[1]).toBe('1')
177+
expect(columnA[2]).toBe('2')
178+
179+
// Sort by first column
180+
wrapper
181+
.findAll('thead > tr > td')
182+
.at(0)
183+
.trigger('click')
184+
await wrapper.vm.$nextTick()
185+
expect(wrapper.emitted('input').length).toBe(2)
186+
$rows = wrapper.findAll('tbody > tr').wrappers
187+
expect($rows.length).toBe(3)
188+
// Map the rows to the column text value
189+
columnA = $rows.map(row => {
190+
return row
191+
.findAll('td')
192+
.at(0)
193+
.text()
194+
})
195+
expect(columnA[0]).toBe('1')
196+
expect(columnA[1]).toBe('2')
197+
expect(columnA[2]).toBe('3')
198+
199+
// Click first column header again to reverse sort
200+
wrapper
201+
.findAll('thead > tr > td')
202+
.at(0)
203+
.trigger('click')
204+
await wrapper.vm.$nextTick()
205+
expect(wrapper.emitted('input').length).toBe(3)
206+
$rows = wrapper.findAll('tbody > tr').wrappers
207+
expect($rows.length).toBe(3)
208+
// Map the rows to the column text value
209+
columnA = $rows.map(row => {
210+
return row
211+
.findAll('td')
212+
.at(0)
213+
.text()
214+
})
215+
expect(columnA[0]).toBe('3')
216+
expect(columnA[1]).toBe('2')
217+
expect(columnA[2]).toBe('1')
218+
219+
// Click second column header to sort by it
220+
wrapper
221+
.findAll('thead > tr > td')
222+
.at(1)
223+
.trigger('click')
224+
await wrapper.vm.$nextTick()
225+
expect(wrapper.emitted('input').length).toBe(4)
226+
$rows = wrapper.findAll('tbody > tr').wrappers
227+
expect($rows.length).toBe(3)
228+
// Map the rows to the column text value
229+
columnB = $rows.map(row => {
230+
return row
231+
.findAll('td')
232+
.at(1)
233+
.text()
234+
})
235+
expect(columnA[0]).toBe('a')
236+
expect(columnA[1]).toBe('b')
237+
expect(columnA[2]).toBe('c')
238+
239+
// Click third column header to clear sort
240+
wrapper
241+
.findAll('thead > tr > td')
242+
.at(2)
243+
.trigger('click')
244+
await wrapper.vm.$nextTick()
245+
expect(wrapper.emitted('input').length).toBe(5)
246+
expect(wrapper.emitted('input')[4][0]).toBe(testItems)
247+
$rows = wrapper.findAll('tbody > tr').wrappers
248+
expect($rows.length).toBe(3)
249+
// Map the rows to the column text value
250+
columnA = $rows.map(row => {
251+
return row
252+
.findAll('td')
253+
.at(1)
254+
.text()
255+
})
256+
expect(columnA[0]).toBe('3')
257+
expect(columnA[1]).toBe('1')
258+
expect(columnA[2]).toBe('2')
259+
})
147260
})

0 commit comments

Comments
 (0)