salix/client/core/src/components/dialog/dialog.spec.js

177 lines
7.1 KiB
JavaScript
Raw Normal View History

2017-10-05 12:28:57 +00:00
describe('Component vnDialog', () => {
let $componentController;
let $element;
let controller;
2017-10-05 12:28:57 +00:00
beforeEach(() => {
angular.mock.module('client');
});
beforeEach(angular.mock.inject(_$componentController_ => {
$componentController = _$componentController_;
$element = angular.element('<div></div>');
controller = $componentController('vnDialog', {$element});
2017-10-05 12:28:57 +00:00
}));
describe('show()', () => {
it(`should define keypressHandler function, call addEventListener function and define element.style.display to block then call onOpen function`, () => {
window.innerHeight = 600;
window.innerWidth = 800;
controller.onOpen = () => {};
controller.dialog = {style: {}, offsetWidth: 780, offsetHeight: 581};
2017-10-05 12:28:57 +00:00
spyOn(controller.document, 'addEventListener');
spyOn(controller, 'onOpen');
controller.show();
expect(controller.keypressHandler).toBeDefined();
expect(controller.document.addEventListener).toHaveBeenCalledWith('keypress', controller.keypressHandler);
expect(controller.element.style.display).toEqual('block');
expect(controller.onOpen).toHaveBeenCalledWith();
});
it(`should define keypressHandler function, call addEventListener function and define element.style.display to block and never call onOpen function`, () => {
window.innerHeight = 600;
window.innerWidth = 800;
controller.dialog = {style: {}, offsetWidth: 781, offsetHeight: 581};
2017-10-05 12:28:57 +00:00
spyOn(controller.document, 'addEventListener');
controller.show();
expect(controller.keypressHandler).toBeDefined();
expect(controller.document.addEventListener).toHaveBeenCalledWith('keypress', controller.keypressHandler);
expect(controller.element.style.display).toEqual('block');
expect(controller.onOpen).not.toBeDefined();
});
});
describe('hide()', () => {
it(`should call fireResponse() and realHide()`, () => {
spyOn(controller, 'fireResponse');
spyOn(controller, 'realHide');
controller.hide();
expect(controller.fireResponse).toHaveBeenCalledWith();
expect(controller.realHide).toHaveBeenCalledWith();
});
});
describe('fireResponse()', () => {
it(`should return cancel as false`, () => {
let result = controller.fireResponse('I am the answer!');
expect(controller.onResponse).not.toBeDefined();
expect(result).toEqual(false);
});
it(`should return onResponse()`, () => {
let text = 'I am the answer!';
controller.onResponse = () => {
2017-10-05 12:28:57 +00:00
return {response: text};
};
2017-10-05 12:28:57 +00:00
let result = controller.fireResponse(text);
expect(result.response).toEqual(text);
});
});
describe('realHide()', () => {
it(`should set element.style.display and lastEvent properties and call removeEvenListener()`, () => {
spyOn(controller.document, 'removeEventListener');
expect(controller.element.style.display).not.toEqual('none');
expect(controller.lastEvent).not.toBeDefined();
controller.realHide();
expect(controller.element.style.display).toEqual('none');
expect(controller.document.removeEventListener).toHaveBeenCalledWith('keypress', controller.keypressHandler);
expect(controller.lastEvent).toEqual(null);
});
});
describe('onButtonClick()', () => {
it(`should call realHide if cancel isn't false`, () => {
controller.element = document.createElement('div');
controller.element.className = 'tpl-buttons';
let childElement = document.createElement('div');
childElement.className = 'button-bar';
controller.element.appendChild(childElement);
let event = {target: controller.element, attribute: true};
spyOn(controller, 'realHide');
spyOn(controller, 'fireResponse').and.returnValue(true);
controller.onButtonClick(event);
expect(controller.realHide).toHaveBeenCalledWith();
});
it(`should call fireResponse with the value of response`, () => {
controller.element = document.createElement('div');
controller.element.className = 'tpl-buttons';
let childElement = document.createElement('div');
childElement.className = 'button-bar';
controller.element.appendChild(childElement);
let attribute = document.createAttribute('response');
attribute.value = 'I am the response!';
controller.element.setAttributeNode(attribute);
spyOn(controller, 'fireResponse');
2017-10-06 13:36:25 +00:00
let event = {target: controller.element};
controller.onButtonClick(event);
expect(controller.fireResponse).toHaveBeenCalledWith('I am the response!');
});
});
describe('onDialogMouseDown()', () => {
it(`should set controller's lastEvent property`, () => {
controller.element = document.createElement('div');
let event = {target: controller.element};
controller.onDialogMouseDown(event);
expect(controller.lastEvent).toEqual(event);
});
});
describe('onBackgroundMouseDown()', () => {
it(`shouldn't call hide() function as event equals lastEvent`, () => {
controller.element = document.createElement('div');
let event = {target: controller.element};
controller.lastEvent = event;
spyOn(controller, 'hide');
controller.onBackgroundMouseDown(event);
expect(controller.hide).not.toHaveBeenCalledWith();
});
it(`should call hide() function as event doesn't equal lastEvent`, () => {
controller.element = document.createElement('div');
let event = {target: controller.element};
controller.lastEvent = event;
controller.lastEvent = 'the singularity event!';
spyOn(controller, 'hide');
controller.onBackgroundMouseDown(event);
expect(controller.hide).toHaveBeenCalledWith();
});
});
describe('onKeypress()', () => {
it(`should call hide() if the key pressed equal the code 27`, () => {
controller.element = document.createElement('div');
let event = {target: controller.element};
event.keyCode = 27;
spyOn(controller, 'hide');
controller.onKeypress(event);
expect(controller.hide).toHaveBeenCalledWith();
});
it(`should't call hide() as the key pressed equal the code 999`, () => {
controller.element = document.createElement('div');
let event = {target: controller.element};
event.keyCode = 999;
spyOn(controller, 'hide');
controller.onKeypress(event);
expect(controller.hide).not.toHaveBeenCalledWith();
});
});
2017-10-05 12:28:57 +00:00
});