1
+ casper . test . begin ( 'grid' , 73 , function ( test ) {
2
+
3
+ casper
4
+ . start ( '../../examples/grid/index.html' )
5
+ . then ( function ( ) {
6
+ // headers
7
+ test . assertElementCount ( 'th' , 2 )
8
+ test . assertElementCount ( 'th.active' , 0 )
9
+ test . assertSelectorHasText ( 'th:nth-child(1)' , 'Name' )
10
+ test . assertSelectorHasText ( 'th:nth-child(2)' , 'Power' )
11
+ assertTable ( test , [ 'name' , 'power' ] , [
12
+ { name : 'Chuck Norris' , power : Infinity } ,
13
+ { name : 'Bruce Lee' , power : 9000 } ,
14
+ { name : 'Jacky Chang' , power : 7000 } ,
15
+ { name : 'Jet Li' , power : 8000 }
16
+ ] )
17
+ } )
18
+ // test sorting
19
+ . thenClick ( 'th:nth-child(1)' , function ( ) {
20
+ test . assertElementCount ( 'th.active:nth-child(1)' , 1 )
21
+ test . assertElementCount ( 'th.active:nth-child(2)' , 0 )
22
+ test . assertElementCount ( 'th:nth-child(1) .arrow.dsc' , 1 )
23
+ test . assertElementCount ( 'th:nth-child(2) .arrow.dsc' , 0 )
24
+ assertTable ( test , [ 'name' , 'power' ] , [
25
+ { name : 'Jet Li' , power : 8000 } ,
26
+ { name : 'Jacky Chang' , power : 7000 } ,
27
+ { name : 'Chuck Norris' , power : Infinity } ,
28
+ { name : 'Bruce Lee' , power : 9000 }
29
+ ] )
30
+ } )
31
+ . thenClick ( 'th:nth-child(2)' , function ( ) {
32
+ test . assertElementCount ( 'th.active:nth-child(1)' , 0 )
33
+ test . assertElementCount ( 'th.active:nth-child(2)' , 1 )
34
+ test . assertElementCount ( 'th:nth-child(1) .arrow.dsc' , 1 )
35
+ test . assertElementCount ( 'th:nth-child(2) .arrow.dsc' , 1 )
36
+ assertTable ( test , [ 'name' , 'power' ] , [
37
+ { name : 'Chuck Norris' , power : Infinity } ,
38
+ { name : 'Bruce Lee' , power : 9000 } ,
39
+ { name : 'Jet Li' , power : 8000 } ,
40
+ { name : 'Jacky Chang' , power : 7000 }
41
+ ] )
42
+ } )
43
+ . thenClick ( 'th:nth-child(2)' , function ( ) {
44
+ test . assertElementCount ( 'th.active:nth-child(1)' , 0 )
45
+ test . assertElementCount ( 'th.active:nth-child(2)' , 1 )
46
+ test . assertElementCount ( 'th:nth-child(1) .arrow.dsc' , 1 )
47
+ test . assertElementCount ( 'th:nth-child(2) .arrow.asc' , 1 )
48
+ assertTable ( test , [ 'name' , 'power' ] , [
49
+ { name : 'Jacky Chang' , power : 7000 } ,
50
+ { name : 'Jet Li' , power : 8000 } ,
51
+ { name : 'Bruce Lee' , power : 9000 } ,
52
+ { name : 'Chuck Norris' , power : Infinity }
53
+ ] )
54
+ } )
55
+ . thenClick ( 'th:nth-child(1)' , function ( ) {
56
+ test . assertElementCount ( 'th.active:nth-child(1)' , 1 )
57
+ test . assertElementCount ( 'th.active:nth-child(2)' , 0 )
58
+ test . assertElementCount ( 'th:nth-child(1) .arrow.asc' , 1 )
59
+ test . assertElementCount ( 'th:nth-child(2) .arrow.asc' , 1 )
60
+ assertTable ( test , [ 'name' , 'power' ] , [
61
+ { name : 'Bruce Lee' , power : 9000 } ,
62
+ { name : 'Chuck Norris' , power : Infinity } ,
63
+ { name : 'Jacky Chang' , power : 7000 } ,
64
+ { name : 'Jet Li' , power : 8000 }
65
+ ] )
66
+ } )
67
+ // test search
68
+ . then ( function ( ) {
69
+ this . fill ( '#search' , {
70
+ query : 'j'
71
+ } )
72
+ } )
73
+ . then ( function ( ) {
74
+ assertTable ( test , [ 'name' , 'power' ] , [
75
+ { name : 'Jacky Chang' , power : 7000 } ,
76
+ { name : 'Jet Li' , power : 8000 }
77
+ ] )
78
+ } )
79
+ . then ( function ( ) {
80
+ this . fill ( '#search' , {
81
+ query : 'infinity'
82
+ } )
83
+ } )
84
+ . then ( function ( ) {
85
+ assertTable ( test , [ 'name' , 'power' ] , [
86
+ { name : 'Chuck Norris' , power : Infinity }
87
+ ] )
88
+ } )
89
+ // run
90
+ . run ( function ( ) {
91
+ test . done ( )
92
+ } )
93
+
94
+ /**
95
+ * Helper to assert the table data is rendered correctly.
96
+ *
97
+ * @param {CasperTester } test
98
+ * @param {Array } columns
99
+ * @param {Array } data
100
+ */
101
+
102
+ function assertTable ( test , columns , data ) {
103
+ test . assertElementCount ( 'td' , data . length * columns . length )
104
+ for ( var i = 0 ; i < data . length ; i ++ ) {
105
+ for ( var j = 0 ; j < columns . length ; j ++ ) {
106
+ test . assertSelectorHasText (
107
+ 'tr:nth-child(' + ( i + 1 ) + ') td:nth-child(' + ( j + 1 ) + ')' ,
108
+ data [ i ] [ columns [ j ] ]
109
+ )
110
+ }
111
+ }
112
+ }
113
+
114
+ } )
0 commit comments