diff --git a/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts b/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts index 025abdf3..27258ff7 100644 --- a/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts +++ b/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts @@ -16,25 +16,12 @@ */ import getLevelFields from './getLevelFields'; -import {getAddForm, addLevel} from '../setupTests'; -import {Level} from './index'; -//import CONST from '../const'; +import {getAddForm, level1, level2, addLevel} from '../setupTests'; document.body.innerHTML = getAddForm(); -const level1: Level = { - difficulty_factor: 200, - visitor_threshold: 500, -}; - -const level2: Level = { - difficulty_factor: 400, - visitor_threshold: 700, -}; - it('get levels fields works', () => { addLevel(level1.visitor_threshold, level1.difficulty_factor); - console.log(document.body.innerHTML); expect(getLevelFields(1)).toEqual(level1); addLevel(level2.visitor_threshold, level2.difficulty_factor); diff --git a/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts b/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts index 97173651..6dfce4af 100644 --- a/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts +++ b/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts @@ -17,7 +17,6 @@ import getNumLevels from './getNumLevels'; import {getAddForm, addLevel} from '../setupTests'; -//import CONST from '../const'; document.body.innerHTML = getAddForm(); diff --git a/templates/panel/sitekey/add/ts/levels/index.ts b/templates/panel/sitekey/add/ts/levels/index.ts index 2bd89ba2..109ec133 100644 --- a/templates/panel/sitekey/add/ts/levels/index.ts +++ b/templates/panel/sitekey/add/ts/levels/index.ts @@ -15,8 +15,6 @@ * along with this program. If not, see . */ -import getNumLevels from './getNumLevels'; - /** Datatype represenging an mCaptcha level */ export type Level = { difficulty_factor: number; @@ -26,13 +24,9 @@ export type Level = { /** Datatype representing a collection of mCaptcha levels */ class Levels { levels: Array; - numOnScreen: number; - numRecoreded: number; constructor() { this.levels = []; - this.numRecoreded = 0; - this.numOnScreen = getNumLevels(); } add = (newLevel: Level) => { @@ -42,7 +36,7 @@ class Levels { } if (newLevel.visitor_threshold <= 0) { - throw new Error('Visitors must be graeter than zero'); + throw new Error('Visitors must be greater than zero'); } if (this.levels.length == 0) { @@ -53,28 +47,19 @@ class Levels { let msg; let count = 1; - const validate = (level: Level, newLevel: Level) => { + this.levels.forEach(level => { if (level.visitor_threshold >= newLevel.visitor_threshold) { - msg = `Level: ${newLevel} visitor count has to greater than previous levels. See ${count}`; - return true; + const msg = `Level: ${newLevel} visitor count has to greater than previous levels. See ${count}`; + throw new Error(msg); + } else if (level.difficulty_factor >= newLevel.difficulty_factor) { + const msg = `Level ${this.levels.length} difficulty has to greater than previous levels See ${count}`; + throw new Error(msg); + } else { + count++; } + }); - if (level.difficulty_factor >= newLevel.difficulty_factor) { - msg = `Level ${this.levels.length} difficulty has to greater than previous levels See ${count}`; - return true; - } - count++; - return false; - }; - - if (this.levels.find(level => validate(level, newLevel))) { - alert(msg); - throw new Error(msg); - } else { - this.levels.push(newLevel); - this.numOnScreen += 1; - this.numRecoreded += 1; - } + this.levels.push(newLevel); }; get = () => this.levels; @@ -87,16 +72,6 @@ export const LEVELS = (function() { return { /** get levels */ getLevels: () => levels.get(), - /** - * get levels displayed on screen. - * This includes the one with add level button - * */ - getOnScreen: () => levels.numOnScreen, - /** - * get levels recorded using LEVELS - * This excludes the one with add level button - * */ - getRecored: () => levels.numRecoreded, /** add new level */ add: (newLevel: Level) => levels.add(newLevel), diff --git a/templates/panel/sitekey/add/ts/levels/levels.test.ts b/templates/panel/sitekey/add/ts/levels/levels.test.ts new file mode 100644 index 00000000..e5881da0 --- /dev/null +++ b/templates/panel/sitekey/add/ts/levels/levels.test.ts @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import {LEVELS, Level} from './index'; +import {level1, level1visErr, level1diffErr, level2} from '../setupTests'; + +const visitorErr = 'visitor count has to greater than previous levels'; +const difficultyErr = 'difficulty has to greater than previous levels'; + +const zeroVisError = 'Visitors must be greater than zero'; +const zeroDiffError = 'Difficulty must be greater than zero'; + +const zeroVis: Level = { + difficulty_factor: 10, + visitor_threshold: 0, +}; + +const zeroDiff: Level = { + difficulty_factor: 0, + visitor_threshold: 10, +}; + +it('LEVELS works', () => { + // add level + LEVELS.add(level1); + expect(LEVELS.getLevels()).toEqual([level1]); + + // add visitor count < prev level + try { + LEVELS.add(level1visErr); + } catch (e) { + expect(e.message).toContain(visitorErr); + } + + // add difficulty < prev level + try { + LEVELS.add(level1diffErr); + } catch (e) { + expect(e.message).toContain(difficultyErr); + } + + // add second level + LEVELS.add(level2); + expect(LEVELS.getLevels()).toEqual([level1, level2]); + + // update level + const newLevel2 = level2; + newLevel2.difficulty_factor = 8000; + LEVELS.update(newLevel2, 2); + expect(LEVELS.getLevels()).toEqual([level1, newLevel2]); + + // update second level + LEVELS.remove(1); + expect(LEVELS.getLevels()).toEqual([newLevel2]); + + // visitor is 0 + try { + LEVELS.add(zeroVis); + } catch (e) { + expect(e.message).toEqual(zeroVisError); + } + // difficulty is 0 + try { + LEVELS.add(zeroDiff); + } catch (e) { + expect(e.message).toEqual(zeroDiffError); + } +}); diff --git a/templates/panel/sitekey/add/ts/levels/updateLevel.ts b/templates/panel/sitekey/add/ts/levels/updateLevel.ts index 9424a1de..a5e855ff 100644 --- a/templates/panel/sitekey/add/ts/levels/updateLevel.ts +++ b/templates/panel/sitekey/add/ts/levels/updateLevel.ts @@ -27,16 +27,12 @@ const updateLevel = (e: Event) => { let level; if (id.includes(CONST.VISITOR_WITHOUT_LEVEL)) { - level = id.slice(CONST.VISITOR_WITHOUT_LEVEL.length); - } else if (id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) { - level = id.slice(CONST.DIFFICULTY_WITHOUT_LEVEL.length); - } else { - throw new Error( - 'update event was triggered by some element other than difficulty or visitor', - ); + level = parseInt(id.slice(CONST.VISITOR_WITHOUT_LEVEL.length)); + } + if (id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) { + level = parseInt(id.slice(CONST.DIFFICULTY_WITHOUT_LEVEL.length)); } - level = parseInt(level); if (Number.isNaN(level)) { console.error(`[updateLevel.ts] level # computed is not correct, got NaN`); } diff --git a/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts b/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts new file mode 100644 index 00000000..3e25de05 --- /dev/null +++ b/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import validateLevel from './validateLevel'; +import {getAddForm, level1, fillAddLevel} from '../setupTests'; + +document.body.innerHTML = getAddForm(); + +it('validate levels fields works', () => { + // null error + expect(validateLevel(1)).toEqual(false); + + fillAddLevel(level1.visitor_threshold, level1.difficulty_factor); + expect(validateLevel(1)).toEqual(true); + + // zero visitor error + fillAddLevel(0, level1.difficulty_factor); + expect(validateLevel(1)).toEqual(false); + + // zero difficulty error + fillAddLevel(level1.visitor_threshold, 0); + expect(validateLevel(1)).toEqual(false); +}); diff --git a/templates/panel/sitekey/add/ts/levels/validateLevel.ts b/templates/panel/sitekey/add/ts/levels/validateLevel.ts index c7c4b9b5..e99c3164 100644 --- a/templates/panel/sitekey/add/ts/levels/validateLevel.ts +++ b/templates/panel/sitekey/add/ts/levels/validateLevel.ts @@ -23,13 +23,8 @@ import getLevelFields from './getLevelFields'; * its contents * */ const validateLevel = (id: number) => { - const level = getLevelFields(id); - - if (level === null) { - return false; - } - try { + const level = getLevelFields(id); LEVELS.add(level); return true; } catch (e) { diff --git a/templates/panel/sitekey/add/ts/setupTests.ts b/templates/panel/sitekey/add/ts/setupTests.ts index 3c1bbf99..43a3a9aa 100644 --- a/templates/panel/sitekey/add/ts/setupTests.ts +++ b/templates/panel/sitekey/add/ts/setupTests.ts @@ -15,6 +15,7 @@ * along with this program. If not, see . */ import getNumLevels from './levels/getNumLevels'; +import {Level} from './levels/index'; import CONST from './const'; import addLevelButtonAddEventListener from './addLevelButton'; @@ -89,8 +90,17 @@ export const getAddForm = () => ` `; -/** add level to DOM by filling add level form */ +/** add level to DOM by filling add level form and clicking "Add" button */ export const addLevel = (visitor: number, diff: number) => { + fillAddLevel(visitor, diff); + const addLevelButton = ( + document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`) + ); + addLevelButton.click(); +}; + +/** Fill add level form without clicking add button */ +export const fillAddLevel = (visitor: number, diff: number) => { addLevelButtonAddEventListener(); const level = getNumLevels(); @@ -103,10 +113,41 @@ export const addLevel = (visitor: number, diff: number) => { document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`) ); diffField.value = diff.toString(); - - const addLevelButton = ( - document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`) - ); - - addLevelButton.click(); +}; + +/** Fill add level form without clicking add button */ +export const editLevel = (level: number, visitor?: number, diff?: number) => { + if (visitor !== undefined) { + const visitorField = ( + document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`) + ); + visitorField.value = visitor.toString(); + } + + if (diff !== undefined) { + const diffField = ( + document.getElementById(`${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`) + ); + diffField.value = diff.toString(); + } +}; + +export const level1: Level = { + difficulty_factor: 200, + visitor_threshold: 500, +}; + +export const level1diffErr: Level = { + difficulty_factor: 100, + visitor_threshold: 600, +}; + +export const level1visErr: Level = { + difficulty_factor: 600, + visitor_threshold: 400, +}; + +export const level2: Level = { + difficulty_factor: 400, + visitor_threshold: 700, };