/* eslint-env browser */
import {findSingleNode, getFindDOMNode} from './helpers';

let findDOMNode = findDOMNode || (global && global.findDOMNode);

function haveComponentWithXpath(component, expression) {
  findDOMNode = findDOMNode || getFindDOMNode();
  const domNode = findDOMNode(component);

  document.body.appendChild(domNode);
  const xpathNode = findSingleNode(expression, domNode.parentNode);
  document.body.removeChild(domNode);

  return xpathNode !== null;
}

export default function haveXpath(Chai) {
  Chai.Assertion.addMethod('xpath', function evaluateXpath(xpath) {
    findDOMNode = findDOMNode || getFindDOMNode();

    const dom = findDOMNode(this._obj).outerHTML;

    this.assert(
      haveComponentWithXpath(this._obj, xpath),
      'Expected "' + dom + '" to have xpath \'' + xpath + '\'',
      'Expected "' + dom + '" to not have xpath \'' + xpath + '\''
    );
  });
}

@@ -152,33 +152,31 @@
 unction have
-Component
+DomNode
 WithXpath(co
@@ -173,25 +173,23 @@
 thXpath(
-component
+domNode
 , expres
@@ -200,100 +200,8 @@
 ) %7B%0A
-  findDOMNode = findDOMNode %7C%7C getFindDOMNode();%0A  const domNode = findDOMNode(component);%0A%0A
   do
@@ -548,16 +548,20 @@
 onst dom
+Node
  = findD
@@ -581,18 +581,8 @@
 obj)
-.outerHTML
 ;%0A%0A 
@@ -611,17 +611,15 @@
 have
-Component
+DomNode
 With
@@ -624,25 +624,23 @@
 thXpath(
-this._obj
+domNode
 , xpath)
@@ -657,32 +657,46 @@
 xpected %22' + dom
+Node.outerHTML
  + '%22 to have xp
@@ -743,16 +743,30 @@
 %22' + dom
+Node.outerHTML
  + '%22 to

Remove unnecessary call to findDOMNode