mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-06-07 14:46:40 +00:00
utils tests
This commit is contained in:
parent
c8d2ddbaf3
commit
14859ab594
@ -35,19 +35,17 @@ const router = new Router();
|
|||||||
router.register(panelRoute, panel);
|
router.register(panelRoute, panel);
|
||||||
router.register(settingsRoute, settings);
|
router.register(settingsRoute, settings);
|
||||||
|
|
||||||
test('error checking in router works', () => {
|
it('checks if Router works', () => {
|
||||||
try {
|
|
||||||
router.register(settingsRoute, settings);
|
|
||||||
} catch (e) {
|
|
||||||
expect(e.message).toBe('URI exists');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
test('checks if Router works', () => {
|
|
||||||
window.history.pushState({}, 'Settings', settingsRoute);
|
window.history.pushState({}, 'Settings', settingsRoute);
|
||||||
router.route();
|
router.route();
|
||||||
expect(result.result).toBe(settingsResult);
|
expect(result.result).toBe(settingsResult);
|
||||||
window.history.pushState({}, 'Panel', panelRoute);
|
window.history.pushState({}, 'Panel', panelRoute);
|
||||||
router.route();
|
router.route();
|
||||||
expect(result.result).toBe(panelResult);
|
expect(result.result).toBe(panelResult);
|
||||||
|
|
||||||
|
try {
|
||||||
|
router.register(settingsRoute, settings);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toBe('URI exists');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
36
templates/utils/genJsonPayload.test.ts
Normal file
36
templates/utils/genJsonPayload.test.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* 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 genJsonPayload from './genJsonPayload';
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
username: 'Jhon',
|
||||||
|
};
|
||||||
|
|
||||||
|
const value = {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
};
|
||||||
|
|
||||||
|
it('getFromUrl workds', () => {
|
||||||
|
expect(genJsonPayload(payload)).toEqual(value);
|
||||||
|
});
|
@ -16,7 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const genJsonPayload = (payload: any) => {
|
const genJsonPayload = (payload: any) => {
|
||||||
let value = {
|
const value = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
@ -21,20 +21,20 @@
|
|||||||
* So when using class-names, pass in ".whatever-classname"
|
* So when using class-names, pass in ".whatever-classname"
|
||||||
* and for ID, "#id".
|
* and for ID, "#id".
|
||||||
* */
|
* */
|
||||||
const getFormUrl = (querySelector: null|string|HTMLFormElement) => {
|
const getFormUrl = (querySelector?: string | HTMLFormElement) => {
|
||||||
let form;
|
let form;
|
||||||
if (querySelector === null) {
|
if (querySelector === undefined) {
|
||||||
form = <HTMLFormElement>document.querySelector("form");
|
form = <HTMLFormElement>document.querySelector('form');
|
||||||
}
|
}
|
||||||
if (querySelector === "string" || querySelector instanceof String) {
|
if (typeof querySelector == 'string' || querySelector instanceof String) {
|
||||||
form = <HTMLFormElement>document.querySelector(querySelector.toString());
|
form = <HTMLFormElement>document.querySelector(querySelector.toString());
|
||||||
}
|
}
|
||||||
if (querySelector instanceof HTMLFormElement) {
|
if (querySelector instanceof HTMLFormElement) {
|
||||||
form = querySelector;
|
form = querySelector;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( form !== undefined) {
|
if (form !== undefined) {
|
||||||
return form.action
|
return form.action;
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Can't find form");
|
throw new Error("Can't find form");
|
||||||
}
|
}
|
||||||
|
66
templates/utils/getFromUrl.test.ts
Normal file
66
templates/utils/getFromUrl.test.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* 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 getFormUrl from './getFormUrl';
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const formClassName = 'form__box';
|
||||||
|
const formURL = '/api/v1/signin';
|
||||||
|
|
||||||
|
document.body.innerHTML = `
|
||||||
|
<form method="POST" action="${formURL}" class="${formClassName}" id="form">
|
||||||
|
<label class="form__in-group" for="username"
|
||||||
|
>Username
|
||||||
|
<input
|
||||||
|
class="form__in-field"
|
||||||
|
id="username"
|
||||||
|
type="text"
|
||||||
|
name="username"
|
||||||
|
required=""
|
||||||
|
autofocus="true"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label for="password" class="form__in-group"
|
||||||
|
>Password
|
||||||
|
<input
|
||||||
|
class="form__in-field"
|
||||||
|
type="password"
|
||||||
|
id="password"
|
||||||
|
name="password"
|
||||||
|
required=""
|
||||||
|
/>
|
||||||
|
<!--
|
||||||
|
<a class="form__pw-recovery" -href="/recovert/password"
|
||||||
|
>Forgot password?</a
|
||||||
|
>
|
||||||
|
-->
|
||||||
|
</label>
|
||||||
|
<input type="submit" class="form__submit-button" value="Sign in" />
|
||||||
|
</form>
|
||||||
|
`;
|
||||||
|
|
||||||
|
it('getFromUrl workds', () => {
|
||||||
|
const name = `.${formClassName}`;
|
||||||
|
expect(getFormUrl(name)).toContain(formURL);
|
||||||
|
|
||||||
|
const form = <HTMLFormElement>document.querySelector('form');
|
||||||
|
expect(getFormUrl(form)).toContain(formURL);
|
||||||
|
|
||||||
|
expect(getFormUrl()).toContain(formURL);
|
||||||
|
});
|
33
templates/utils/isBlankString.test.ts
Normal file
33
templates/utils/isBlankString.test.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* 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 isBlankString from './isBlankString';
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
delete window.alert;
|
||||||
|
|
||||||
|
window.alert = (x: any) => console.log(x);
|
||||||
|
|
||||||
|
it('getFromUrl workds', () => {
|
||||||
|
expect(isBlankString('test', 'username')).toBe(false);
|
||||||
|
try {
|
||||||
|
isBlankString(' ', 'username');
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toContain(`can't be empty`);
|
||||||
|
}
|
||||||
|
});
|
@ -21,8 +21,11 @@ const isBlankString = (value: string|number, field: string, event?: Event) => {
|
|||||||
if (event !== undefined) {
|
if (event !== undefined) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
alert(`${field} can't be empty`);
|
const msg = `${field} can't be empty`;
|
||||||
|
alert(msg);
|
||||||
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default isBlankString;
|
export default isBlankString;
|
||||||
|
28
templates/utils/isNumber.test.ts
Normal file
28
templates/utils/isNumber.test.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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 isNumber from './isNumber';
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
it('getFromUrl workds', () => {
|
||||||
|
expect(isNumber('test')).toBe(false);
|
||||||
|
expect(isNumber('1test213')).toBe(false);
|
||||||
|
|
||||||
|
expect(isNumber('12')).toBe(true);
|
||||||
|
expect(isNumber(2)).toBe(true);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user