'use strict';
describe('ngSwitch', function() {
  var element;
  afterEach(function(){
    dealoc(element);
  });
  it('should switch on value change', inject(function($rootScope, $compile) {
    element = $compile(
      '
' +
        '
first:{{name}}
' +
        '
second:{{name}}
' +
        '
true:{{name}}
' +
      '
 ')($rootScope);
    expect(element.html()).toEqual(
        '');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('first:');
    $rootScope.name="shyam";
    $rootScope.$apply();
    expect(element.text()).toEqual('first:shyam');
    $rootScope.select = 2;
    $rootScope.$apply();
    expect(element.text()).toEqual('second:shyam');
    $rootScope.name = 'misko';
    $rootScope.$apply();
    expect(element.text()).toEqual('second:misko');
    $rootScope.select = true;
    $rootScope.$apply();
    expect(element.text()).toEqual('true:misko');
  }));
  it('should show all switch-whens that match the current value', inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '- first:{{name}}
 ' +
        '- , first too:{{name}}
 ' +
        '- second:{{name}}
 ' +
        '- true:{{name}}
 ' +
      '
')($rootScope);
    expect(element.html()).toEqual('' +
                                   '' +
                                   '' +
                                   '');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('first:, first too:');
    $rootScope.name="shyam";
    $rootScope.$apply();
    expect(element.text()).toEqual('first:shyam, first too:shyam');
    $rootScope.select = 2;
    $rootScope.$apply();
    expect(element.text()).toEqual('second:shyam');
    $rootScope.name = 'misko';
    $rootScope.$apply();
    expect(element.text()).toEqual('second:misko');
    $rootScope.select = true;
    $rootScope.$apply();
    expect(element.text()).toEqual('true:misko');
  }));
  it('should switch on switch-when-default', inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        'one
' +
        'other
' +
      '')($rootScope);
    $rootScope.$apply();
    expect(element.text()).toEqual('other');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('one');
  }));
  it('should show all switch-when-default', inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '- one
 ' +
        '- other
 ' +
        '- , other too
 ' +
      '
')($rootScope);
    $rootScope.$apply();
    expect(element.text()).toEqual('other, other too');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('one');
  }));
  it('should always display the elements that do not match a switch',
      inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '- always 
 ' +
        '- one 
 ' +
        '- two 
 ' +
        '- other, 
 ' +
        '- other too 
 ' +
      '
')($rootScope);
    $rootScope.$apply();
    expect(element.text()).toEqual('always other, other too ');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('always one ');
  }));
  it('should display the elements that do not have ngSwitchWhen nor ' +
     'ngSwitchDefault at the position specified in the template, when the ' +
     'first and last elements in the ngSwitch body do not have a ngSwitch* ' +
     'directive', inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '- 1
 ' +
        '- 2
 ' +
        '- 3
 ' +
        '- 4
 ' +
        '- 5
 ' +
        '- 6
 ' +
        '- 7
 ' +
        '- 8
 ' +
      '
')($rootScope);
    $rootScope.$apply();
    expect(element.text()).toEqual('135678');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('12368');
  }));
  it('should display the elements that do not have ngSwitchWhen nor ' +
     'ngSwitchDefault at the position specified in the template when the ' +
     'first and last elements in the ngSwitch have a ngSwitch* directive',
      inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '- 2
 ' +
        '- 3
 ' +
        '- 4
 ' +
        '- 5
 ' +
        '- 6
 ' +
        '- 7
 ' +
      '
')($rootScope);
    $rootScope.$apply();
    expect(element.text()).toEqual('3567');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('236');
  }));
  it('should call change on switch', inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '{{name}}
' +
      '')($rootScope);
    $rootScope.url = 'a';
    $rootScope.$apply();
    expect($rootScope.name).toEqual('works');
    expect(element.text()).toEqual('works');
  }));
  it('should properly create and destroy child scopes', inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '{{name}}
' +
      '')($rootScope);
    $rootScope.$apply();
    var getChildScope = function() { return element.find('div').scope(); };
    expect(getChildScope()).toBeUndefined();
    $rootScope.url = 'a';
    $rootScope.$apply();
    var child1 = getChildScope();
    expect(child1).toBeDefined();
    spyOn(child1, '$destroy');
    $rootScope.url = 'x';
    $rootScope.$apply();
    expect(getChildScope()).toBeUndefined();
    expect(child1.$destroy).toHaveBeenCalledOnce();
    $rootScope.url = 'a';
    $rootScope.$apply();
    var child2 = getChildScope();
    expect(child2).toBeDefined();
    expect(child2).not.toBe(child1);
  }));
  it('should not leak jq data when compiled but not attached to parent when parent is destroyed',
      inject(function($rootScope, $compile) {
    element = $compile(
      '' +
        '
' +
          '{{name}}
' +
        '' +
      '
 ')($rootScope);
    $rootScope.$apply();
    // element now contains only empty repeater. this element is dealocated by local afterEach.
    // afterwards a global afterEach will check for leaks in jq data cache object
  }));
});
describe('ngSwitch animations', function() {
  var body, element, $rootElement;
  function html(html) {
    $rootElement.html(html);
    element = $rootElement.children().eq(0);
    return element;
  }
  beforeEach(module('mock.animate'));
  beforeEach(module(function() {
    // we need to run animation on attached elements;
    return function(_$rootElement_) {
      $rootElement = _$rootElement_;
      body = jqLite(document.body);
      body.append($rootElement);
    };
  }));
  afterEach(function(){
    dealoc(body);
    dealoc(element);
  });
  it('should fire off the enter animation',
    inject(function($compile, $rootScope, $animate) {
      var item;
      var $scope = $rootScope.$new();
      element = $compile(html(
        '' +
          '
one
' +
          '
two
' +
          '
three
' +
        '
 '
      ))($scope);
      $rootScope.$digest(); // re-enable the animations;
      $scope.val = 'one';
      $scope.$digest();
      item = $animate.process('enter').element;
      expect(item.text()).toBe('one');
  }));
  it('should fire off the leave animation',
    inject(function($compile, $rootScope, $animate) {
      var item;
      var $scope = $rootScope.$new();
      element = $compile(html(
        '' +
          '
one
' +
          '
two
' +
          '
three
' +
        '
 '
      ))($scope);
      $rootScope.$digest(); // re-enable the animations;
      $scope.val = 'two';
      $scope.$digest();
      item = $animate.process('enter').element;
      expect(item.text()).toBe('two');
      $scope.val = 'three';
      $scope.$digest();
      item = $animate.process('leave').element;
      expect(item.text()).toBe('two');
      item = $animate.process('enter').element;
      expect(item.text()).toBe('three');
  }));
});