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

119 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-09-26 12:15:00 +00:00
import './tooltip';
describe('Component vnTooltip', () => {
let $element;
2018-10-19 07:33:12 +00:00
let $ctrl;
let $parent;
let element;
let window;
beforeEach(ngModule('vnCore'));
beforeEach(inject(($compile, $rootScope, $document) => {
$element = $compile(`<vn-tooltip class="text">test</span></vn-tooltip>`)($rootScope);
$document.find('body').append($element);
$ctrl = $element.controller('vnTooltip');
element = $element[0];
window = $ctrl.window;
$parent = angular.element('<div/>');
$parent.css({
backgroundColor: 'red',
position: 'absolute',
width: '10px',
height: '10px',
top: '0',
left: '0'
});
$document.find('body').append($parent);
2018-09-26 12:15:00 +00:00
}));
2018-10-19 07:33:12 +00:00
afterEach(() => {
$element.remove();
$parent.remove();
});
2018-09-26 12:15:00 +00:00
describe('show()', () => {
2018-10-15 10:46:56 +00:00
it(`should check that tooltip is visible into the screen`, () => {
2018-10-19 07:33:12 +00:00
$ctrl.show($parent[0]);
2018-09-26 12:15:00 +00:00
2018-10-19 07:33:12 +00:00
let rect = element.getBoundingClientRect();
let style = window.getComputedStyle(element);
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
expect(style.visibility).toEqual('visible');
expect(style.display).not.toEqual('none');
2018-10-19 07:33:12 +00:00
expect(0).toBeLessThanOrEqual(rect.top);
expect(0).toBeLessThanOrEqual(rect.left);
expect(window.innerHeight).toBeGreaterThan(rect.bottom);
expect(window.innerWidth).toBeGreaterThan(rect.right);
2018-09-26 12:15:00 +00:00
});
});
describe('hide()', () => {
2018-10-15 10:46:56 +00:00
it(`should check that tooltip is not visible`, () => {
2018-10-19 07:33:12 +00:00
$ctrl.show($parent[0]);
$ctrl.hide();
let style = window.getComputedStyle(element);
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
expect(style.display).toEqual('none');
2018-09-26 12:15:00 +00:00
});
});
2018-10-15 10:46:56 +00:00
describe('relocate()', () => {
it(`should reallocate tooltip on top-left`, () => {
2018-10-19 07:33:12 +00:00
$ctrl.show($parent[0]);
let rect = element.getBoundingClientRect();
expect($ctrl.margin).toBeLessThan(rect.top);
expect($ctrl.margin).toEqual(rect.left);
2018-10-15 10:46:56 +00:00
});
it(`should reallocate tooltip on bottom-left`, () => {
2018-10-19 07:33:12 +00:00
$parent.css({
top: `${window.innerHeight}px`
});
$ctrl.show($parent[0]);
let rect = element.getBoundingClientRect();
expect(window.innerHeight).toBeGreaterThan(rect.top);
expect($ctrl.margin).toEqual(rect.left);
2018-10-15 10:46:56 +00:00
});
it(`should reallocate tooltip on bottom-right`, () => {
2018-10-19 07:33:12 +00:00
$parent.css({
top: `${window.innerHeight}px`,
left: `${window.innerWidth}px`
});
$ctrl.show($parent[0]);
let rect = element.getBoundingClientRect();
expect(window.innerWidth).toBeGreaterThan(rect.left);
expect(window.innerWidth - $ctrl.margin).toEqual(rect.right);
2018-10-15 10:46:56 +00:00
});
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
it(`should reallocate tooltip on top-right`, () => {
2018-10-19 07:33:12 +00:00
$parent.css({
left: `${window.innerWidth}px`
});
$ctrl.show($parent[0]);
let rect = element.getBoundingClientRect();
expect($ctrl.margin).toBeLessThan(rect.top);
expect(window.innerWidth - $ctrl.margin).toEqual(rect.right);
2018-09-26 12:15:00 +00:00
});
2018-10-15 10:46:56 +00:00
it(`should reallocate tooltip on center`, () => {
2018-10-19 07:33:12 +00:00
$parent.css({
top: `${window.window.innerHeight / 2}px`,
left: `${window.window.innerWidth / 2}px`
});
$ctrl.show($parent[0]);
let rect = element.getBoundingClientRect();
expect(window.innerHeight / 2).toBeLessThan(rect.top);
expect(window.innerWidth / 2).toBeGreaterThan(rect.left);
2018-09-26 12:15:00 +00:00
});
});
});