@@ -11,7 +11,7 @@ test('isElement', function (t) {
1111 function ( ) {
1212 isElement ( null , true )
1313 } ,
14- ' Expected ` string` or `Array.<string>` for `tagNames`, not `true`' ,
14+ / E x p e c t e d f u n c t i o n , s t r i n g , o r a r r a y a s t e s t / ,
1515 'should throw when the second parameter is invalid'
1616 )
1717
@@ -99,5 +99,115 @@ test('isElement', function (t) {
9999 st . end ( )
100100 } )
101101
102+ t . test ( 'isElement(node, test)' , function ( st ) {
103+ st . equal (
104+ isElement ( { type : 'text' } , function ( ) {
105+ throw new Error ( '!' )
106+ } ) ,
107+ false ,
108+ 'should not call `test` if the given node is not an element'
109+ )
110+
111+ st . equal (
112+ isElement ( { type : 'element' , tagName : 'a' , children : [ ] } , function ( node ) {
113+ return node . children . length === 0
114+ } ) ,
115+ true ,
116+ 'should call `test` if the given node is a valid element (1)'
117+ )
118+
119+ st . equal (
120+ isElement (
121+ { type : 'element' , tagName : 'a' , children : [ { type : 'text' } ] } ,
122+ function ( node ) {
123+ return node . children . length === 0
124+ }
125+ ) ,
126+ false ,
127+ 'should call `test` if the given node is a valid element (2)'
128+ )
129+
130+ var ctx = { }
131+ var root = {
132+ type : 'root' ,
133+ children : [ { type : 'element' , tagName : 'a' , children : [ ] } ]
134+ }
135+
136+ st . equal (
137+ isElement (
138+ root . children [ 0 ] ,
139+ function ( node , index , parent ) {
140+ st . equal ( node , root . children [ 0 ] , 'should pass `node` to test' )
141+ st . equal ( index , 0 , 'should pass `index` to test' )
142+ st . equal ( parent , root , 'should pass `parent` to test' )
143+ st . equal ( this , ctx , 'should pass `context` to test' )
144+ } ,
145+ 0 ,
146+ root ,
147+ ctx
148+ ) ,
149+ false ,
150+ 'should call `test` if the given node is a valid element (2)'
151+ )
152+
153+ st . throws (
154+ function ( ) {
155+ isElement ( root . children [ 0 ] , function ( ) { } , 0 )
156+ } ,
157+ / E x p e c t e d b o t h p a r e n t a n d i n d e x / ,
158+ 'should throw if `index` is passed but not `parent`'
159+ )
160+
161+ st . throws (
162+ function ( ) {
163+ isElement ( root . children [ 0 ] , function ( ) { } , undefined , root )
164+ } ,
165+ / E x p e c t e d b o t h p a r e n t a n d i n d e x / ,
166+ 'should throw if `parent` is passed but not `index`'
167+ )
168+
169+ st . throws (
170+ function ( ) {
171+ isElement ( root . children [ 0 ] , function ( ) { } , false )
172+ } ,
173+ / E x p e c t e d p o s i t i v e f i n i t e i n d e x f o r c h i l d n o d e / ,
174+ 'should throw if `index` is not a number'
175+ )
176+
177+ st . throws (
178+ function ( ) {
179+ isElement ( root . children [ 0 ] , function ( ) { } , - 1 )
180+ } ,
181+ / E x p e c t e d p o s i t i v e f i n i t e i n d e x f o r c h i l d n o d e / ,
182+ 'should throw if `index` is negative'
183+ )
184+
185+ st . throws (
186+ function ( ) {
187+ isElement ( root . children [ 0 ] , function ( ) { } , Infinity )
188+ } ,
189+ / E x p e c t e d p o s i t i v e f i n i t e i n d e x f o r c h i l d n o d e / ,
190+ 'should throw if `index` is infinity'
191+ )
192+
193+ st . throws (
194+ function ( ) {
195+ isElement ( root . children [ 0 ] , function ( ) { } , 0 , true )
196+ } ,
197+ / E x p e c t e d p a r e n t n o d e / ,
198+ 'should throw if `parent` is not a node'
199+ )
200+
201+ st . throws (
202+ function ( ) {
203+ isElement ( root . children [ 0 ] , function ( ) { } , 0 , { type : 'root' } )
204+ } ,
205+ / E x p e c t e d p a r e n t n o d e / ,
206+ 'should throw if `parent` is not a parent'
207+ )
208+
209+ st . end ( )
210+ } )
211+
102212 t . end ( )
103213} )
0 commit comments