levels tests

This commit is contained in:
realaravinth 2021-05-06 17:22:25 +05:30
parent 6069509504
commit 20ee5c35c6
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
8 changed files with 184 additions and 72 deletions

View file

@ -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);

View file

@ -17,7 +17,6 @@
import getNumLevels from './getNumLevels';
import {getAddForm, addLevel} from '../setupTests';
//import CONST from '../const';
document.body.innerHTML = getAddForm();

View file

@ -15,8 +15,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<Level>;
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),

View file

@ -0,0 +1,82 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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);
}
});

View file

@ -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`);
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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);
});

View file

@ -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) {

View file

@ -15,6 +15,7 @@
* 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';
@ -89,8 +90,17 @@ export const getAddForm = () => `
</form>
`;
/** 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 = <HTMLElement>(
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 = <HTMLElement>(
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 = <HTMLInputElement>(
document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`)
);
visitorField.value = visitor.toString();
}
if (diff !== undefined) {
const diffField = <HTMLInputElement>(
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,
};