/*
 * The MIT License
 *
 * Copyright (c) 2016 Juan Cruz Viotti. https://github.com/jviotti
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

'use strict';

var m = require('mochainon');
var angular = require('angular');
require('angular-mocks');

describe('AngularIfState', function() {

  this.timeout(false);

  beforeEach(angular.mock.module(
    require('./index')
  ));

  var $compile;
  var $rootScope;
  var $state;

  beforeEach(angular.mock.inject(function(_$compile_, _$rootScope_, _$state_) {
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $state = _$state_;
  }));

  describe('given the current state is "foo"', function() {

    beforeEach(function() {
      this.stateIsStub = m.sinon.stub($state, 'is');
      this.stateIsStub.withArgs('foo').returns(true);
      this.stateIsStub.returns(false);
    });

    afterEach(function() {
      this.stateIsStub.restore();
    });

    describe('hideIfState', function() {

      it('should hide the element if the attribute equals "foo"', function() {
        var element = $compile('<span hide-if-state="foo">FooBar.io</span>')($rootScope);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('none');
      });

      it('should show the element if the attribute does not equal "foo"', function() {
        var element = $compile('<span hide-if-state="bar">FooBar.io</span>')($rootScope);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('initial');
      });

      it('should show the element if the state changes', function() {
        var element = $compile('<span hide-if-state="foo">FooBar.io</span>')($rootScope);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('none');
        this.stateIsStub.withArgs('foo').returns(false);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('initial');
      });

    });

    describe('showIfState', function() {

      it('should hide the element if the attribute does not equal "foo"', function() {
        var element = $compile('<span show-if-state="bar">FooBar.io</span>')($rootScope);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('none');
      });

      it('should show the element if the attribute equals "foo"', function() {
        var element = $compile('<span show-if-state="foo">FooBar.io</span>')($rootScope);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('initial');
      });

      it('should hide the element if the state changes', function() {
        var element = $compile('<span show-if-state="foo">FooBar.io</span>')($rootScope);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('initial');
        this.stateIsStub.withArgs('foo').returns(false);
        $rootScope.$digest();
        m.chai.expect(element.css('display')).to.equal('none');
      });

    });

  });

});
