Skip to content

Commit

Permalink
Add unit test #7584
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Dec 28, 2023
1 parent fcd3c82 commit 8394651
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
23 changes: 12 additions & 11 deletions src/utils/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ let _IPad = false;

export const IsMobile = _isMobile || _IPad;

export function getIsTouch() {
let _isTouch = false;

if (typeof window !== "undefined") {
_isTouch = "ontouchstart" in (<any>window) || navigator.maxTouchPoints > 0;
}

return _isTouch;
}
export var mouseInfo = {
get isTouch(): boolean {
if(this.hasMouse) return false;
return this.hasTouchEvent;
},
get hasTouchEvent(): boolean {
return typeof window !== "undefined" && "ontouchstart" in (<any>window) || navigator.maxTouchPoints > 0;
},
hasMouse: true
};

const pointerMatches = (!!matchMedia && matchMedia("(pointer:fine)")) || undefined;
const hasMouse = !!pointerMatches && !!pointerMatches.matches;
mouseInfo.hasMouse = !!pointerMatches && !!pointerMatches.matches;

export let IsTouch = !hasMouse && getIsTouch();
export let IsTouch = mouseInfo.isTouch;

//for tests
export function _setIsTouch(val: boolean): void {
Expand Down
26 changes: 19 additions & 7 deletions tests/utilstests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IAction } from "../src/actions/action";
import { defaultListCss } from "../src/list";
import { Question } from "../src/question";
import { createSvg, doKey2ClickDown, doKey2ClickUp, sanitizeEditableContent } from "../src/utils/utils";
import { getIsTouch } from "../src/utils/devices";
import { mouseInfo } from "../src/utils/devices";

export default QUnit.module("utils");
function checkSanitizer(element, text, selectionNodeIndex, selectionStart) {
Expand Down Expand Up @@ -157,11 +156,24 @@ QUnit.test(
}
);

QUnit.skip(
"utils: devices: getIsTouch",
QUnit.test(
"utils: devices: isTouch",
function (assert) {
assert.equal(getIsTouch(), false, "getIsTouch() return false for 'mouse' screens");
window["ontouchstart"] = ()=>{};
assert.equal(getIsTouch(), true, "getIsTouch() return true for 'touch' screens");
mouseInfo.hasMouse = true;
assert.equal(mouseInfo.isTouch, false, "isTouch, #1");
mouseInfo.hasMouse = false;
const hasTouchEvent = mouseInfo.hasTouchEvent;
assert.equal(mouseInfo.isTouch, hasTouchEvent, "isTouch, #2. hasTouch in window: " + hasTouchEvent);
if(!hasTouchEvent) {
window["ontouchstart"] = ()=>{};
}
mouseInfo.hasMouse = true;
assert.equal(mouseInfo.isTouch, false, "isTouch, #3");
mouseInfo.hasMouse = false;
assert.equal(mouseInfo.isTouch, true, "isTouch, #4");
mouseInfo.hasMouse = true;
if(!hasTouchEvent) {
window["ontouchstart"] = undefined;
}
}
);

0 comments on commit 8394651

Please sign in to comment.