@@ -23,28 +23,76 @@ describe('table sorting', () => {
23
23
expect ( wrapper . emitted ( 'input' ) ) . toBeDefined ( )
24
24
expect ( wrapper . emitted ( 'input' ) . length ) . toBe ( 1 )
25
25
expect ( wrapper . emitted ( 'input' ) [ 0 ] [ 0 ] ) . toEqual ( testItems )
26
- const $rows = wrapper . findAll ( 'tbody > tr' )
26
+ const $rows = wrapper . findAll ( 'tbody > tr' ) . array
27
27
expect ( $rows . length ) . toBe ( 3 )
28
- expect (
29
- $rows
30
- . at ( 0 )
31
- . findAll ( 'td' )
32
- . at ( 0 )
33
- . text ( )
34
- ) . toBe ( '3' )
35
- expect (
36
- $rows
37
- . at ( 1 )
38
- . findAll ( 'td' )
39
- . at ( 0 )
40
- . text ( )
41
- ) . toBe ( '1' )
42
- expect (
43
- $rows
44
- . at ( 2 )
45
- . findAll ( 'td' )
46
- . at ( 0 )
47
- . text ( )
48
- ) . toBe ( '2' )
28
+ // Map the rows to the first column text value
29
+ const columnA = $rows . map ( row => {
30
+ return row . findAll ( 'td' ) . at ( 0 ) . text ( )
31
+ } )
32
+ expect ( columnA [ 0 ] ) . toBe ( '3' )
33
+ expect ( columnA [ 1 ] ) . toBe ( '1' )
34
+ expect ( columnA [ 2 ] ) . toBe ( '2' )
35
+ } )
36
+
37
+ it ( 'should sort column descending when sortBy set and sortDesc changed' , async ( ) => {
38
+ const wrapper = mount ( Table , {
39
+ propsData : {
40
+ fields : testFields ,
41
+ items : testItems ,
42
+ sortBy : 'a' ,
43
+ sortDesc : false
44
+ }
45
+ } )
46
+ expect ( wrapper ) . toBeDefined ( )
47
+ expect ( wrapper . findAll ( 'tbody > tr' ) . exists ( ) ) . toBe ( true )
48
+ expect ( wrapper . findAll ( 'tbody > tr' ) . length ) . toBe ( 3 )
49
+ let $rows
50
+ let columnA
51
+
52
+ await wrapper . vm . $nextTick ( )
53
+ expect ( wrapper . emitted ( 'input' ) ) . toBeDefined ( )
54
+ expect ( wrapper . emitted ( 'input' ) . length ) . toBe ( 1 )
55
+ $rows = wrapper . findAll ( 'tbody > tr' ) . array
56
+ expect ( $rows . length ) . toBe ( 3 )
57
+ // Map the rows to the first column text value
58
+ columnA = $rows . map ( row => {
59
+ return row . findAll ( 'td' ) . at ( 0 ) . text ( )
60
+ } )
61
+ expect ( columnA [ 0 ] ) . toBe ( '1' )
62
+ expect ( columnA [ 1 ] ) . toBe ( '2' )
63
+ expect ( columnA [ 2 ] ) . toBe ( '3' )
64
+
65
+ // Change sort direction
66
+ wrapper . setProps ( {
67
+ sortDesc : true
68
+ } )
69
+ await wrapper . vm . $nextTick ( )
70
+ expect ( wrapper . emitted ( 'input' ) . length ) . toBe ( 2 )
71
+ $rows = wrapper . findAll ( 'tbody > tr' ) . array
72
+ expect ( $rows . length ) . toBe ( 3 )
73
+ // Map the rows to the first column text value
74
+ columnA = $rows . map ( row => {
75
+ return row . findAll ( 'td' ) . at ( 0 ) . text ( )
76
+ } )
77
+ expect ( columnA [ 0 ] ) . toBe ( '3' )
78
+ expect ( columnA [ 1 ] ) . toBe ( '2' )
79
+ expect ( columnA [ 2 ] ) . toBe ( '1' )
80
+
81
+ // Clear sort
82
+ wrapper . setProps ( {
83
+ sortBy : null ,
84
+ sortDesc : false
85
+ } )
86
+ await wrapper . vm . $nextTick ( )
87
+ expect ( wrapper . emitted ( 'input' ) . length ) . toBe ( 3 )
88
+ $rows = wrapper . findAll ( 'tbody > tr' ) . array
89
+ expect ( $rows . length ) . toBe ( 3 )
90
+ // Map the rows to the first column text value
91
+ columnA = $rows . map ( row => {
92
+ return row . findAll ( 'td' ) . at ( 0 ) . text ( )
93
+ } )
94
+ expect ( columnA [ 0 ] ) . toBe ( '3' )
95
+ expect ( columnA [ 1 ] ) . toBe ( '1' )
96
+ expect ( columnA [ 2 ] ) . toBe ( '2' )
49
97
} )
50
98
} )
0 commit comments