gitlab-org--gitlab-foss/spec/javascripts/behaviors/bind_in_out_spec.js

193 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-03-06 08:06:33 -05:00
import BindInOut from '~/behaviors/bind_in_out';
import ClassSpecHelper from '../helpers/class_spec_helper';
2017-02-20 08:51:47 -05:00
2018-10-17 03:13:26 -04:00
describe('BindInOut', function() {
describe('constructor', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.in = {};
this.out = {};
this.bindInOut = new BindInOut(this.in, this.out);
});
2018-10-17 03:13:26 -04:00
it('should set .in', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.in).toBe(this.in);
});
2018-10-17 03:13:26 -04:00
it('should set .out', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.out).toBe(this.out);
});
2018-10-17 03:13:26 -04:00
it('should set .eventWrapper', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.eventWrapper).toEqual({});
});
2018-10-17 03:13:26 -04:00
describe('if .in is an input', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.bindInOut = new BindInOut({ tagName: 'INPUT' });
});
2018-10-17 03:13:26 -04:00
it('should set .eventType to keyup ', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.eventType).toEqual('keyup');
});
});
2018-10-17 03:13:26 -04:00
describe('if .in is a textarea', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.bindInOut = new BindInOut({ tagName: 'TEXTAREA' });
});
2018-10-17 03:13:26 -04:00
it('should set .eventType to keyup ', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.eventType).toEqual('keyup');
});
});
2018-10-17 03:13:26 -04:00
describe('if .in is not an input or textarea', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.bindInOut = new BindInOut({ tagName: 'SELECT' });
});
2018-10-17 03:13:26 -04:00
it('should set .eventType to change ', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.eventType).toEqual('change');
});
});
});
2018-10-17 03:13:26 -04:00
describe('addEvents', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.in = jasmine.createSpyObj('in', ['addEventListener']);
this.bindInOut = new BindInOut(this.in);
this.addEvents = this.bindInOut.addEvents();
});
2018-10-17 03:13:26 -04:00
it('should set .eventWrapper.updateOut', function() {
2017-02-20 08:51:47 -05:00
expect(this.bindInOut.eventWrapper.updateOut).toEqual(jasmine.any(Function));
});
2018-10-17 03:13:26 -04:00
it('should call .addEventListener', function() {
expect(this.in.addEventListener).toHaveBeenCalledWith(
this.bindInOut.eventType,
this.bindInOut.eventWrapper.updateOut,
);
2017-02-20 08:51:47 -05:00
});
2018-10-17 03:13:26 -04:00
it('should return the instance', function() {
2017-02-20 08:51:47 -05:00
expect(this.addEvents).toBe(this.bindInOut);
});
});
2018-10-17 03:13:26 -04:00
describe('updateOut', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.in = { value: 'the-value' };
this.out = { textContent: 'not-the-value' };
this.bindInOut = new BindInOut(this.in, this.out);
this.updateOut = this.bindInOut.updateOut();
});
2018-10-17 03:13:26 -04:00
it('should set .out.textContent to .in.value', function() {
2017-02-20 08:51:47 -05:00
expect(this.out.textContent).toBe(this.in.value);
});
2018-10-17 03:13:26 -04:00
it('should return the instance', function() {
2017-02-20 08:51:47 -05:00
expect(this.updateOut).toBe(this.bindInOut);
});
});
2018-10-17 03:13:26 -04:00
describe('removeEvents', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
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();
});
2018-10-17 03:13:26 -04:00
it('should call .removeEventListener', function() {
expect(this.in.removeEventListener).toHaveBeenCalledWith(
this.bindInOut.eventType,
this.updateOut,
);
2017-02-20 08:51:47 -05:00
});
2018-10-17 03:13:26 -04:00
it('should return the instance', function() {
2017-02-20 08:51:47 -05:00
expect(this.removeEvents).toBe(this.bindInOut);
});
});
2018-10-17 03:13:26 -04:00
describe('initAll', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
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');
2018-10-17 03:13:26 -04:00
it('should call .querySelectorAll', function() {
2017-02-20 08:51:47 -05:00
expect(document.querySelectorAll).toHaveBeenCalledWith('*[data-bind-in]');
});
2018-10-17 03:13:26 -04:00
it('should call .map', function() {
2017-02-20 08:51:47 -05:00
expect(Array.prototype.map).toHaveBeenCalledWith(jasmine.any(Function));
});
2018-10-17 03:13:26 -04:00
it('should call .init for each element', function() {
2017-02-20 08:51:47 -05:00
expect(BindInOut.init.calls.count()).toEqual(3);
});
2018-10-17 03:13:26 -04:00
it('should return an array of instances', function() {
2017-02-20 08:51:47 -05:00
expect(this.initAll).toEqual(jasmine.any(Array));
});
});
2018-10-17 03:13:26 -04:00
describe('init', function() {
beforeEach(function() {
spyOn(BindInOut.prototype, 'addEvents').and.callFake(function() {
return this;
});
spyOn(BindInOut.prototype, 'updateOut').and.callFake(function() {
return this;
});
2017-02-20 08:51:47 -05:00
this.init = BindInOut.init({}, {});
});
ClassSpecHelper.itShouldBeAStaticMethod(BindInOut, 'init');
2018-10-17 03:13:26 -04:00
it('should call .addEvents', function() {
2017-02-20 08:51:47 -05:00
expect(BindInOut.prototype.addEvents).toHaveBeenCalled();
});
2018-10-17 03:13:26 -04:00
it('should call .updateOut', function() {
2017-02-20 08:51:47 -05:00
expect(BindInOut.prototype.updateOut).toHaveBeenCalled();
});
2018-10-17 03:13:26 -04:00
describe('if no anOut is provided', function() {
beforeEach(function() {
2017-02-20 08:51:47 -05:00
this.anIn = { dataset: { bindIn: 'the-data-bind-in' } };
spyOn(document, 'querySelector');
BindInOut.init(this.anIn);
});
2018-10-17 03:13:26 -04:00
it('should call .querySelector', function() {
expect(document.querySelector).toHaveBeenCalledWith(
`*[data-bind-out="${this.anIn.dataset.bindIn}"]`,
);
2017-02-20 08:51:47 -05:00
});
});
});
});