2018-10-25 06:18:24 +08:00
|
|
|
/**
|
|
|
|
* Test Utility Functions
|
|
|
|
*/
|
2018-10-17 05:26:21 +08:00
|
|
|
export default class TestUtility {
|
|
|
|
/**
|
2018-10-25 06:18:24 +08:00
|
|
|
* Set's a basic DOM structure to test in
|
2018-10-17 05:26:21 +08:00
|
|
|
*/
|
|
|
|
setDOM = (divClass) => {
|
|
|
|
this.clear();
|
2019-07-14 23:27:18 +08:00
|
|
|
const wrapperDOM = document.createElement('div');
|
|
|
|
wrapperDOM.setAttribute("id", "root");
|
|
|
|
|
|
|
|
const keyboardDOM = document.createElement('div');
|
|
|
|
keyboardDOM.className = divClass || "simple-keyboard";
|
|
|
|
|
|
|
|
wrapperDOM.appendChild(keyboardDOM);
|
|
|
|
document.body.appendChild(wrapperDOM);
|
2018-10-17 05:26:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-25 06:18:24 +08:00
|
|
|
/**
|
|
|
|
* Clears DOM structure
|
|
|
|
*/
|
2018-10-17 05:26:21 +08:00
|
|
|
clear = () => {
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
}
|
|
|
|
|
2018-10-25 06:18:24 +08:00
|
|
|
/**
|
|
|
|
* Test if standard buttons respect maxLength and do input a value
|
|
|
|
*/
|
2018-10-17 05:26:21 +08:00
|
|
|
testLayoutStdButtons = (keyboard) => {
|
|
|
|
let stdBtnCount = 0;
|
|
|
|
let fullInput = '';
|
|
|
|
|
|
|
|
this.iterateButtons((button) => {
|
|
|
|
let label = button.getAttribute("data-skbtn");
|
|
|
|
|
|
|
|
if(label.includes("{"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Click all standard buttons, respects maxLength
|
|
|
|
button.onclick();
|
|
|
|
|
|
|
|
// Recording fullInput, bypasses maxLength
|
|
|
|
fullInput = keyboard.utilities.getUpdatedInput(label, fullInput, keyboard.options, null);
|
|
|
|
|
|
|
|
stdBtnCount += label.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if maxLength is respected
|
|
|
|
*/
|
|
|
|
if(
|
|
|
|
(
|
|
|
|
typeof keyboard.options.maxLength === "object" &&
|
|
|
|
keyboard.getInput().length !== keyboard.options.maxLength[keyboard.options.layoutName]
|
|
|
|
) ||
|
|
|
|
(
|
|
|
|
typeof keyboard.options.maxLength !== "object" &&
|
|
|
|
keyboard.getInput().length !== keyboard.options.maxLength
|
|
|
|
)
|
|
|
|
)
|
|
|
|
throw new Error("MAX_LENGTH_ISSUE");
|
|
|
|
else
|
|
|
|
console.log("MAX_LENGTH PASSED:", keyboard.options.layoutName, keyboard.getInput().length, keyboard.options.maxLength);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if all standard buttons are inputting something
|
|
|
|
* (Regardless of maxLength)
|
|
|
|
*/
|
|
|
|
if(stdBtnCount !== fullInput.length)
|
|
|
|
throw new Error("STANDARD_BUTTONS_ISSUE");
|
|
|
|
else
|
|
|
|
console.log("STANDARD_BUTTONS PASSED:", keyboard.options.layoutName, stdBtnCount, fullInput.length);
|
|
|
|
}
|
|
|
|
|
2018-10-25 06:18:24 +08:00
|
|
|
/**
|
|
|
|
* Test if function buttons are interactive (have an onclick)
|
|
|
|
*/
|
2018-10-17 05:26:21 +08:00
|
|
|
testLayoutFctButtons = (callback) => {
|
|
|
|
let fctBtnCount = 0;
|
|
|
|
let fctBtnHasOnclickCount = 0;
|
|
|
|
|
|
|
|
this.iterateButtons((button) => {
|
|
|
|
let label = button.getAttribute("data-skbtn");
|
|
|
|
|
|
|
|
if(!label.includes("{") && !label.includes("}"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fctBtnCount++;
|
|
|
|
|
|
|
|
if(button.onclick){
|
|
|
|
button.onclick();
|
|
|
|
fctBtnHasOnclickCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(fctBtnCount, fctBtnHasOnclickCount);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-25 06:18:24 +08:00
|
|
|
/**
|
|
|
|
* Iterates on the keyboard buttons
|
|
|
|
*/
|
2018-10-17 05:26:21 +08:00
|
|
|
iterateButtons = (callback, selector) => {
|
|
|
|
let rows = document.body.querySelector(selector || '.simple-keyboard').children;
|
|
|
|
|
|
|
|
Array.from(rows).forEach(row => {
|
|
|
|
Array.from(row.children).forEach((button) => {
|
|
|
|
callback(button);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|