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(settingsRoute, settings);
test('error checking in router works', () => {
try {
router.register(settingsRoute, settings);
} catch (e) {
expect(e.message).toBe('URI exists');
}
});
test('checks if Router works', () => {
it('checks if Router works', () => {
window.history.pushState({}, 'Settings', settingsRoute);
router.route();
expect(result.result).toBe(settingsResult);
window.history.pushState({}, 'Panel', panelRoute);
router.route();
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) => {
let value = {
const value = {
method: 'POST',
headers: {
'Content-Type': 'application/json',

View file

@ -15,26 +15,26 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* querySelector is the the selector that will be
/**
* querySelector is the the selector that will be
* used to fetch elements.
* So when using class-names, pass in ".whatever-classname"
* and for ID, "#id".
* */
const getFormUrl = (querySelector: null|string|HTMLFormElement) => {
const getFormUrl = (querySelector?: string | HTMLFormElement) => {
let form;
if (querySelector === null) {
form = <HTMLFormElement>document.querySelector("form");
}
if (querySelector === "string" || querySelector instanceof String) {
if (querySelector === undefined) {
form = <HTMLFormElement>document.querySelector('form');
}
if (typeof querySelector == 'string' || querySelector instanceof String) {
form = <HTMLFormElement>document.querySelector(querySelector.toString());
}
}
if (querySelector instanceof HTMLFormElement) {
form = querySelector;
form = querySelector;
}
if ( form !== undefined) {
return form.action
if (form !== undefined) {
return form.action;
} else {
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) {
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;

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