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

119 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-09-26 12:15:00 +00:00
import './tooltip';
import template from './tooltip.html';
describe('Component vnTooltip', () => {
let $element;
let $scope;
let $httpBackend;
let $timeout;
let controller;
2018-10-15 10:46:56 +00:00
let $tooltip;
let $controller;
let parent;
2018-09-26 12:15:00 +00:00
beforeEach(() => {
angular.mock.module('client');
});
2018-10-15 10:46:56 +00:00
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$timeout_, $compile, $document) => {
2018-09-26 12:15:00 +00:00
$scope = $rootScope.$new();
$timeout = _$timeout_;
$element = angular.element(`<div>${template}</div>`);
2018-10-15 10:46:56 +00:00
$tooltip = $compile(`<vn-tooltip class="text">test</span></vn-tooltip>`)($rootScope);
$document.find('body').append($tooltip);
2018-09-26 12:15:00 +00:00
$httpBackend = _$httpBackend_;
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
controller = _$componentController_('vnTooltip', {$element, $scope, $timeout, $transclude: null});
2018-10-15 10:46:56 +00:00
$controller = $tooltip[0].$ctrl;
parent = angular.element('<div/>')[0];
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`, () => {
$controller.show(parent);
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
let clientRect = $controller.element.getBoundingClientRect();
let style = window.getComputedStyle($controller.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');
expect(0).toBeLessThanOrEqual(clientRect.top);
expect(0).toBeLessThanOrEqual(clientRect.left);
expect($controller.window.innerHeight).toBeGreaterThan(clientRect.bottom);
expect($controller.window.innerWidth).toBeGreaterThan(clientRect.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`, () => {
$controller.show(parent);
$controller.hide();
let style = window.getComputedStyle($controller.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`, () => {
let parent = {getBoundingClientRect() {
return {top: 0, left: 0, height: 10, width: 10};
}};
$controller.show(parent);
let clientRect = $controller.element.getBoundingClientRect();
expect($controller.margin).toBeLessThan(clientRect.top);
expect($controller.margin).toEqual(clientRect.left);
});
it(`should reallocate tooltip on bottom-left`, () => {
let parent = {
getBoundingClientRect() {
return {top: $controller.window.innerHeight, left: 0, height: 10, width: 10};
}};
$controller.show(parent);
let clientRect = $controller.element.getBoundingClientRect();
expect($controller.window.innerHeight).toBeGreaterThan(clientRect.top);
expect($controller.margin).toEqual(clientRect.left);
});
it(`should reallocate tooltip on bottom-right`, () => {
let parent = {
getBoundingClientRect() {
return {top: $controller.window.innerHeight, left: $controller.window.innerWidth, height: 10, width: 10};
}};
$controller.show(parent);
let clientRect = $controller.element.getBoundingClientRect();
expect($controller.window.innerWidth).toBeGreaterThan(clientRect.left);
expect($controller.window.innerWidth - $controller.margin).toEqual(clientRect.right);
});
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
it(`should reallocate tooltip on top-right`, () => {
let parent = {
getBoundingClientRect() {
return {top: 0, left: $controller.window.innerWidth, height: 10, width: 10};
}};
$controller.show(parent);
let clientRect = $controller.element.getBoundingClientRect();
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
expect($controller.margin).toBeLessThan(clientRect.top);
expect($controller.window.innerWidth - $controller.margin).toEqual(clientRect.right);
2018-09-26 12:15:00 +00:00
});
2018-10-15 10:46:56 +00:00
it(`should reallocate tooltip on center`, () => {
let parent = {
getBoundingClientRect() {
return {top: $controller.window.innerHeight / 2, left: $controller.window.innerWidth / 2, height: 10, width: 10};
}};
$controller.show(parent);
let clientRect = $controller.element.getBoundingClientRect();
2018-09-26 12:15:00 +00:00
2018-10-15 10:46:56 +00:00
expect($controller.window.innerHeight / 2).toBeLessThan(clientRect.top);
expect($controller.window.innerWidth / 2).toBeGreaterThan(clientRect.left);
2018-09-26 12:15:00 +00:00
});
});
});