@testing-library/user-event
Version:
Fire events the same way the user does
155 lines (150 loc) • 7.46 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var buttons = require('./buttons.js');
var device = require('./device.js');
var mouse = require('./mouse.js');
var pointer = require('./pointer.js');
var _registry, _k;
class PointerHost {
isKeyPressed(keyDef) {
return this.devices.get(keyDef.pointerType).isPressed(keyDef);
}
async press(config, keyDef, position) {
const pointerName = this.getPointerName(keyDef);
const pointer = keyDef.pointerType === 'touch' ? this.pointers.new(pointerName, keyDef).init(config, position) : this.pointers.get(pointerName);
// TODO: deprecate the following implicit setting of position
pointer.position = position;
if (pointer.pointerType !== 'touch') {
this.mouse.position = position;
}
this.devices.get(keyDef.pointerType).addPressed(keyDef);
this.buttons.down(keyDef);
pointer.down(config, keyDef);
if (pointer.pointerType !== 'touch' && !pointer.isPrevented) {
this.mouse.down(config, keyDef, pointer);
}
}
async move(config, pointerName, position) {
const pointer = this.pointers.get(pointerName);
// In (some?) browsers this order of events can be observed.
// This interweaving of events is probably unnecessary.
// While the order of mouse (or pointer) events is defined per spec,
// the order in which they interweave/follow on a user interaction depends on the implementation.
const pointermove = pointer.move(config, position);
const mousemove = pointer.pointerType === 'touch' || pointer.isPrevented && pointer.isDown ? undefined : this.mouse.move(config, position);
pointermove === null || pointermove === void 0 ? void 0 : pointermove.leave();
mousemove === null || mousemove === void 0 ? void 0 : mousemove.leave();
pointermove === null || pointermove === void 0 ? void 0 : pointermove.enter();
mousemove === null || mousemove === void 0 ? void 0 : mousemove.enter();
pointermove === null || pointermove === void 0 ? void 0 : pointermove.move();
mousemove === null || mousemove === void 0 ? void 0 : mousemove.move();
}
async release(config, keyDef, position) {
const device = this.devices.get(keyDef.pointerType);
device.removePressed(keyDef);
this.buttons.up(keyDef);
const pointer = this.pointers.get(this.getPointerName(keyDef));
// TODO: deprecate the following implicit setting of position
pointer.position = position;
if (pointer.pointerType !== 'touch') {
this.mouse.position = position;
}
if (device.countPressed === 0) {
pointer.up(config, keyDef);
}
if (pointer.pointerType === 'touch') {
pointer.release(config);
}
if (!pointer.isPrevented) {
if (pointer.pointerType === 'touch' && !pointer.isMultitouch) {
const mousemove = this.mouse.move(config, pointer.position);
mousemove === null || mousemove === void 0 ? void 0 : mousemove.leave();
mousemove === null || mousemove === void 0 ? void 0 : mousemove.enter();
mousemove === null || mousemove === void 0 ? void 0 : mousemove.move();
this.mouse.down(config, keyDef, pointer);
}
if (!pointer.isMultitouch) {
const mousemove1 = this.mouse.move(config, pointer.position);
mousemove1 === null || mousemove1 === void 0 ? void 0 : mousemove1.leave();
mousemove1 === null || mousemove1 === void 0 ? void 0 : mousemove1.enter();
mousemove1 === null || mousemove1 === void 0 ? void 0 : mousemove1.move();
this.mouse.up(config, keyDef, pointer);
}
}
}
getPointerName(keyDef) {
return keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType;
}
getPreviousPosition(pointerName) {
return this.pointers.has(pointerName) ? this.pointers.get(pointerName).position : undefined;
}
resetClickCount() {
this.mouse.resetClickCount();
}
getMouseTarget(config) {
var _target;
return (_target = this.mouse.position.target) !== null && _target !== void 0 ? _target : config.document.body;
}
setMousePosition(position) {
this.mouse.position = position;
this.pointers.get('mouse').position = position;
}
constructor(system){
this.devices = new class {
get(k) {
var ref;
(ref = (_registry = this.registry)[_k = k]) !== null && ref !== void 0 ? ref : _registry[_k] = new device.Device();
return this.registry[k];
}
constructor(){
this.registry = {};
}
}();
this.pointers = new class {
new(pointerName, keyDef) {
const isPrimary = keyDef.pointerType !== 'touch' || !Object.values(this.registry).some((p)=>p.pointerType === 'touch' && !p.isCancelled);
if (!isPrimary) {
Object.values(this.registry).forEach((p)=>{
if (p.pointerType === keyDef.pointerType && !p.isCancelled) {
p.isMultitouch = true;
}
});
}
this.registry[pointerName] = new pointer.Pointer({
pointerId: this.nextId++,
pointerType: keyDef.pointerType,
isPrimary
});
return this.registry[pointerName];
}
get(pointerName) {
if (!this.has(pointerName)) {
throw new Error(`Trying to access pointer "${pointerName}" which does not exist.`);
}
return this.registry[pointerName];
}
has(pointerName) {
return pointerName in this.registry;
}
constructor(){
this.registry = {
mouse: new pointer.Pointer({
pointerId: 1,
pointerType: 'mouse',
isPrimary: true
})
};
this.nextId = 2;
}
}();
this.system = system;
this.buttons = new buttons.Buttons();
this.mouse = new mouse.Mouse();
}
}
function isDifferentPointerPosition(positionA, positionB) {
var ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
return positionA.target !== positionB.target || ((ref = positionA.coords) === null || ref === void 0 ? void 0 : ref.x) !== ((ref1 = positionB.coords) === null || ref1 === void 0 ? void 0 : ref1.y) || ((ref2 = positionA.coords) === null || ref2 === void 0 ? void 0 : ref2.y) !== ((ref3 = positionB.coords) === null || ref3 === void 0 ? void 0 : ref3.y) || ((ref4 = positionA.caret) === null || ref4 === void 0 ? void 0 : ref4.node) !== ((ref5 = positionB.caret) === null || ref5 === void 0 ? void 0 : ref5.node) || ((ref6 = positionA.caret) === null || ref6 === void 0 ? void 0 : ref6.offset) !== ((ref7 = positionB.caret) === null || ref7 === void 0 ? void 0 : ref7.offset);
}
exports.PointerHost = PointerHost;
exports.isDifferentPointerPosition = isDifferentPointerPosition;