@@ -247,3 +247,66 @@ exports.assertPlotSize = function(opts, msg) {
247247 if ( widthLessThan ) expect ( actualWidth ) . toBeLessThan ( widthLessThan - 1 , 'widthLessThan' + msgPlus ) ;
248248 if ( heightLessThan ) expect ( actualHeight ) . toBeLessThan ( heightLessThan - 1 , 'heightLessThan' + msgPlus ) ;
249249} ;
250+
251+ /**
252+ * Ordering test - since SVG layering is purely dependent on ordering in the
253+ * node tree, this tells you if the items are layered correctly.
254+ * Note that we only take the first matching node for each selector, and it's
255+ * not necessary that the nodes be siblings or at the same level of nesting.
256+ *
257+ * @param {string } selectorBehind: css selector for the node that should be behind
258+ * @param {string } selectorInFront: css selector for the node that should be in front
259+ * @param {string } msg: context for debugging
260+ */
261+ exports . assertNodeOrder = function ( selectorBehind , selectorInFront , msg ) {
262+ var nodeBehind = document . querySelector ( selectorBehind ) ;
263+ var nodeInFront = document . querySelector ( selectorInFront ) ;
264+ if ( ! nodeBehind ) {
265+ fail ( selectorBehind + ' not found (' + msg + ')' ) ;
266+ }
267+ else if ( ! nodeInFront ) {
268+ fail ( selectorInFront + ' not found (' + msg + ')' ) ;
269+ }
270+ else {
271+ var parentsBehind = getParents ( nodeBehind ) ;
272+ var parentsInFront = getParents ( nodeInFront ) ;
273+
274+ var commonParent = null ;
275+ var siblingBehind = null ;
276+ var siblingInFront = null ;
277+ for ( var i = 0 ; i < parentsBehind . length ; i ++ ) {
278+ if ( parentsBehind [ i ] === parentsInFront [ i ] ) {
279+ commonParent = parentsBehind [ i ] ;
280+ }
281+ else {
282+ siblingBehind = parentsBehind [ i ] ;
283+ siblingInFront = parentsInFront [ i ] ;
284+ break ;
285+ }
286+ }
287+ var allSiblings = collectionToArray ( commonParent . children ) ;
288+ var behindIndex = allSiblings . indexOf ( siblingBehind ) ;
289+ var frontIndex = allSiblings . indexOf ( siblingInFront ) ;
290+
291+ // sanity check - if these fail there's just something wrong in this routine
292+ expect ( behindIndex ) . toBeGreaterThan ( - 1 , 'error in assertNodeOrder: ' + msg ) ;
293+ expect ( frontIndex ) . toBeGreaterThan ( - 1 , 'error in assertNodeOrder: ' + msg ) ;
294+
295+ // the real test
296+ expect ( frontIndex ) . toBeGreaterThan ( behindIndex ,
297+ '"' + selectorBehind + '" is not behind "' + selectorInFront + '": ' + msg ) ;
298+ }
299+ } ;
300+
301+ function getParents ( node ) {
302+ var parent = node . parentNode ;
303+ if ( parent ) return getParents ( parent ) . concat ( node ) ;
304+ return [ node ] ;
305+ }
306+
307+ function collectionToArray ( collection ) {
308+ var len = collection . length ;
309+ var a = new Array ( len ) ;
310+ for ( var i = 0 ; i < len ; i ++ ) a [ i ] = collection [ i ] ;
311+ return a ;
312+ }
0 commit comments