gitlab-org--gitlab-foss/spec/javascripts/droplab/plugins/input_setter_spec.js

215 lines
7 KiB
JavaScript
Raw Normal View History

2017-04-06 06:36:26 -04:00
import InputSetter from '~/droplab/plugins/input_setter';
2018-10-17 03:13:26 -04:00
describe('InputSetter', function() {
describe('init', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.config = { InputSetter: {} };
this.hook = { config: this.config };
this.inputSetter = jasmine.createSpyObj('inputSetter', ['addEvents']);
InputSetter.init.call(this.inputSetter, this.hook);
});
2018-10-17 03:13:26 -04:00
it('should set .hook', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.hook).toBe(this.hook);
});
2018-10-17 03:13:26 -04:00
it('should set .config', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.config).toBe(this.config.InputSetter);
});
2018-10-17 03:13:26 -04:00
it('should set .eventWrapper', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.eventWrapper).toEqual({});
});
2018-10-17 03:13:26 -04:00
it('should call .addEvents', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.addEvents).toHaveBeenCalled();
});
2018-10-17 03:13:26 -04:00
describe('if config.InputSetter is not set', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.config = { InputSetter: undefined };
this.hook = { config: this.config };
InputSetter.init.call(this.inputSetter, this.hook);
});
2018-10-17 03:13:26 -04:00
it('should set .config to an empty object', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.config).toEqual({});
});
2018-10-17 03:13:26 -04:00
it('should set hook.config to an empty object', function() {
2017-04-06 06:36:26 -04:00
expect(this.hook.config.InputSetter).toEqual({});
});
2018-10-17 03:13:26 -04:00
});
2017-04-06 06:36:26 -04:00
});
2018-10-17 03:13:26 -04:00
describe('addEvents', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.hook = { list: { list: jasmine.createSpyObj('list', ['addEventListener']) } };
this.inputSetter = { eventWrapper: {}, hook: this.hook, setInputs: () => {} };
InputSetter.addEvents.call(this.inputSetter);
});
2018-10-17 03:13:26 -04:00
it('should set .eventWrapper.setInputs', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.eventWrapper.setInputs).toEqual(jasmine.any(Function));
});
2018-10-17 03:13:26 -04:00
it('should call .addEventListener', function() {
expect(this.hook.list.list.addEventListener).toHaveBeenCalledWith(
'click.dl',
this.inputSetter.eventWrapper.setInputs,
);
2017-04-06 06:36:26 -04:00
});
});
2018-10-17 03:13:26 -04:00
describe('removeEvents', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.hook = { list: { list: jasmine.createSpyObj('list', ['removeEventListener']) } };
this.eventWrapper = jasmine.createSpyObj('eventWrapper', ['setInputs']);
this.inputSetter = { eventWrapper: this.eventWrapper, hook: this.hook };
InputSetter.removeEvents.call(this.inputSetter);
});
2018-10-17 03:13:26 -04:00
it('should call .removeEventListener', function() {
expect(this.hook.list.list.removeEventListener).toHaveBeenCalledWith(
'click.dl',
this.eventWrapper.setInputs,
);
2017-04-06 06:36:26 -04:00
});
});
2018-10-17 03:13:26 -04:00
describe('setInputs', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.event = { detail: { selected: {} } };
this.config = [0, 1];
this.inputSetter = { config: this.config, setInput: () => {} };
spyOn(this.inputSetter, 'setInput');
InputSetter.setInputs.call(this.inputSetter, this.event);
});
2018-10-17 03:13:26 -04:00
it('should call .setInput for each config element', function() {
2017-04-06 06:36:26 -04:00
const allArgs = this.inputSetter.setInput.calls.allArgs();
expect(allArgs.length).toEqual(2);
allArgs.forEach((args, i) => {
expect(args[0]).toBe(this.config[i]);
expect(args[1]).toBe(this.event.detail.selected);
});
});
2018-10-17 03:13:26 -04:00
describe('if config isnt an array', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.inputSetter = { config: {}, setInput: () => {} };
InputSetter.setInputs.call(this.inputSetter, this.event);
});
2018-10-17 03:13:26 -04:00
it('should set .config to an array with .config as the first element', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.config).toEqual([{}]);
});
});
});
2018-10-17 03:13:26 -04:00
describe('setInput', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.selectedItem = { getAttribute: () => {} };
2017-04-06 18:03:11 -04:00
this.input = { value: 'oldValue', tagName: 'INPUT', hasAttribute: () => {} };
2017-04-06 06:36:26 -04:00
this.config = { valueAttribute: {}, input: this.input };
this.inputSetter = { hook: { trigger: {} } };
this.newValue = 'newValue';
spyOn(this.selectedItem, 'getAttribute').and.returnValue(this.newValue);
2017-04-06 18:03:11 -04:00
spyOn(this.input, 'hasAttribute').and.returnValue(false);
2017-04-06 06:36:26 -04:00
InputSetter.setInput.call(this.inputSetter, this.config, this.selectedItem);
});
2018-10-17 03:13:26 -04:00
it('should call .getAttribute', function() {
2017-04-06 06:36:26 -04:00
expect(this.selectedItem.getAttribute).toHaveBeenCalledWith(this.config.valueAttribute);
});
2018-10-17 03:13:26 -04:00
it('should call .hasAttribute', function() {
2017-04-06 18:03:11 -04:00
expect(this.input.hasAttribute).toHaveBeenCalledWith(undefined);
});
2018-10-17 03:13:26 -04:00
it('should set the value of the input', function() {
2017-04-06 06:36:26 -04:00
expect(this.input.value).toBe(this.newValue);
2017-04-06 18:03:11 -04:00
});
2018-10-17 03:13:26 -04:00
describe('if no config.input is provided', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.config = { valueAttribute: {} };
2017-04-06 18:03:11 -04:00
this.trigger = { value: 'oldValue', tagName: 'INPUT', hasAttribute: () => {} };
2017-04-06 06:36:26 -04:00
this.inputSetter = { hook: { trigger: this.trigger } };
InputSetter.setInput.call(this.inputSetter, this.config, this.selectedItem);
});
2018-10-17 03:13:26 -04:00
it('should set the value of the hook.trigger', function() {
2017-04-06 06:36:26 -04:00
expect(this.trigger.value).toBe(this.newValue);
});
});
2018-10-17 03:13:26 -04:00
describe('if the input tag is not INPUT', function() {
beforeEach(function() {
2017-04-06 18:03:11 -04:00
this.input = { textContent: 'oldValue', tagName: 'SPAN', hasAttribute: () => {} };
2017-04-06 06:36:26 -04:00
this.config = { valueAttribute: {}, input: this.input };
InputSetter.setInput.call(this.inputSetter, this.config, this.selectedItem);
});
2018-10-17 03:13:26 -04:00
it('should set the textContent of the input', function() {
2017-04-06 18:03:11 -04:00
expect(this.input.textContent).toBe(this.newValue);
});
});
2018-10-17 03:13:26 -04:00
describe('if there is an inputAttribute', function() {
beforeEach(function() {
2017-04-06 18:03:11 -04:00
this.selectedItem = { getAttribute: () => {} };
this.input = { id: 'oldValue', hasAttribute: () => {}, setAttribute: () => {} };
this.inputSetter = { hook: { trigger: {} } };
this.newValue = 'newValue';
this.inputAttribute = 'id';
this.config = {
valueAttribute: {},
input: this.input,
inputAttribute: this.inputAttribute,
};
spyOn(this.selectedItem, 'getAttribute').and.returnValue(this.newValue);
spyOn(this.input, 'hasAttribute').and.returnValue(true);
spyOn(this.input, 'setAttribute');
InputSetter.setInput.call(this.inputSetter, this.config, this.selectedItem);
});
2018-10-17 03:13:26 -04:00
it('should call setAttribute', function() {
2017-04-06 18:03:11 -04:00
expect(this.input.setAttribute).toHaveBeenCalledWith(this.inputAttribute, this.newValue);
});
2018-10-17 03:13:26 -04:00
it('should not set the value or textContent of the input', function() {
2017-04-06 18:03:11 -04:00
expect(this.input.value).not.toBe('newValue');
expect(this.input.textContent).not.toBe('newValue');
2017-04-06 06:36:26 -04:00
});
});
});
2018-10-17 03:13:26 -04:00
describe('destroy', function() {
beforeEach(function() {
2017-04-06 06:36:26 -04:00
this.inputSetter = jasmine.createSpyObj('inputSetter', ['removeEvents']);
InputSetter.destroy.call(this.inputSetter);
});
2018-10-17 03:13:26 -04:00
it('should call .removeEvents', function() {
2017-04-06 06:36:26 -04:00
expect(this.inputSetter.removeEvents).toHaveBeenCalled();
});
});
});