"use strict";

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _Router = require("../Router");

var _sinon = _interopRequireDefault(require("sinon"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* globals console */

/* eslint no-console: ["error", { allow: ["warn", "error"] }] */

/* eslint-disable react/jsx-no-bind */
describe('Router', function () {
  var View = function View() {
    return _react.default.createElement("button", null);
  }; // the internal representation of
  // <Router>
  // 	<Route path="app" component={View}>
  // 		<Route path="home" component={View} />
  // 		<Route path="settings" component={View} />
  // 	</Route>
  // 	<Route path="admin" component={View} />
  // </Router>


  var routes = {
    app: {
      $component: View,
      $props: {},
      home: {
        $component: View,
        $props: {}
      },
      settings: {
        $component: View,
        $props: {}
      }
    },
    admin: {
      $component: View,
      $props: {}
    }
  };
  it('should render a single component matching the {path}', function () {
    var subject = (0, _enzyme.shallow)(_react.default.createElement(_Router.Router, {
      routes: routes,
      path: "/app"
    }));
    var expected = 1;
    var actual = subject.find(View).length;
    expect(actual).to.equal(expected);
  });
  it('should render an array of components matching the {path}', function () {
    var subject = (0, _enzyme.shallow)(_react.default.createElement(_Router.Router, {
      routes: routes,
      path: "/app/home"
    }));
    var expected = 2;
    var actual = subject.find(View).length;
    expect(actual).to.equal(expected);
  });
  it('should render an array of components matching the {path} as an array', function () {
    var subject = (0, _enzyme.shallow)(_react.default.createElement(_Router.Router, {
      routes: routes,
      path: ['app', 'home']
    }));
    var expected = 2;
    var actual = subject.find(View).length;
    expect(actual).to.equal(expected);
  });
  it('should render no children if {path} does not exist in {routes}', function () {
    // Remove Global Spy and replace with a stub instead
    console.error.restore();

    _sinon.default.stub(console, 'error');

    var subject = (0, _enzyme.shallow)(_react.default.createElement(_Router.Router, {
      routes: routes,
      path: "/help"
    }));
    var expected = 0;
    var actual = subject.children().length;
    expect(actual).to.equal(expected);
  });
  it('should render children into {component}', function () {
    var component = function component(props) {
      return _react.default.createElement("div", {
        className: "div1"
      }, _react.default.createElement("div", {
        className: "div2"
      }, props.children));
    };

    var subject = (0, _enzyme.mount)(_react.default.createElement(_Router.Router, {
      routes: routes,
      path: "/app/settings",
      component: component
    }));
    var expected = 2;
    var actual = subject.find('.div2').children().length;
    expect(actual).to.equal(expected);
  });
  it('should compile children into route object', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Router.Router, {
      path: "/app"
    }, _react.default.createElement(_Router.Route, {
      path: "app",
      component: View
    }, _react.default.createElement(_Router.Route, {
      path: "home",
      component: View
    }), _react.default.createElement(_Router.Route, {
      path: "settings",
      component: View
    })), _react.default.createElement(_Router.Route, {
      path: "admin",
      component: View
    })));
    var expected = JSON.stringify(routes);
    var actual = JSON.stringify(subject.instance().routes);
    expect(actual).to.equal(expected);
  });
  it('should render an array of components matching the {path} using JSX routes', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Router.Router, {
      path: "/app/home"
    }, _react.default.createElement(_Router.Route, {
      path: "app",
      component: View
    }, _react.default.createElement(_Router.Route, {
      path: "home",
      component: View
    }), _react.default.createElement(_Router.Route, {
      path: "settings",
      component: View
    })), _react.default.createElement(_Router.Route, {
      path: "admin",
      component: View
    })));
    var expected = 2;
    var actual = subject.find(View).length;
    expect(actual).to.equal(expected);
  });
  it('should render a different component when the routes change for the same {path}', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Router.Router, {
      path: "/app"
    }, _react.default.createElement(_Router.Route, {
      path: "app",
      component: View
    }, _react.default.createElement(_Router.Route, {
      path: "home",
      component: View
    }), _react.default.createElement(_Router.Route, {
      path: "settings",
      component: View
    })), _react.default.createElement(_Router.Route, {
      path: "admin",
      component: View
    })));

    var NewView = function NewView() {
      return _react.default.createElement("span", null);
    };

    subject.setProps({
      path: '/app',
      children: [_react.default.createElement(_Router.Route, {
        path: "app",
        component: NewView
      })]
    });
    subject.update();
    var expected = NewView;
    var actual = subject.childAt(0).childAt(0).type();
    expect(actual).to.equal(expected);
  });
  it('should render nothing for an invalid path', function () {
    // Remove Global Spy and replace with a stub instead
    console.error.restore();

    _sinon.default.stub(console, 'error');

    var subject = (0, _enzyme.mount)(_react.default.createElement(_Router.Router, {
      path: "/does/not/exist"
    }, _react.default.createElement(_Router.Route, {
      path: "app",
      component: View
    }, _react.default.createElement(_Router.Route, {
      path: "home",
      component: View
    }), _react.default.createElement(_Router.Route, {
      path: "settings",
      component: View
    })), _react.default.createElement(_Router.Route, {
      path: "admin",
      component: View
    })));
    var expected = 0;
    var actual = subject.find('div').children().length;
    expect(actual).to.equal(expected);
  });
  it('should render nothing for a partially valid path', function () {
    // Remove Global Spy and replace with a stub instead
    console.error.restore();

    _sinon.default.stub(console, 'error');

    var subject = (0, _enzyme.mount)(_react.default.createElement(_Router.Router, {
      path: "/app/home/other"
    }, _react.default.createElement(_Router.Route, {
      path: "app",
      component: View
    }, _react.default.createElement(_Router.Route, {
      path: "home",
      component: View
    }), _react.default.createElement(_Router.Route, {
      path: "settings",
      component: View
    })), _react.default.createElement(_Router.Route, {
      path: "admin",
      component: View
    })));
    var expected = 0;
    var actual = subject.find('div').children().length;
    expect(actual).to.equal(expected);
  });
});