BigW Consortium Gitlab

bind_in_out_spec.js 5.32 KB
Newer Older
Z.J. van de Weg committed
1 2
import BindInOut from '~/behaviors/bind_in_out';
import ClassSpecHelper from '../helpers/class_spec_helper';
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

describe('BindInOut', function () {
  describe('.constructor', function () {
    beforeEach(function () {
      this.in = {};
      this.out = {};

      this.bindInOut = new BindInOut(this.in, this.out);
    });

    it('should set .in', function () {
      expect(this.bindInOut.in).toBe(this.in);
    });

    it('should set .out', function () {
      expect(this.bindInOut.out).toBe(this.out);
    });

    it('should set .eventWrapper', function () {
      expect(this.bindInOut.eventWrapper).toEqual({});
    });

    describe('if .in is an input', function () {
      beforeEach(function () {
        this.bindInOut = new BindInOut({ tagName: 'INPUT' });
      });

      it('should set .eventType to keyup ', function () {
        expect(this.bindInOut.eventType).toEqual('keyup');
      });
    });

    describe('if .in is a textarea', function () {
      beforeEach(function () {
        this.bindInOut = new BindInOut({ tagName: 'TEXTAREA' });
      });

      it('should set .eventType to keyup ', function () {
        expect(this.bindInOut.eventType).toEqual('keyup');
      });
    });

    describe('if .in is not an input or textarea', function () {
      beforeEach(function () {
        this.bindInOut = new BindInOut({ tagName: 'SELECT' });
      });

      it('should set .eventType to change ', function () {
        expect(this.bindInOut.eventType).toEqual('change');
      });
    });
  });

  describe('.addEvents', function () {
    beforeEach(function () {
      this.in = jasmine.createSpyObj('in', ['addEventListener']);

      this.bindInOut = new BindInOut(this.in);

      this.addEvents = this.bindInOut.addEvents();
    });

    it('should set .eventWrapper.updateOut', function () {
      expect(this.bindInOut.eventWrapper.updateOut).toEqual(jasmine.any(Function));
    });

    it('should call .addEventListener', function () {
      expect(this.in.addEventListener)
        .toHaveBeenCalledWith(
          this.bindInOut.eventType,
          this.bindInOut.eventWrapper.updateOut,
        );
    });

    it('should return the instance', function () {
      expect(this.addEvents).toBe(this.bindInOut);
    });
  });

  describe('.updateOut', function () {
    beforeEach(function () {
      this.in = { value: 'the-value' };
      this.out = { textContent: 'not-the-value' };

      this.bindInOut = new BindInOut(this.in, this.out);

      this.updateOut = this.bindInOut.updateOut();
    });

    it('should set .out.textContent to .in.value', function () {
      expect(this.out.textContent).toBe(this.in.value);
    });

    it('should return the instance', function () {
      expect(this.updateOut).toBe(this.bindInOut);
    });
  });

  describe('.removeEvents', function () {
    beforeEach(function () {
      this.in = jasmine.createSpyObj('in', ['removeEventListener']);
      this.updateOut = () => {};

      this.bindInOut = new BindInOut(this.in);
      this.bindInOut.eventWrapper.updateOut = this.updateOut;

      this.removeEvents = this.bindInOut.removeEvents();
    });

    it('should call .removeEventListener', function () {
      expect(this.in.removeEventListener)
        .toHaveBeenCalledWith(
          this.bindInOut.eventType,
          this.updateOut,
        );
    });

    it('should return the instance', function () {
      expect(this.removeEvents).toBe(this.bindInOut);
    });
  });

  describe('.initAll', function () {
    beforeEach(function () {
      this.ins = [0, 1, 2];
      this.instances = [];

      spyOn(document, 'querySelectorAll').and.returnValue(this.ins);
      spyOn(Array.prototype, 'map').and.callThrough();
      spyOn(BindInOut, 'init');

      this.initAll = BindInOut.initAll();
    });

    ClassSpecHelper.itShouldBeAStaticMethod(BindInOut, 'initAll');

    it('should call .querySelectorAll', function () {
      expect(document.querySelectorAll).toHaveBeenCalledWith('*[data-bind-in]');
    });

    it('should call .map', function () {
      expect(Array.prototype.map).toHaveBeenCalledWith(jasmine.any(Function));
    });

    it('should call .init for each element', function () {
      expect(BindInOut.init.calls.count()).toEqual(3);
    });

    it('should return an array of instances', function () {
      expect(this.initAll).toEqual(jasmine.any(Array));
    });
  });

  describe('.init', function () {
    beforeEach(function () {
      spyOn(BindInOut.prototype, 'addEvents').and.callFake(function () { return this; });
      spyOn(BindInOut.prototype, 'updateOut').and.callFake(function () { return this; });

      this.init = BindInOut.init({}, {});
    });

    ClassSpecHelper.itShouldBeAStaticMethod(BindInOut, 'init');

    it('should call .addEvents', function () {
      expect(BindInOut.prototype.addEvents).toHaveBeenCalled();
    });

    it('should call .updateOut', function () {
      expect(BindInOut.prototype.updateOut).toHaveBeenCalled();
    });

    describe('if no anOut is provided', function () {
      beforeEach(function () {
        this.anIn = { dataset: { bindIn: 'the-data-bind-in' } };

        spyOn(document, 'querySelector');

        BindInOut.init(this.anIn);
      });

      it('should call .querySelector', function () {
        expect(document.querySelector)
          .toHaveBeenCalledWith(`*[data-bind-out="${this.anIn.dataset.bindIn}"]`);
      });
    });
  });
});