frontend linting

This commit is contained in:
realaravinth
2021-10-08 15:24:29 +05:30
parent f7afc72d81
commit 53720ff740
91 changed files with 2158 additions and 1677 deletions

View File

@@ -15,16 +15,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from './levels/getNumLevels';
import {getAddForm, trim, addLevel} from './setupTests';
import setup from '../../../../components/error/setUpTests';
import getNumLevels from "./levels/getNumLevels";
import {getAddForm, trim, addLevel} from "./setupTests";
import setup from "../../../../components/error/setUpTests";
document.body.innerHTML = getAddForm();
document.body.appendChild(setup());
jest.useFakeTimers();
it('addLevelButton works', () => {
it("addLevelButton works", () => {
expect(getNumLevels()).toBe(1);
// add a level
addLevel(2, 4);
@@ -36,7 +36,7 @@ it('addLevelButton works', () => {
addLevel(4, 9);
expect(getNumLevels()).toBe(3);
let a = document.body.innerHTML;
const a = document.body.innerHTML;
expect(trim(a)).toBe(trim(finalHtml()));

View File

@@ -14,16 +14,16 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import validateLevel from './levels/validateLevel';
import getNumLevels from './levels/getNumLevels';
import * as UpdateLevel from './levels/updateLevel';
import validateLevel from "./levels/validateLevel";
import getNumLevels from "./levels/getNumLevels";
import * as UpdateLevel from "./levels/updateLevel";
import {
getRemoveButtonHTML,
addRemoveLevelButtonEventListener,
} from './removeLevelButton';
import CONST from './const';
} from "./removeLevelButton";
import CONST from "./const";
import log from '../../../../logger';
import log from "../../../../logger";
/**
* Gets executed when 'Add' Button is clicked to add levels
@@ -39,30 +39,30 @@ const addLevel = (e: Event) => {
const isValid = validateLevel(onScreenLevel);
log.debug(`[addLevelButton] isValid: ${isValid}`);
if (!isValid) {
let error = `Aborting level ${onScreenLevel} addition`;
const error = `Aborting level ${onScreenLevel} addition`;
return log.error(error);
}
FIELDSET.replaceChild(getRemoveButtonHTML(onScreenLevel), PARENT);
const newLevelElement = getHtml(onScreenLevel + 1);
FIELDSET.insertAdjacentElement('afterend', newLevelElement);
FIELDSET.insertAdjacentElement("afterend", newLevelElement);
UpdateLevel.register(onScreenLevel);
addRemoveLevelButtonEventListener(onScreenLevel);
addLevelButtonAddEventListener();
const main = document.querySelector('body');
const main = document.querySelector("body");
const style = main.style.display;
main.style.display = 'none';
main.style.display = "none";
main.style.display = style;
};
/** adds onclick event listener */
const addLevelButtonAddEventListener = () => {
const addLevelButtonAddEventListener = (): void => {
const addLevelButton = <HTMLElement>(
document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`)
);
addLevelButton.addEventListener('click', addLevel);
addLevelButton.addEventListener("click", addLevel);
};
/**
@@ -72,25 +72,25 @@ const addLevelButtonAddEventListener = () => {
const getHtml = (level: number) => {
log.debug(`[generating HTML getHtml]level: ${level}`);
const fieldset = document.createElement('fieldset'); // new HTMLFieldSetElement();
const fieldset = document.createElement("fieldset"); // new HTMLFieldSetElement();
fieldset.className = CONST.LEVEL_CONTAINER_CLASS;
fieldset.id = `${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`;
const legend = document.createElement('legend'); // new HTMLLegendElement();
const legend = document.createElement("legend"); // new HTMLLegendElement();
legend.className = CONST.LEGEND_CLASS;
const legendText = document.createTextNode(`Level ${level}`);
legend.appendChild(legendText);
fieldset.appendChild(legend);
const vistitorLabel = document.createElement('label'); //document.createElement('label');
const vistitorLabel = document.createElement("label"); //document.createElement('label');
vistitorLabel.className = CONST.LABEL_CLASS;
const visitorText = document.createTextNode('Visitor');
const visitorText = document.createTextNode("Visitor");
vistitorLabel.appendChild(visitorText);
const visitor = document.createElement('input'); //document.createElement('input');
const visitor = document.createElement("input"); //document.createElement('input');
const visitorId = `${CONST.VISITOR_WITHOUT_LEVEL}${level}`;
visitor.className = CONST.LEVEL_INPUT_CLASS;
visitor.type = 'number';
visitor.type = "number";
visitor.name = visitorId;
visitor.id = visitorId;
vistitorLabel.htmlFor = visitorId;
@@ -98,13 +98,13 @@ const getHtml = (level: number) => {
fieldset.appendChild(vistitorLabel);
const difficultyLabel = document.createElement('label');
const difficultyLabel = document.createElement("label");
difficultyLabel.className = CONST.LABEL_CLASS;
const difficultyText = document.createTextNode('Difficulty');
const difficultyText = document.createTextNode("Difficulty");
difficultyLabel.appendChild(difficultyText);
const difficulty = document.createElement('input');
const difficulty = document.createElement("input");
const difficultyID = `${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`;
difficulty.type = 'number';
difficulty.type = "number";
difficulty.name = difficultyID;
difficulty.className = CONST.LEVEL_INPUT_CLASS;
difficulty.id = difficultyID;
@@ -113,18 +113,18 @@ const getHtml = (level: number) => {
fieldset.appendChild(difficultyLabel);
const addLevelLabel = document.createElement('label');
const addLevelLabel = document.createElement("label");
addLevelLabel.className = CONST.REMOVE_LEVEL_LABEL_CLASS;
const addLevel = document.createElement('input');
const addLevel = document.createElement("input");
addLevel.className = CONST.ADD_LEVEL_BUTTON;
addLevel.type = 'button';
const addLevelButtonID = 'add';
addLevel.type = "button";
const addLevelButtonID = "add";
addLevel.name = addLevelButtonID;
addLevel.id = addLevelButtonID;
addLevelLabel.htmlFor = addLevelButtonID;
const addText = document.createTextNode('Add level');
const addText = document.createTextNode("Add level");
addLevelLabel.appendChild(addText);
addLevel.value = 'Add';
addLevel.value = "Add";
addLevelLabel.appendChild(addLevel);
fieldset.appendChild(addLevelLabel);

View File

@@ -15,25 +15,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const LABEL_INNER_TEXT_WITHOUT_LEVEL = 'Level ';
const LABEL_CLASS = 'sitekey-form__level-label';
const LABEL_INNER_TEXT_WITHOUT_LEVEL = "Level ";
const LABEL_CLASS = "sitekey-form__level-label";
const INPUT_ID_WITHOUT_LEVEL = 'level';
const LEVEL_INPUT_CLASS = 'sitekey-form__level-input';
const INPUT_ID_WITHOUT_LEVEL = "level";
const LEVEL_INPUT_CLASS = "sitekey-form__level-input";
const VISITOR_WITHOUT_LEVEL = 'visitor';
const DIFFICULTY_WITHOUT_LEVEL = 'difficulty';
const VISITOR_WITHOUT_LEVEL = "visitor";
const DIFFICULTY_WITHOUT_LEVEL = "difficulty";
const LEVEL_CONTAINER_CLASS = 'sitekey__level-container';
const LEVEL_FIELDSET_ID_WITHOUT_LEVEL = 'level-group-';
const LEGEND_CLASS = 'sitekey__level-title';
const LEVEL_CONTAINER_CLASS = "sitekey__level-container";
const LEVEL_FIELDSET_ID_WITHOUT_LEVEL = "level-group-";
const LEGEND_CLASS = "sitekey__level-title";
const REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL = 'remove-level';
const REMOVE_LEVEL_BUTTON_CLASS = 'sitekey-form__level-remove-level-button';
const REMOVE_LEVEL_LABEL_TEXT = 'Remove Level';
const REMOVE_LEVEL_LABEL_CLASS = 'sitekey-form__level-label--hidden';
const REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL = "remove-level";
const REMOVE_LEVEL_BUTTON_CLASS = "sitekey-form__level-remove-level-button";
const REMOVE_LEVEL_LABEL_TEXT = "Remove Level";
const REMOVE_LEVEL_LABEL_CLASS = "sitekey-form__level-label--hidden";
const ADD_LEVEL_BUTTON = 'sitekey-form__level-add-level-button';
const ADD_LEVEL_BUTTON = "sitekey-form__level-add-level-button";
const CONST = {
LABEL_CLASS,

View File

@@ -15,30 +15,31 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LEVELS} from '../levels';
import { LEVELS } from "../levels";
import getFormUrl from '../../../../../utils/getFormUrl';
import genJsonPayload from '../../../../../utils/genJsonPayload';
import getFormUrl from "../../../../../utils/getFormUrl";
import genJsonPayload from "../../../../../utils/genJsonPayload";
import VIEWS from '../../../../../views/v1/routes';
import VIEWS from "../../../../../views/v1/routes";
import validateDescription from './validateDescription';
import validateDuration from './validateDuration';
import validateDescription from "./validateDescription";
import validateDuration from "./validateDuration";
import createError from '../../../../../components/error';
import createError from "../../../../../components/error";
export const SITE_KEY_FORM_CLASS = 'sitekey-form';
export const FORM = <HTMLFormElement>document.querySelector(`.${SITE_KEY_FORM_CLASS}`);
export const SITE_KEY_FORM_CLASS = "sitekey-form";
export const FORM = <HTMLFormElement>(
document.querySelector(`.${SITE_KEY_FORM_CLASS}`)
);
export const addSubmitEventListener = () => {
FORM.addEventListener('submit', submit, true);
};
export const addSubmitEventListener = (): void =>
FORM.addEventListener("submit", submit, true);
const submit = async (e: Event) => {
e.preventDefault();
const description = validateDescription(e);
const duration = validateDuration(e);
const duration = validateDuration();
const formUrl = getFormUrl(FORM);

View File

@@ -15,11 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import validateDescription from './validateDescription';
import {getAddForm, fillDescription} from '../setupTests';
import {mockAlert} from '../../../../../setUpTests';
import validateDescription from "./validateDescription";
import {getAddForm, fillDescription} from "../setupTests";
import {mockAlert} from "../../../../../setUpTests";
import setup from '../../../../../components/error/setUpTests';
import setup from "../../../../../components/error/setUpTests";
mockAlert();
@@ -27,17 +27,17 @@ document.body.innerHTML = getAddForm();
const emptyErr = "can't be empty";
it('validateDescription workds', () => {
document.querySelector('body').appendChild(setup());
it("validateDescription workds", () => {
document.querySelector("body").appendChild(setup());
try {
const event = new Event('submit');
const event = new Event("submit");
validateDescription(event);
} catch (e) {
expect(e.message).toContain(emptyErr);
}
// fill and validate
fillDescription('testing');
const event = new Event('submit');
fillDescription("testing");
const event = new Event("submit");
validateDescription(event);
});

View File

@@ -15,12 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import isBlankString from '../../../../../utils/isBlankString';
import isBlankString from "../../../../../utils/isBlankString";
const validateDescription = (e: Event) => {
const inputElement = <HTMLInputElement>document.getElementById('description');
const validateDescription = (e: Event): string => {
const inputElement = <HTMLInputElement>document.getElementById("description");
const val = inputElement.value;
const filed = 'Description';
const filed = "Description";
isBlankString(val, filed, e);
return val;
};

View File

@@ -14,8 +14,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import isNumber from '../../../../../utils/isNumber';
//const validateDuration = (e: Event) => {
// const duartionElement = <HTMLInputElement>document.getElementById('duration');
// const duration = parseInt(duartionElement.value);
@@ -31,30 +29,28 @@ import isNumber from '../../../../../utils/isNumber';
//
//export default validateDuration;
import validateDuration from './validateDuration';
import {getAddForm, fillDuration} from '../setupTests';
import validateDuration from "./validateDuration";
import {getAddForm, fillDuration} from "../setupTests";
document.body.innerHTML = getAddForm();
const emptyErr = "can't be empty";
const NaNErr = 'duration can contain nubers only';
const zeroErr = 'duration must be greater than zero';
const NaNErr = "duration can contain nubers only";
const zeroErr = "duration must be greater than zero";
const duration = 30;
it('validateDuration workds', () => {
it("validateDuration workds", () => {
try {
const event = new Event('submit');
validateDuration(event);
validateDuration();
} catch (e) {
expect(e.message).toContain(emptyErr);
}
// fill string error
try {
fillDuration('testing');
const event = new Event('submit');
validateDuration(event);
fillDuration("testing");
validateDuration();
} catch (e) {
expect(e.message).toContain(NaNErr);
}
@@ -62,13 +58,11 @@ it('validateDuration workds', () => {
// zero err
try {
fillDuration(0);
const event = new Event('submit');
validateDuration(event);
validateDuration();
} catch (e) {
expect(e.message).toContain(zeroErr);
}
fillDuration(duration);
const event = new Event('submit');
expect(validateDuration(event)).toBe(duration);
expect(validateDuration()).toBe(duration);
});

View File

@@ -14,17 +14,17 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import isNumber from '../../../../../utils/isNumber';
import isNumber from "../../../../../utils/isNumber";
const validateDuration = (e: Event) => {
const duartionElement = <HTMLInputElement>document.getElementById('duration');
const validateDuration = (): number => {
const duartionElement = <HTMLInputElement>document.getElementById("duration");
const duration = parseInt(duartionElement.value);
if (!isNumber(duration) || Number.isNaN(duration)) {
throw new Error('duration can contain nubers only');
throw new Error("duration can contain nubers only");
}
if (duration <= 0) {
throw new Error('duration must be greater than zero');
throw new Error("duration must be greater than zero");
}
return duration;
};

View File

@@ -15,10 +15,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import addLevelButtonAddEventListener from './addLevelButton';
import addSubmitEventListener from './form';
import addLevelButtonAddEventListener from "./addLevelButton";
import addSubmitEventListener from "./form";
export const index = () => {
export const index = (): void => {
addLevelButtonAddEventListener();
addSubmitEventListener();
};

View File

@@ -15,27 +15,27 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getLevelFields from './getLevelFields';
import getLevelFields from "./getLevelFields";
import {
getAddForm,
level1,
level2,
fillAddLevel,
addLevel,
} from '../setupTests';
} from "../setupTests";
document.body.innerHTML = getAddForm();
const visNumErr = 'visitor can contain nubers only';
const diffNumErr = 'difficulty can contain nubers only';
const visNumErr = "visitor can contain nubers only";
const diffNumErr = "difficulty can contain nubers only";
it('get levels fields works', () => {
it("get levels fields works", () => {
addLevel(level1.visitor_threshold, level1.difficulty_factor);
expect(getLevelFields(1)).toEqual(level1);
// NaN visitor
try {
fillAddLevel('test', level2.difficulty_factor);
fillAddLevel("test", level2.difficulty_factor);
getLevelFields(2);
} catch (e) {
expect(e.message).toBe(visNumErr);
@@ -43,7 +43,7 @@ it('get levels fields works', () => {
// Nan difficulty_factor
try {
fillAddLevel(level2.visitor_threshold, 'fooasdads');
fillAddLevel(level2.visitor_threshold, "fooasdads");
getLevelFields(2);
} catch (e) {
expect(e.message).toBe(diffNumErr);

View File

@@ -15,13 +15,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Level} from './index';
import CONST from '../const';
import { Level } from "./index";
import CONST from "../const";
import log from '../../../../../logger';
import log from "../../../../../logger";
/** Fetches level from DOM using the ID passesd and validates */
const getLevelFields = (id: number) => {
const getLevelFields = (id: number): Level => {
log.debug(`[getLevelFields]: id: ${id}`);
const visitorID = CONST.VISITOR_WITHOUT_LEVEL + id.toString();
const difficultyID = CONST.DIFFICULTY_WITHOUT_LEVEL + id.toString();
@@ -35,11 +35,11 @@ const getLevelFields = (id: number) => {
const difficulty_factor = parseInt(difficultyElement.value);
if (Number.isNaN(visitor_threshold)) {
throw new Error('visitor can contain nubers only');
throw new Error("visitor can contain nubers only");
}
if (Number.isNaN(difficulty_factor)) {
throw new Error('difficulty can contain nubers only');
throw new Error("difficulty can contain nubers only");
}
const level: Level = {
@@ -48,7 +48,7 @@ const getLevelFields = (id: number) => {
};
log.debug(
`[getLevelFields.ts] visitor: ${visitor_threshold} difficulty: ${difficulty_factor}`,
`[getLevelFields.ts] visitor: ${visitor_threshold} difficulty: ${difficulty_factor}`
);
return level;

View File

@@ -15,12 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from './getNumLevels';
import {getAddForm, addLevel} from '../setupTests';
import getNumLevels from "./getNumLevels";
import {getAddForm, addLevel} from "../setupTests";
document.body.innerHTML = getAddForm();
it('get num levels works', () => {
it("get num levels works", () => {
expect(getNumLevels()).toBe(1);
addLevel(2, 4);
expect(getNumLevels()).toBe(2);

View File

@@ -15,16 +15,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../const';
import CONST from "../const";
import log from '../../../../../logger';
import log from "../../../../../logger";
/** returns number of level input fields currently in DOM */
const getNumLevels = () => {
const getNumLevels = (): number => {
let numLevels = 0;
document
.querySelectorAll(`.${CONST.LEVEL_CONTAINER_CLASS}`)
.forEach(_ => numLevels++);
.forEach(() => numLevels++);
log.debug(`[getNumLevels]: numLevels: ${numLevels}`);
return numLevels;
};

View File

@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import log from '../../../../../logger';
import log from "../../../../../logger";
/** Datatype represenging an mCaptcha level */
export type Level = {
@@ -95,7 +95,7 @@ export const LEVELS = (function() {
}
}
levels.levels = tmpLevel.levels;
log.debug(`post update:`);
log.debug("post update:");
LEVELS.print();
return true;
} catch (e) {
@@ -133,7 +133,7 @@ export const LEVELS = (function() {
}
}
levels.levels = tmpLevel.levels;
log.debug('Post remove:');
log.debug("Post remove:");
LEVELS.print();
return true;
} catch (e) {

View File

@@ -15,14 +15,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LEVELS, Level} from './index';
import {level1, level1visErr, level1diffErr, level2} from '../setupTests';
import {LEVELS, Level} from "./index";
import {level1, level1visErr, level1diffErr, level2} from "../setupTests";
const visitorErr = 'visitor count should be greater than previous levels';
const difficultyErr = 'difficulty should be greater than previous levels';
const visitorErr = "visitor count should be greater than previous levels";
const difficultyErr = "difficulty should be greater than previous levels";
const zeroVisError = 'visitors must be greater than zero';
const zeroDiffError = 'difficulty must be greater than zero';
const zeroVisError = "visitors must be greater than zero";
const zeroDiffError = "difficulty must be greater than zero";
const zeroVis: Level = {
difficulty_factor: 10,
@@ -34,7 +34,7 @@ const zeroDiff: Level = {
visitor_threshold: 10,
};
it('LEVELS works', () => {
it("LEVELS works", () => {
// add level
LEVELS.add(level1);
expect(LEVELS.getLevels()).toEqual([level1]);

View File

@@ -15,14 +15,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../const';
import getLevelFields from './getLevelFields';
import {LEVELS} from './index';
import CONST from "../const";
import getLevelFields from "./getLevelFields";
import { LEVELS } from "./index";
import createError from '../../../../../components/error';
import createError from "../../../../../components/error";
/** on-change event handler to update level */
const updateLevel = (e: Event) => {
const updateLevel = (e: Event): void => {
const target = <HTMLInputElement>e.target;
const id = target.id;
@@ -36,7 +36,7 @@ const updateLevel = (e: Event) => {
}
if (Number.isNaN(level)) {
console.error(`[updateLevel.ts] level # computed is not correct, got NaN`);
console.error("[updateLevel.ts] level # computed is not correct, got NaN");
}
try {
@@ -48,7 +48,7 @@ const updateLevel = (e: Event) => {
};
/** registers on-change event handlers to update levels */
export const register = (id: number) => {
export const register = (id: number): void => {
const visitorID = CONST.VISITOR_WITHOUT_LEVEL + id.toString();
const difficultyID = CONST.DIFFICULTY_WITHOUT_LEVEL + id.toString();
@@ -57,6 +57,6 @@ export const register = (id: number) => {
document.getElementById(difficultyID)
);
visitorElement.addEventListener('input', updateLevel, false);
difficultyElement.addEventListener('input', updateLevel, false);
visitorElement.addEventListener("input", updateLevel, false);
difficultyElement.addEventListener("input", updateLevel, false);
};

View File

@@ -15,15 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import validateLevel from './validateLevel';
import {getAddForm, level1, fillAddLevel} from '../setupTests';
import setup from '../../../../../components/error/setUpTests';
import validateLevel from "./validateLevel";
import {getAddForm, level1, fillAddLevel} from "../setupTests";
import setup from "../../../../../components/error/setUpTests";
document.body.innerHTML = getAddForm();
document.body.appendChild(setup());
it('validate levels fields works', () => {
it("validate levels fields works", () => {
// null error
expect(validateLevel(1)).toEqual(false);

View File

@@ -15,15 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LEVELS} from './index';
import getLevelFields from './getLevelFields';
import createError from '../../../../../components/error/';
import {LEVELS} from "./index";
import getLevelFields from "./getLevelFields";
import createError from "../../../../../components/error/";
/**
* Fetches level from DOM using the ID passesd and validates
* its contents
* */
const validateLevel = (id: number) => {
const validateLevel = (id: number): boolean => {
try {
const level = getLevelFields(id);
LEVELS.add(level);

View File

@@ -14,13 +14,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LEVELS} from '../levels/index';
import updateLevelNumbersOnDOM from './updateDom';
import CONST from '../const';
import { LEVELS } from "../levels/index";
import updateLevelNumbersOnDOM from "./updateDom";
import CONST from "../const";
import log from '../../../../../logger';
import log from "../../../../../logger";
const REMOVE_LEVEL_BUTTON = 'sitekey-form__level-remove-level-button';
const REMOVE_LEVEL_BUTTON = "sitekey-form__level-remove-level-button";
/**
* Gets executed when 'Remove' Button is clicked to remove levels
@@ -31,12 +31,12 @@ const removeLevel = (e: Event) => {
const FIELDSET = <HTMLElement>PARENT.parentElement;
const levelNum = parseInt(
eventTarget.id.slice(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL.length),
eventTarget.id.slice(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL.length)
);
if (Number.isNaN(levelNum)) {
const msg =
'[removeLevelButton.ts] error in parsing level number from remove button ID';
"[removeLevelButton.ts] error in parsing level number from remove button ID";
//log.error(msg);
throw new Error(msg);
}
@@ -47,19 +47,19 @@ const removeLevel = (e: Event) => {
};
/** adds onclick event listener */
export const addRemoveLevelButtonEventListener = (level: number) => {
export const addRemoveLevelButtonEventListener = (level: number): void => {
const removeButton = document.getElementById(
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`,
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`
);
removeButton.addEventListener('click', removeLevel);
removeButton.addEventListener("click", removeLevel);
};
/** adds onclick event listener to all remove buttons */
export const addRemoveLevelButtonEventListenerAll = () => {
export const addRemoveLevelButtonEventListenerAll = (): void => {
const removeButtons = document.querySelectorAll(`.${REMOVE_LEVEL_BUTTON}`);
removeButtons.forEach(button =>
button.addEventListener('click', removeLevel),
removeButtons.forEach((button) =>
button.addEventListener("click", removeLevel)
);
};
@@ -67,20 +67,20 @@ export const addRemoveLevelButtonEventListenerAll = () => {
* Generate Remove button HTML. On-click handler should be added
* seprately
*/
export const getRemoveButtonHTML = (level: number) => {
export const getRemoveButtonHTML = (level: number): HTMLLabelElement => {
log.log(`[generating HTML getHtml]level: ${level}`);
const btn = document.createElement('input');
const btn = document.createElement("input");
btn.className = CONST.REMOVE_LEVEL_BUTTON_CLASS;
btn.type = 'button';
btn.type = "button";
const id = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`;
btn.name = id;
btn.id = id;
btn.value = 'x';
btn.value = "x";
const removeLabel = document.createElement('label');
const removeLabel = document.createElement("label");
removeLabel.className = CONST.REMOVE_LEVEL_LABEL_CLASS;
const removeLabelText = document.createTextNode('RemoveLevel');
const removeLabelText = document.createTextNode("RemoveLevel");
removeLabel.appendChild(removeLabelText);
removeLabel.appendChild(btn);
removeLabel.htmlFor = id;

View File

@@ -15,17 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../levels/getNumLevels';
import {
getAddForm,
getRemoveButtonHTMLForm,
trim,
addLevel,
} from '../setupTests';
import CONST from '../const';
import getNumLevels from "../levels/getNumLevels";
import { getAddForm, addLevel } from "../setupTests";
import CONST from "../const";
import log from '../../../../../logger';
import {MODE} from '../../../../../logger';
import log from "../../../../../logger";
import { MODE } from "../../../../../logger";
document.body.innerHTML = getAddForm();
@@ -46,13 +41,13 @@ const setUp = () => {
log.setMode(MODE.none);
it('removeLevelButton works', () => {
it("removeLevelButton works", () => {
setUp();
for (let i = 1; i < 4; i++) {
const l1 = <HTMLButtonElement>(
document.getElementById(
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${1}`,
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${1}`
)
);

View File

@@ -14,25 +14,25 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import CONST from '../../const';
import log from '../../../../../../logger';
import getNumLevels from "../../levels/getNumLevels";
import CONST from "../../const";
import log from "../../../../../../logger";
import updateLabels from './updateLabel';
import updateInputs from './updateInputs';
import updateRemoveButton from './updateRemoveButton';
import updateLevelGroup from './updateLevelGroup';
import updateLabels from "./updateLabel";
import updateInputs from "./updateInputs";
import updateRemoveButton from "./updateRemoveButton";
import updateLevelGroup from "./updateLevelGroup";
/**
* update level number on fieldset legends and their ids too
* @param {number} id - level number that was ordered to remove.
* All updates are made relative to id
* */
const updateLevelNumbersOnDOM = (id: number) => {
const updateLevelNumbersOnDOM = (id: number): void => {
const numLevels = getNumLevels();
if (id == numLevels) {
throw new Error(
"Can't remove the very fist element, it has to be first added to DOM",
"Can't remove the very fist element, it has to be first added to DOM"
);
}
@@ -42,7 +42,7 @@ const updateLevelNumbersOnDOM = (id: number) => {
const newLevel = i - 1;
const levelGroup = document.querySelector(
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${i}`,
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${i}`
);
if (levelGroup === null) {
@@ -53,9 +53,9 @@ const updateLevelNumbersOnDOM = (id: number) => {
}
// rename legend
const legend = levelGroup.getElementsByTagName('legend')[0];
const legend = levelGroup.getElementsByTagName("legend")[0];
const legendText = document.createTextNode(`Level ${newLevel}`);
const newLegend = document.createElement('legend');
const newLegend = document.createElement("legend");
newLegend.className = legend.className;
newLegend.appendChild(legendText);
legend.replaceWith(newLegend);

View File

@@ -15,12 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import {getAddForm, addLevel} from '../../setupTests';
import getNumLevels from "../../levels/getNumLevels";
import { getAddForm, addLevel } from "../../setupTests";
document.body.innerHTML = getAddForm();
export const setupAddlevels = () => {
export const setupAddlevels = (): void => {
expect(getNumLevels()).toBe(1);
// add a level
addLevel(2, 2);

View File

@@ -15,20 +15,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {getAddForm, trim} from '../../setupTests';
import updateInputs from './updateInputs';
import CONST from '../../const';
import {getAddForm, trim} from "../../setupTests";
import updateInputs from "./updateInputs";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import log from "../../../../../../logger";
import {MODE} from "../../../../../../logger";
import {setupAddlevels} from './setupTests';
import {setupAddlevels} from "./setupTests";
document.body.innerHTML = getAddForm();
log.setMode(MODE.none);
it('updateInputs works', () => {
it("updateInputs works", () => {
setupAddlevels();
// removing level 2
const level = 2;
@@ -58,7 +58,7 @@ it('updateInputs works', () => {
});
/** get initial form to test remove button functionality */
export const update = () => {
export const update = (): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<h1 class="form__title">

View File

@@ -14,11 +14,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import log from '../../../../../../logger';
import CONST from "../../const";
import log from "../../../../../../logger";
/** update input IDs with new level */
const updateInput = (levelGroup: Element, newLevel: number) => {
const updateInput = (levelGroup: Element, newLevel: number): void => {
const inputs = <NodeListOf<HTMLInputElement>>(
levelGroup.querySelectorAll(`.${CONST.LEVEL_INPUT_CLASS}`)
);
@@ -26,17 +26,17 @@ const updateInput = (levelGroup: Element, newLevel: number) => {
inputs.forEach(input => {
if (input.id.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
log.log(`${input.id}`);
log.log('changing visitor_threshold input');
log.log("changing visitor_threshold input");
const id = `${CONST.VISITOR_WITHOUT_LEVEL}${newLevel}`;
input.id = id;
input.name = id;
} else if (input.id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
log.log('changing difficulty input');
log.log("changing difficulty input");
const id = `${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`;
input.id = id;
input.name = id;
} else {
if (input.id != 'add') {
if (input.id != "add") {
throw new Error(`Did you add an extra input to DOM? ${input.id} ${input.className} ${input.name}`);
}
}

View File

@@ -15,18 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import {getAddForm, trim} from '../../setupTests';
import updateLabels from './updateLabel';
import CONST from '../../const';
import { trim } from "../../setupTests";
import updateLabels from "./updateLabel";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import {setupAddlevels} from './setupTests';
import log from "../../../../../../logger";
import { MODE } from "../../../../../../logger";
/** get initial form to test remove button functionality */
export const labelLevel = (level: number) => {
export const labelLevel = (level: number): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<fieldset class="sitekey__level-container" id="level-group-2">
@@ -85,11 +82,11 @@ document.body.innerHTML = labelLevel(2);
log.setMode(MODE.none);
it('addLevelButton works', () => {
it("addLevelButton works", () => {
// removing level 2
const level = 2;
const levelGroup = document.querySelector(
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`,
`#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`
);
const newLevel = 20;
@@ -100,22 +97,22 @@ it('addLevelButton works', () => {
levelGroup.querySelectorAll(`.${CONST.LABEL_CLASS}`)
);
log.log(labels);
labels.forEach(label => {
labels.forEach((label) => {
log.log(`${label.htmlFor}`);
if (label.htmlFor.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
expect(label.htmlFor).toBe(`${CONST.VISITOR_WITHOUT_LEVEL}${newLevel}`);
} else if (label.htmlFor.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
expect(label.htmlFor).toBe(
`${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`,
`${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`
);
} else if (
label.htmlFor.includes(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL)
) {
expect(label.htmlFor).toBe(
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`,
`${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`
);
} else {
throw new Error('Did you add an extra label to DOM?');
throw new Error("Did you add an extra label to DOM?");
}
});

View File

@@ -14,17 +14,17 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import log from '../../../../../../logger';
import CONST from "../../const";
import log from "../../../../../../logger";
/** update level lables to match new level */
const updateLabels = (levelGroup: Element, newLevel: number) => {
const updateLabels = (levelGroup: Element, newLevel: number): void => {
// rename labels
const labels = <NodeListOf<HTMLLabelElement>>(
levelGroup.querySelectorAll(`label`)
levelGroup.querySelectorAll("label")
);
log.log(labels);
labels.forEach(label => {
labels.forEach((label) => {
log.log(`${label.htmlFor}`);
const currentFor = label.htmlFor;
if (currentFor.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
@@ -36,9 +36,9 @@ const updateLabels = (levelGroup: Element, newLevel: number) => {
) {
label.htmlFor = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`;
} else {
if (currentFor != 'add') {
if (currentFor != "add") {
throw new Error(
`Did you add an extra label to DOM? Found label with for: ${currentFor}`,
`Did you add an extra label to DOM? Found label with for: ${currentFor}`
);
}
}

View File

@@ -15,18 +15,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from '../../levels/getNumLevels';
import {getAddForm, trim} from '../../setupTests';
import updateLevelGroup from './updateLevelGroup';
import CONST from '../../const';
import { trim} from "../../setupTests";
import updateLevelGroup from "./updateLevelGroup";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import log from "../../../../../../logger";
import {MODE} from "../../../../../../logger";
import {setupAddlevels} from './setupTests';
/** get initial form to test remove button functionality */
export const labelLevel = (level: number) => {
export const labelLevel = (level: number): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<fieldset class="sitekey__level-container" id="level-group-${level}">
@@ -85,7 +83,7 @@ document.body.innerHTML = labelLevel(2);
log.setMode(MODE.none);
it('update levelGroup works', () => {
it("update levelGroup works", () => {
// removing level 2
const level = 2;
const levelGroup = document.querySelector(

View File

@@ -14,10 +14,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import CONST from "../../const";
/** update level grup to match new level */
const updateLevelGroup = (levelGroup: Element, newLevel: number) =>
const updateLevelGroup = (levelGroup: Element, newLevel: number): string =>
(levelGroup.id = `${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${newLevel}`);
export default updateLevelGroup;

View File

@@ -15,15 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {trim} from '../../setupTests';
import updateRemoveButton from './updateRemoveButton';
import CONST from '../../const';
import {trim} from "../../setupTests";
import updateRemoveButton from "./updateRemoveButton";
import CONST from "../../const";
import log from '../../../../../../logger';
import {MODE} from '../../../../../../logger';
import log from "../../../../../../logger";
import {MODE} from "../../../../../../logger";
/** get initial form to test remove button functionality */
export const labelLevel = (level: number) => {
export const labelLevel = (level: number): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<fieldset class="sitekey__level-container" id="level-group-">
@@ -83,7 +83,7 @@ document.body.innerHTML = labelLevel(level);
log.setMode(MODE.none);
it('update remove button works', () => {
it("update remove button works", () => {
// removing level 2
const levelGroup = document.getElementById(

View File

@@ -14,10 +14,10 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from '../../const';
import CONST from "../../const";
/** update remove level button's ID */
const updateRemoveButton = (levelGroup: Element, newLevel: number) => {
const updateRemoveButton = (levelGroup: Element, newLevel: number): void => {
// rename button
const button = <HTMLInputElement>(
levelGroup.querySelector(`.${CONST.REMOVE_LEVEL_BUTTON_CLASS}`)

View File

@@ -14,13 +14,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from './levels/getNumLevels';
import {Level} from './levels/index';
import CONST from './const';
import addLevelButtonAddEventListener from './addLevelButton';
import getNumLevels from "./levels/getNumLevels";
import { Level } from "./levels/index";
import CONST from "./const";
import addLevelButtonAddEventListener from "./addLevelButton";
/** get rid of all whitespaces, useful when comparing DOM states */
export const trim = (s: string) => s.replace(/\s/g, '');
export const trim = (s: string): string => s.replace(/\s/g, "");
export const level1: Level = {
difficulty_factor: 200,
@@ -43,7 +43,7 @@ export const level2: Level = {
};
/** add level to DOM by filling add level form and clicking "Add" button */
export const addLevel = (visitor: number, diff: number) => {
export const addLevel = (visitor: number, diff: number): void => {
fillAddLevel(visitor, diff);
const addLevelButton = <HTMLElement>(
document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`)
@@ -54,8 +54,8 @@ export const addLevel = (visitor: number, diff: number) => {
/** Fill add level form without clicking add button */
export const fillAddLevel = (
visitor: number | string,
diff: number | string,
) => {
diff: number | string
): void => {
addLevelButtonAddEventListener();
const level = getNumLevels();
@@ -71,7 +71,11 @@ export const fillAddLevel = (
};
/** Fill add level form without clicking add button */
export const editLevel = (level: number, visitor?: number, diff?: number) => {
export const editLevel = (
level: number,
visitor?: number,
diff?: number
): void => {
if (visitor !== undefined) {
const visitorField = <HTMLInputElement>(
document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`)
@@ -88,18 +92,18 @@ export const editLevel = (level: number, visitor?: number, diff?: number) => {
};
/** Fill description in add level form */
export const fillDescription = (description: string) => {
const inputElement = <HTMLInputElement>document.getElementById('description');
export const fillDescription = (description: string): void => {
const inputElement = <HTMLInputElement>document.getElementById("description");
inputElement.value = description;
};
/** Fill duration in add level form */
export const fillDuration = (duration: number | string) => {
const inputElement = <HTMLInputElement>document.getElementById('duration');
export const fillDuration = (duration: number | string): void => {
const inputElement = <HTMLInputElement>document.getElementById("duration");
inputElement.value = duration.toString();
};
export const getAddForm = () => `
export const getAddForm = (): string => `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<h1 class="form__title">
Add Sitekey
@@ -171,7 +175,7 @@ export const getAddForm = () => `
`;
/** get initial form to test remove button functionality */
export const getRemoveButtonHTMLForm = () => {
export const getRemoveButtonHTMLForm = (): string => {
return `
<form class="sitekey-form" action="/api/v1/mcaptcha/levels/add" method="post">
<h1 class="form__title">