Add tests for utils/Mouse
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* @license MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
import jsdom = require('jsdom');
|
||||||
|
import { assert } from 'chai';
|
||||||
|
import { getCoords } from './Mouse';
|
||||||
|
import { MockCharMeasure } from './TestUtils.test';
|
||||||
|
|
||||||
|
const CHAR_WIDTH = 10;
|
||||||
|
const CHAR_HEIGHT = 20;
|
||||||
|
|
||||||
|
describe('getCoords', () => {
|
||||||
|
let dom: jsdom.JSDOM;
|
||||||
|
let window: Window;
|
||||||
|
let document: Document;
|
||||||
|
|
||||||
|
let charMeasure: MockCharMeasure;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
dom = new jsdom.JSDOM('');
|
||||||
|
window = dom.window;
|
||||||
|
document = window.document;
|
||||||
|
charMeasure = new MockCharMeasure();
|
||||||
|
charMeasure.width = CHAR_WIDTH;
|
||||||
|
charMeasure.height = CHAR_HEIGHT;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when charMeasure is not initialized', () => {
|
||||||
|
it('should return null', () => {
|
||||||
|
charMeasure = new MockCharMeasure();
|
||||||
|
assert.equal(getCoords({ pageX: 0, pageY: 0 }, document.createElement('div'), charMeasure, 1, 10, 10), null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when pageX/pageY are not supported', () => {
|
||||||
|
it('should return null', () => {
|
||||||
|
assert.equal(getCoords({ pageX: undefined, pageY: undefined }, document.createElement('div'), charMeasure, 1, 10, 10), null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the cell that was clicked', () => {
|
||||||
|
let coords: [number, number];
|
||||||
|
coords = getCoords({ pageX: CHAR_WIDTH / 2, pageY: CHAR_HEIGHT / 2 }, document.createElement('div'), charMeasure, 1, 10, 10);
|
||||||
|
assert.deepEqual(coords, [1, 1]);
|
||||||
|
coords = getCoords({ pageX: CHAR_WIDTH, pageY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 1, 10, 10);
|
||||||
|
assert.deepEqual(coords, [1, 1]);
|
||||||
|
coords = getCoords({ pageX: CHAR_WIDTH, pageY: CHAR_HEIGHT + 1 }, document.createElement('div'), charMeasure, 1, 10, 10);
|
||||||
|
assert.deepEqual(coords, [1, 2]);
|
||||||
|
coords = getCoords({ pageX: CHAR_WIDTH + 1, pageY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 1, 10, 10);
|
||||||
|
assert.deepEqual(coords, [2, 1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ensure the coordinates are returned within the terminal bounds', () => {
|
||||||
|
let coords: [number, number];
|
||||||
|
coords = getCoords({ pageX: -1, pageY: -1 }, document.createElement('div'), charMeasure, 1, 10, 10);
|
||||||
|
assert.deepEqual(coords, [1, 1]);
|
||||||
|
// Event are double the cols/rows
|
||||||
|
coords = getCoords({ pageX: CHAR_WIDTH * 20, pageY: CHAR_HEIGHT * 20 }, document.createElement('div'), charMeasure, 1, 10, 10);
|
||||||
|
assert.deepEqual(coords, [11, 11], 'coordinates should never come back as larger than the terminal');
|
||||||
|
});
|
||||||
|
});
|
||||||
+3
-3
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import { ICharMeasure } from '../Interfaces';
|
import { ICharMeasure } from '../Interfaces';
|
||||||
|
|
||||||
export function getCoordsRelativeToElement(event: MouseEvent, element: HTMLElement): [number, number] {
|
export function getCoordsRelativeToElement(event: {pageX: number, pageY: number}, element: HTMLElement): [number, number] {
|
||||||
// Ignore browsers that don't support MouseEvent.pageX
|
// Ignore browsers that don't support MouseEvent.pageX
|
||||||
if (event.pageX == null) {
|
if (event.pageX == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -15,7 +15,7 @@ export function getCoordsRelativeToElement(event: MouseEvent, element: HTMLEleme
|
|||||||
|
|
||||||
// Converts the coordinates from being relative to the document to being
|
// Converts the coordinates from being relative to the document to being
|
||||||
// relative to the terminal.
|
// relative to the terminal.
|
||||||
while (element && element !== self.document.documentElement) {
|
while (element) {
|
||||||
x -= element.offsetLeft;
|
x -= element.offsetLeft;
|
||||||
y -= element.offsetTop;
|
y -= element.offsetTop;
|
||||||
element = 'offsetParent' in element ? <HTMLElement>element.offsetParent : <HTMLElement>element.parentElement;
|
element = 'offsetParent' in element ? <HTMLElement>element.offsetParent : <HTMLElement>element.parentElement;
|
||||||
@@ -36,7 +36,7 @@ export function getCoordsRelativeToElement(event: MouseEvent, element: HTMLEleme
|
|||||||
* apply an offset to the x value such that the left half of the cell will
|
* apply an offset to the x value such that the left half of the cell will
|
||||||
* select that cell and the right half will select the next cell.
|
* select that cell and the right half will select the next cell.
|
||||||
*/
|
*/
|
||||||
export function getCoords(event: MouseEvent, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number] {
|
export function getCoords(event: {pageX: number, pageY: number}, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number] {
|
||||||
// Coordinates cannot be measured if charMeasure has not been initialized
|
// Coordinates cannot be measured if charMeasure has not been initialized
|
||||||
if (!charMeasure.width || !charMeasure.height) {
|
if (!charMeasure.width || !charMeasure.height) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -68,6 +68,14 @@ export class MockTerminal implements ITerminal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class MockCharMeasure implements ICharMeasure {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
measure(options: ITerminalOptions): void {
|
||||||
|
throw new Error('Method not implemented.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class MockInputHandlingTerminal implements IInputHandlingTerminal {
|
export class MockInputHandlingTerminal implements IInputHandlingTerminal {
|
||||||
element: HTMLElement;
|
element: HTMLElement;
|
||||||
options: ITerminalOptions = {};
|
options: ITerminalOptions = {};
|
||||||
|
|||||||
Reference in New Issue
Block a user