diff --git a/templates/router.test.ts b/templates/router.test.ts index ac8a7e8a..ba9f3ce3 100644 --- a/templates/router.test.ts +++ b/templates/router.test.ts @@ -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'); + } }); diff --git a/templates/utils/genJsonPayload.test.ts b/templates/utils/genJsonPayload.test.ts new file mode 100644 index 00000000..a8d859b1 --- /dev/null +++ b/templates/utils/genJsonPayload.test.ts @@ -0,0 +1,36 @@ +/* + * 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 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); +}); diff --git a/templates/utils/genJsonPayload.ts b/templates/utils/genJsonPayload.ts index dc8ba5c5..2e7cebb5 100644 --- a/templates/utils/genJsonPayload.ts +++ b/templates/utils/genJsonPayload.ts @@ -16,7 +16,7 @@ */ const genJsonPayload = (payload: any) => { - let value = { + const value = { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/templates/utils/getFormUrl.ts b/templates/utils/getFormUrl.ts index b0b57a0c..679680bd 100644 --- a/templates/utils/getFormUrl.ts +++ b/templates/utils/getFormUrl.ts @@ -15,26 +15,26 @@ * along with this program. If not, see . */ -/** - * 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 = document.querySelector("form"); - } - if (querySelector === "string" || querySelector instanceof String) { + if (querySelector === undefined) { + form = document.querySelector('form'); + } + if (typeof querySelector == 'string' || querySelector instanceof String) { form = 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"); } diff --git a/templates/utils/getFromUrl.test.ts b/templates/utils/getFromUrl.test.ts new file mode 100644 index 00000000..edc88f8c --- /dev/null +++ b/templates/utils/getFromUrl.test.ts @@ -0,0 +1,66 @@ +/* + * 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 getFormUrl from './getFormUrl'; + +'use strict'; + +const formClassName = 'form__box'; +const formURL = '/api/v1/signin'; + +document.body.innerHTML = ` +
+ + + + +
+`; + +it('getFromUrl workds', () => { + const name = `.${formClassName}`; + expect(getFormUrl(name)).toContain(formURL); + + const form = document.querySelector('form'); + expect(getFormUrl(form)).toContain(formURL); + + expect(getFormUrl()).toContain(formURL); +}); diff --git a/templates/utils/isBlankString.test.ts b/templates/utils/isBlankString.test.ts new file mode 100644 index 00000000..3ce8cc96 --- /dev/null +++ b/templates/utils/isBlankString.test.ts @@ -0,0 +1,33 @@ +/* + * 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 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`); + } +}); diff --git a/templates/utils/isBlankString.ts b/templates/utils/isBlankString.ts index 2946efab..52c22e14 100644 --- a/templates/utils/isBlankString.ts +++ b/templates/utils/isBlankString.ts @@ -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; diff --git a/templates/utils/isNumber.test.ts b/templates/utils/isNumber.test.ts new file mode 100644 index 00000000..8ed7d3e7 --- /dev/null +++ b/templates/utils/isNumber.test.ts @@ -0,0 +1,28 @@ +/* + * 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 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); +});