/*!
  Copyright (C) 2017 Google Inc.
  Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
*/

describe('can.Control', function () {
  'use strict';

  var Control;

  beforeEach(function () {
    Control = can.Control.prototype;
  });

  describe('bindXHRToButton() method', function () {
    var dfd;

    beforeEach(function () {
      dfd = new can.Deferred();
    });

    function callBindXHRToButton(done, actualInnerHtml, newtext, disable) {
      var element = $('<button>' + actualInnerHtml + '</button>');

      // call bindXHRToButton()
      Control.bindXHRToButton(dfd, element, newtext, disable);

      // button should have 'disabled' class after call bindXHRToButton
      expect(element.hasClass('disabled')).toEqual(true);

      if (disable) {
        // button should have 'disabled' attr after call bindXHRToButton
        expect(element.attr('disabled')).toEqual('disabled');
      }

      if (newtext) {
        // button should have 'newText' before dfd.resolve()
        expect(element[0].innerHTML).toEqual(newtext);
      }

      dfd.then(
        setTimeout(function () {
          expect(element[0].innerHTML).toEqual(actualInnerHtml);

          // button should not have 'disabled' class after dfd.resolve()
          expect(element.hasClass('disabled')).toEqual(false);

          if (disable) {
            // button should not have 'disabled' attr after call bindXHRToButton
            expect(element.attr('disabled')).toEqual(undefined);
          }
          done();
        }, 3)
      );

      dfd.resolve();
    }

    it('bindXHRToButton() should save text of button', function (done) {
      callBindXHRToButton(done, 'HELLO WORLD');
    });

    it('bindXHRToButton() should save icon inside button', function (done) {
      callBindXHRToButton(done, '<i class="fa fa-icon"></i>');
    });

    it('bindXHRToButton() should save icon and text inside button',
      function (done) {
        callBindXHRToButton(done, '<i class="fa fa-icon"></i>My text');
      }
    );

    it('bindXHRToButton() should save text of button. Disable button',
      function (done) {
        callBindXHRToButton(done, 'HELLO WORLD', '', true);
      }
    );

    it('bindXHRToButton() should save icon inside button. New text',
      function (done) {
        callBindXHRToButton(done, '<i class="fa fa-icon"></i>', 'My new text');
      }
    );

    it('bindXHRToButton() should save text of button. New text. Disable button',
      function (done) {
        callBindXHRToButton(done, 'HELLO WORLD', '<i class="fa"></i>', true);
      }
    );
  });
});

@@ -2595,16 +2595,489 @@
 %7D%0A    );
+%0A%0A    it('bindXHRToButton() should resolve empty element',%0A      function (done) %7B%0A        var element = $('');%0A%0A        // call bindXHRToButton()%0A        Control.bindXHRToButton(dfd, element);%0A%0A        dfd.then(%0A          setTimeout(function () %7B%0A            // button should not have 'disabled' class after dfd.resolve()%0A            expect(element.hasClass('disabled')).toEqual(false);%0A            done();%0A          %7D, 3)%0A        );%0A%0A        dfd.resolve();%0A      %7D%0A    );
 %0A  %7D);%0A%7D

Update unit tests for 'bindXHRToButton' function