Compare commits

...

4 Commits

4 changed files with 36 additions and 21 deletions

View File

@ -113,7 +113,9 @@
### open
- custom validation not triggering error message nor visible styles
- außerdem bleiben nichtvalid. Werte stehen, wenn man das modal mit ESC schließt und dann mit edit wieder öffnet
- color scheme beim Favicon anpassen
- Deployment: use minified libraries (aug-ui, bs, jq)
- focus-related stuff
- after pressing damage button, focus moves to first table row act button
- it's probably b/c I resort the table

View File

@ -1,16 +1,27 @@
const s = require("../src/js/sr2ini.js");
const sr2ini = require("../src/js/sr2ini.js");
describe ("test function rollForInitiative()", () => {
test("function should accept numbes as integers as well as strings", () => {
expect(typeof s.rollForInitiative("1", "5")).toBe("number");
expect(typeof s.rollForInitiative(1, 5)).toBe("number");
test("function should accept integers and strings as parameters", () => {
expect(typeof sr2ini.rollForInitiative("1", "5")).toBe("number");
expect(typeof sr2ini.rollForInitiative(1, 5)).toBe("number");
expect(typeof sr2ini.rollForInitiative(1, "5")).toBe("number");
expect(typeof sr2ini.rollForInitiative("1", 5)).toBe("number");
});
test("result should be greater than zero", () => {
expect(s.rollForInitiative("1", "5")).toBeGreaterThan(0);
test("return value should be zero for bad parameters", () => {
// one or zero parameters
expect(sr2ini.rollForInitiative(1)).toBe(0);
expect(sr2ini.rollForInitiative()).toBe(0);
// non-number strings
expect(sr2ini.rollForInitiative("qwerty", 12)).toBe(0);
expect(sr2ini.rollForInitiative(1, "six")).toBe(0);
// parameters as an array
expect(sr2ini.rollForInitiative(new Array(1, 2))).toBe(0);
expect(sr2ini.rollForInitiative(new Array("1", "2"))).toBe(0);
});
test("should return zero for bad parameters", () => {
expect(s.rollForInitiative("a", 2)).toBe(0);
test("should return a value within the possible range of dice rolls", () => {
expect(sr2ini.rollForInitiative(1, 0)).toBeGreaterThanOrEqual(1);
expect(sr2ini.rollForInitiative(1, 0)).toBeLessThanOrEqual(6);
expect(sr2ini.rollForInitiative(5, 20)).toBeGreaterThanOrEqual(25);
expect(sr2ini.rollForInitiative(5, 20)).toBeLessThanOrEqual(50);
});
});

View File

@ -197,4 +197,4 @@
</footer>
</div>
</body>
</html>
</html>

View File

@ -466,6 +466,11 @@ function sortTable() {
// validate a combatant row form by checking for all conditions, including regular HTML5 validation
function validateCombatant() {
// do standard HTML5 form validation first
// (makes sure that name is not empty and that all other values are numbers within their individual ranges)
if ( ! $("#combatant-form").get(0).reportValidity() ) {
return false;
}
// get input elements
let inputElements = {
name: $("#combatant-modal-name").get(0),
@ -473,29 +478,20 @@ function validateCombatant() {
dice: $("#combatant-modal-dice").get(0),
rea: $("#combatant-modal-rea").get(0)
};
// do standard HTML5 form validation first
// (makes sure that name is not empty and that all other values are numbers within their individual ranges)
let valid = true;
Object.values(inputElements).forEach( input => {
if (!input.reportValidity()) { valid = false; }
})
if (!valid) { return false; }
// now for some custom validation; first we need to get the input values
let ini = inputElements["ini"].value.trim();
let dice = inputElements["dice"].value.trim();
let rea = inputElements["rea"].value.trim();
// invalidate if ini, dice and rea are all empty
if (ini == "" && (dice == "" || rea == "")) {
inputElements["ini"].setCustomValidity("Requiring values for ini dice and reaction, or initiative, or all three");
inputElements["ini"].setCustomValidity("Values required for either initiative, or dice and reaction, or all three");
inputElements["ini"].reportValidity();
inputElements["ini"].setCustomValidity("");
return false;
}
// invalidate if dice or rea is empty but not both
if ((dice == "") != (rea == "")) {
inputElements["dice"].setCustomValidity("Values required for both dice and reaction, or none (in which case ini is required)");
inputElements["dice"].reportValidity();
inputElements["dice"].setCustomValidity("");
return false;
}
// ok then
@ -521,6 +517,10 @@ $(document).ready(function () {
$("#confirm-modal-remove-combatant-ok-button").on("click", () => {
removeCombatant();
});
// add event handler to certain input elements that removes any custom validity message
$("#combatant-modal-dice #combatant-modal-rea #combatant-modal-ini").on("input", (event) => {
event.target.setCustomValidity("");
});
// add event listeners to damage sliders in combatant modal
$("#combatant-modal-stun").on("input change", () => {
if ($("#combatant-modal-stun").val() == "10") {
@ -555,3 +555,5 @@ $(document).ready(function () {
});
addTestCombatant();
});
module.exports = { rollForInitiative, validateCombatant, whoGoesFirst, getEffectiveIni };