utils tests

This commit is contained in:
realaravinth 2021-05-06 12:56:53 +05:30
parent c8d2ddbaf3
commit 14859ab594
No known key found for this signature in database
GPG Key ID: AD9F0F08E855ED88
8 changed files with 186 additions and 22 deletions

View File

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

View 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);
});

View File

@ -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',

View File

@ -21,12 +21,12 @@
* 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) {
@ -34,7 +34,7 @@ const getFormUrl = (querySelector: null|string|HTMLFormElement) => {
} }
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");
} }

View 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);
});

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

View File

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

View 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);
});