'use strict';
describe('ngOutlet', function () {
  var elt,
      $compile,
      $rootScope,
      $router,
      $templateCache,
      $controllerProvider;
  var OneController, TwoController, UserController;
  beforeEach(function () {
    module('ng');
    module('ngComponentRouter');
    module(function (_$controllerProvider_) {
      $controllerProvider = _$controllerProvider_;
    });
    inject(function (_$compile_, _$rootScope_, _$router_, _$templateCache_) {
      $compile = _$compile_;
      $rootScope = _$rootScope_;
      $router = _$router_;
      $templateCache = _$templateCache_;
    });
    UserController = registerComponent('user', '
hello {{user.name}}
', function ($routeParams) {
      this.name = $routeParams.name;
    });
    OneController = registerComponent('one', '{{one.number}}
', boringController('number', 'one'));
    TwoController = registerComponent('two', '{{two.number}}
', boringController('number', 'two'));
  });
  it('should allow linking from the parent to the child', function () {
    put('one', '{{number}}
');
    $router.config([
      { path: '/a', component: OneController },
      { path: '/b', component: TwoController, as: 'two' }
    ]);
    compile('link | outer {  }');
    $router.navigate('/a');
    $rootScope.$digest();
    expect(elt.find('a').attr('href')).toBe('./b');
  });
  it('should allow linking from the child and the parent', function () {
    put('one', '');
    $router.config([
      { path: '/a', component: OneController },
      { path: '/b', component: TwoController, as: 'two' }
    ]);
    compile('outer {  }');
    $router.navigate('/a');
    $rootScope.$digest();
    expect(elt.find('a').attr('href')).toBe('./b');
  });
  it('should allow params in routerLink directive', function () {
    put('router', '');
    put('one', '');
    $router.config([
      { path: '/a', component: OneController },
      { path: '/b/:param', component: TwoController, as: 'two' }
    ]);
    compile('');
    $router.navigate('/a');
    $rootScope.$digest();
    expect(elt.find('a').attr('href')).toBe('./b/lol');
  });
  // TODO: test dynamic links
  it('should update the href of links with bound params', function () {
    put('router', '');
    put('one', '');
    $router.config([
      { path: '/a', component: OneController },
      { path: '/b/:param', component: TwoController, as: 'two' }
    ]);
    compile('');
    $router.navigate('/a');
    $rootScope.$digest();
    expect(elt.find('a').attr('href')).toBe('./b/one');
  });
  it('should navigate on left-mouse click when a link url matches a route', function () {
    $router.config([
      { path: '/', component: OneController },
      { path: '/two', component: TwoController }
    ]);
    compile('link | ');
    $rootScope.$digest();
    expect(elt.text()).toBe('link | one');
    elt.find('a')[0].click();
    $rootScope.$digest();
    expect(elt.text()).toBe('link | two');
  });
  it('should not navigate on non-left mouse click when a link url matches a route', inject(function ($router) {
    $router.config([
      { path: '/', component: OneController },
      { path: '/two', component: TwoController }
    ]);
    compile('link | ');
    $rootScope.$digest();
    expect(elt.text()).toBe('link | one');
    elt.find('a').triggerHandler({ type: 'click', which: 3 });
    $rootScope.$digest();
    expect(elt.text()).toBe('link | one');
  }));
  // See https://github.com/angular/router/issues/206
  it('should not navigate a link without an href', function () {
    $router.config([
      { path: '/', component: OneController },
      { path: '/two', component: TwoController }
    ]);
    expect(function () {
      compile('link');
      $rootScope.$digest();
      expect(elt.text()).toBe('link');
      elt.find('a')[0].click();
      $rootScope.$digest();
    }).not.toThrow();
  });
  function registerComponent(name, template, config) {
    var Ctrl;
    if (!template) {
      template = '';
    }
    if (!config) {
      Ctrl = function () {};
    } else if (angular.isArray(config)) {
      Ctrl = function () {};
      Ctrl.annotations = [new angular.annotations.RouteConfig(config)];
    } else if (typeof config === 'function') {
      Ctrl = config;
    } else {
      Ctrl = function () {};
      if (config.canActivate) {
        Ctrl.$canActivate = config.canActivate;
        delete config.canActivate;
      }
      Ctrl.prototype = config;
    }
    $controllerProvider.register(componentControllerName(name), Ctrl);
    put(name, template);
    return Ctrl;
  }
  function boringController(model, value) {
    return function () {
      this[model] = value;
    };
  }
  function put(name, template) {
    $templateCache.put(componentTemplatePath(name), [200, template, {}]);
  }
  function compile(template) {
    elt = $compile('' + template + '
')($rootScope);
    $rootScope.$digest();
    return elt;
  }
});