gl_form_spec.js 3.57 KB
Newer Older
1
import autosize from 'autosize';
Phil Hughes's avatar
Phil Hughes committed
2
import GLForm from '~/gl_form';
3 4
import '~/lib/utils/text_utility';
import '~/lib/utils/common_utils';
5

Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
6 7 8 9 10 11 12 13 14
describe('GLForm', () => {
  describe('when instantiated', function () {
    beforeEach((done) => {
      this.form = $('<form class="gfm-form"><textarea class="js-gfm-input"></form>');
      this.textarea = this.form.find('textarea');
      spyOn($.prototype, 'off').and.returnValue(this.textarea);
      spyOn($.prototype, 'on').and.returnValue(this.textarea);
      spyOn($.prototype, 'css');

15
      this.glForm = new GLForm(this.form, false);
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
16 17 18 19 20 21 22 23
      setTimeout(() => {
        $.prototype.off.calls.reset();
        $.prototype.on.calls.reset();
        $.prototype.css.calls.reset();
        done();
      });
    });

24
    describe('setupAutosize', () => {
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      beforeEach((done) => {
        this.glForm.setupAutosize();
        setTimeout(() => {
          done();
        });
      });

      it('should register an autosize event handler on the textarea', () => {
        expect($.prototype.off).toHaveBeenCalledWith('autosize:resized');
        expect($.prototype.on).toHaveBeenCalledWith('autosize:resized', jasmine.any(Function));
      });

      it('should register a mouseup event handler on the textarea', () => {
        expect($.prototype.off).toHaveBeenCalledWith('mouseup.autosize');
        expect($.prototype.on).toHaveBeenCalledWith('mouseup.autosize', jasmine.any(Function));
      });

      it('should set the resize css property to vertical', () => {
        expect($.prototype.css).toHaveBeenCalledWith('resize', 'vertical');
      });
    });

47
    describe('setHeightData', () => {
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      beforeEach(() => {
        spyOn($.prototype, 'data');
        spyOn($.prototype, 'outerHeight').and.returnValue(200);
        this.glForm.setHeightData();
      });

      it('should set the height data attribute', () => {
        expect($.prototype.data).toHaveBeenCalledWith('height', 200);
      });

      it('should call outerHeight', () => {
        expect($.prototype.outerHeight).toHaveBeenCalled();
      });
    });

63
    describe('destroyAutosize', () => {
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
64 65 66 67 68
      describe('when called', () => {
        beforeEach(() => {
          spyOn($.prototype, 'data');
          spyOn($.prototype, 'outerHeight').and.returnValue(200);
          spyOn(window, 'outerHeight').and.returnValue(400);
69
          spyOn(autosize, 'destroy');
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
70 71 72 73 74 75 76 77 78 79 80 81 82

          this.glForm.destroyAutosize();
        });

        it('should call outerHeight', () => {
          expect($.prototype.outerHeight).toHaveBeenCalled();
        });

        it('should get data-height attribute', () => {
          expect($.prototype.data).toHaveBeenCalledWith('height');
        });

        it('should call autosize destroy', () => {
83
          expect(autosize.destroy).toHaveBeenCalledWith(this.textarea);
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        });

        it('should set the data-height attribute', () => {
          expect($.prototype.data).toHaveBeenCalledWith('height', 200);
        });

        it('should set the outerHeight', () => {
          expect($.prototype.outerHeight).toHaveBeenCalledWith(200);
        });

        it('should set the css', () => {
          expect($.prototype.css).toHaveBeenCalledWith('max-height', window.outerHeight);
        });
      });

      it('should return undefined if the data-height equals the outerHeight', () => {
        spyOn($.prototype, 'outerHeight').and.returnValue(200);
        spyOn($.prototype, 'data').and.returnValue(200);
102
        spyOn(autosize, 'destroy');
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
103
        expect(this.glForm.destroyAutosize()).toBeUndefined();
104
        expect(autosize.destroy).not.toHaveBeenCalled();
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
105 106 107 108
      });
    });
  });
});