@aref-shojaei/router 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,60 +1,60 @@
1
- import { InvalidArgumentTypeError } from "../exception.js"
2
-
3
- /**
4
- * @abstract
5
- */
6
- export default class Selector {
7
- static #elements = []
8
-
9
-
10
- constructor() {
11
- throw new Error(`${new.target.name} class must not be called with \"new\" keyword!`)
12
- }
13
-
14
- /**
15
- * @param {HTMLAnchorElement} element
16
- * @param {Document} document
17
- * @returns {Selector}
18
- */
19
- static findAll(element, document) {
20
- if (typeof element !== "string") throw new InvalidArgumentTypeError("'element' must be an HTMLElement object!")
21
-
22
- if (typeof document !== "object") throw new InvalidArgumentTypeError("'document' must be a Document object!")
23
-
24
-
25
- const elements = document.querySelectorAll(element)
26
-
27
- this._setElements(elements)
28
-
29
- return this
30
- }
31
-
32
- /**
33
- * @param {function} callback
34
- * @returns {void}
35
- * @returns {void}
36
- */
37
- static each(callback) {
38
- if (typeof callback !== "function") throw new InvalidArgumentTypeError("'callback' must be a function!")
39
-
40
-
41
- const elements = this._getElements()
42
-
43
- if (this.#elements.length) elements.forEach(callback)
44
- }
45
-
46
- /**
47
- * @param {array} elements
48
- * @returns {void}
49
- */
50
- static _setElements(elements) {
51
- this.#elements.push(...elements)
52
- }
53
-
54
- /**
55
- * @returns {array}
56
- */
57
- static _getElements() {
58
- return this.#elements
59
- }
1
+ import { InvalidArgumentTypeError } from "../exception.js"
2
+
3
+ /**
4
+ * @abstract
5
+ */
6
+ export default class Selector {
7
+ static #elements = []
8
+
9
+
10
+ constructor() {
11
+ throw new Error(`${new.target.name} class must not be called with \"new\" keyword!`)
12
+ }
13
+
14
+ /**
15
+ * @param {HTMLAnchorElement} element
16
+ * @param {Document} document
17
+ * @returns {Selector}
18
+ */
19
+ static findAll(element, document) {
20
+ if (typeof element !== "string") throw new InvalidArgumentTypeError("'element' must be an HTMLElement object!")
21
+
22
+ if (typeof document !== "object") throw new InvalidArgumentTypeError("'document' must be a Document object!")
23
+
24
+
25
+ const elements = document.querySelectorAll(element)
26
+
27
+ this._setElements(elements)
28
+
29
+ return this
30
+ }
31
+
32
+ /**
33
+ * @param {function} callback
34
+ * @returns {void}
35
+ * @returns {void}
36
+ */
37
+ static each(callback) {
38
+ if (typeof callback !== "function") throw new InvalidArgumentTypeError("'callback' must be a function!")
39
+
40
+
41
+ const elements = this._getElements()
42
+
43
+ if (this.#elements.length) elements.forEach(callback)
44
+ }
45
+
46
+ /**
47
+ * @param {array} elements
48
+ * @returns {void}
49
+ */
50
+ static _setElements(elements) {
51
+ this.#elements.push(...elements)
52
+ }
53
+
54
+ /**
55
+ * @returns {array}
56
+ */
57
+ static _getElements() {
58
+ return this.#elements
59
+ }
60
60
  }
package/src/view.js CHANGED
@@ -1,25 +1,25 @@
1
- import { InvalidArgumentTypeError } from "./exception.js"
2
-
3
- /**
4
- * @abstract
5
- */
6
- export default class View {
7
- constructor() {
8
- throw new Error(`${new.target.name} class must not be called with \"new\" keyword!`)
9
- }
10
-
11
- /**
12
- * @param {function} template
13
- * @param {object} data
14
- * @returns {string}
15
- */
16
- static render(template, data = {}) {
17
- if (typeof template !== "function") throw new InvalidArgumentTypeError("'template' must be a function!")
18
-
19
- try {
20
- return template(data)
21
- } catch (error) {
22
- console.error("Error during template rendering: ", error);
23
- }
24
- }
1
+ import { InvalidArgumentTypeError } from "./exception.js"
2
+
3
+ /**
4
+ * @abstract
5
+ */
6
+ export default class View {
7
+ constructor() {
8
+ throw new Error(`${new.target.name} class must not be called with \"new\" keyword!`)
9
+ }
10
+
11
+ /**
12
+ * @param {function} template
13
+ * @param {object} data
14
+ * @returns {string}
15
+ */
16
+ static render(template, data = {}) {
17
+ if (typeof template !== "function") throw new InvalidArgumentTypeError("'template' must be a function!")
18
+
19
+ try {
20
+ return template(data)
21
+ } catch (error) {
22
+ console.error("Error during template rendering: ", error);
23
+ }
24
+ }
25
25
  }
@@ -1,33 +1,33 @@
1
- import { JSDOM } from "jsdom"
2
- import Element from "../../src/utils/element"
3
-
4
-
5
- describe("Element tests", () => {
6
- let documentInstance
7
-
8
- beforeEach(() => {
9
- const { window : { document } } = new JSDOM(`
10
- <html>
11
- <head>
12
- <title>SPA Page</title>
13
- </head>
14
- <body>
15
- <a href='/'>Home page</a>
16
- </body>
17
- </html>
18
- `)
19
-
20
- documentInstance = document
21
- })
22
-
23
-
24
- it("should prevent instantiation of the class", () => {
25
- expect(() => new Element).toThrow()
26
- })
27
-
28
- it("should fire click event", () => {
29
- const link = documentInstance.querySelector("a")
30
-
31
- Element.onClick(link , (event) => expect(typeof event).toBe(Event))
32
- })
1
+ import { JSDOM } from "jsdom"
2
+ import Element from "../../src/utils/element"
3
+
4
+
5
+ describe("Element tests", () => {
6
+ let documentInstance
7
+
8
+ beforeEach(() => {
9
+ const { window : { document } } = new JSDOM(`
10
+ <html>
11
+ <head>
12
+ <title>SPA Page</title>
13
+ </head>
14
+ <body>
15
+ <a href='/'>Home page</a>
16
+ </body>
17
+ </html>
18
+ `)
19
+
20
+ documentInstance = document
21
+ })
22
+
23
+
24
+ it("should prevent instantiation of the class", () => {
25
+ expect(() => new Element).toThrow()
26
+ })
27
+
28
+ it("should fire click event", () => {
29
+ const link = documentInstance.querySelector("a")
30
+
31
+ Element.onClick(link , (event) => expect(typeof event).toBe(Event))
32
+ })
33
33
  })
@@ -1,49 +1,49 @@
1
- import { JSDOM } from "jsdom"
2
- import Page from "../../src/page.js"
3
-
4
-
5
- describe("Page tests", () => {
6
- let documentInstance
7
- const titles = {
8
- default : "SPA Page",
9
- custom : "Custom Page",
10
- }
11
-
12
- beforeEach(() => {
13
- const { window : { document } } = new JSDOM(`
14
- <html>
15
- <head>
16
- <title>${titles["default"]}</title>
17
- </head>
18
- <body>
19
- <a href='/'>Home page</a>
20
- </body>
21
- </html>
22
- `)
23
-
24
- documentInstance = document
25
-
26
- Page.setDocument(documentInstance)
27
- })
28
-
29
-
30
- it("should prevent instantiation of the class", () => {
31
- expect(() => new Page).toThrow()
32
- })
33
-
34
- it("should set page default title", () => {
35
- Page.setRootTitle(documentInstance.title)
36
-
37
- expect(Page.getRootTitle()).toBeDefined()
38
- expect(Page.getRootTitle()).toBe(titles["default"])
39
- expect(typeof Page.getRootTitle()).toBe("string")
40
- })
41
-
42
- it("should set custom page title", () => {
43
- Page.setTitle(titles["custom"])
44
-
45
- expect(Page.getTitle()).toBeDefined()
46
- expect(Page.getTitle()).toBe(titles["custom"])
47
- expect(typeof Page.getTitle()).toBe("string")
48
- })
1
+ import { JSDOM } from "jsdom"
2
+ import Page from "../../src/page.js"
3
+
4
+
5
+ describe("Page tests", () => {
6
+ let documentInstance
7
+ const titles = {
8
+ default : "SPA Page",
9
+ custom : "Custom Page",
10
+ }
11
+
12
+ beforeEach(() => {
13
+ const { window : { document } } = new JSDOM(`
14
+ <html>
15
+ <head>
16
+ <title>${titles["default"]}</title>
17
+ </head>
18
+ <body>
19
+ <a href='/'>Home page</a>
20
+ </body>
21
+ </html>
22
+ `)
23
+
24
+ documentInstance = document
25
+
26
+ Page.setDocument(documentInstance)
27
+ })
28
+
29
+
30
+ it("should prevent instantiation of the class", () => {
31
+ expect(() => new Page).toThrow()
32
+ })
33
+
34
+ it("should set page default title", () => {
35
+ Page.setRootTitle(documentInstance.title)
36
+
37
+ expect(Page.getRootTitle()).toBeDefined()
38
+ expect(Page.getRootTitle()).toBe(titles["default"])
39
+ expect(typeof Page.getRootTitle()).toBe("string")
40
+ })
41
+
42
+ it("should set custom page title", () => {
43
+ Page.setTitle(titles["custom"])
44
+
45
+ expect(Page.getTitle()).toBeDefined()
46
+ expect(Page.getTitle()).toBe(titles["custom"])
47
+ expect(typeof Page.getTitle()).toBe("string")
48
+ })
49
49
  })
@@ -1,83 +1,83 @@
1
- import Route from "../../src/route.js"
2
-
3
-
4
- describe("Route tests", () => {
5
- it("should add a single route", () => {
6
- const route = "/"
7
- const template = () => "Welcome Page"
8
-
9
- Route.add(route, template)
10
-
11
- const routes = Route._routes
12
-
13
- expect(routes[route]).toBeDefined()
14
- })
15
-
16
- it("should add the group route", () => {
17
- const routePrefix = "/auth"
18
- const route = "/login"
19
- const template = () => "Login Page"
20
-
21
- Route.group(routePrefix, () => {
22
- Route.add(route, template)
23
- })
24
-
25
- const routes = Route._routes
26
-
27
- expect(routes[routePrefix + route]).toBeDefined()
28
- expect(typeof routes[routePrefix + route]).toBe("object")
29
- })
30
-
31
- it("should add a dynamic route", () => {
32
- const route = "/users/{id}"
33
- const template = ({ params: { id } }) => `User #${id}`
34
-
35
- Route.add(route, template)
36
-
37
- const routes = Route._routes
38
-
39
- expect(routes[route]).toBeDefined()
40
- expect(typeof routes[route]).toBe("object")
41
- })
42
-
43
- it("should add middleware to a route", () => {
44
- const route = "/admin"
45
- const template = () => "Admin Page"
46
- const loggerMiddleware = () => { message : "Custom Log Message!" }
47
-
48
- Route.add(route, template).middleware([loggerMiddleware])
49
-
50
- const routes = Route._routes
51
-
52
- expect(routes[route]["middlewares"]).toBeDefined()
53
- expect(typeof routes[route]["middlewares"]).toBe("object")
54
- })
55
-
56
- it("should add page title to a route", () => {
57
- const route = "/product"
58
- const template = () => "SPA Page"
59
-
60
- Route.add(route, template).title("Custom page title (SPA)")
61
-
62
- const routes = Route._routes
63
-
64
- expect(routes[route]["title"]).toBeDefined()
65
- expect(typeof routes[route]["title"]).toBe("string")
66
- })
67
-
68
- it("should redirect route", () => {
69
- const welcomeRoute = "/"
70
- const redirectionRoute = "/redirection"
71
- const distRoute = welcomeRoute
72
-
73
- Route.add("/", () => "Welcome Page")
74
- Route.add("/redirection", () => Route.redirect(distRoute))
75
-
76
- const routes = Route._routes
77
-
78
- expect(routes[redirectionRoute]).toBeDefined()
79
- expect(typeof routes[redirectionRoute]).toBe("object")
80
- expect(routes[welcomeRoute]).toBeDefined()
81
- expect(typeof routes[welcomeRoute]).toBe("object")
82
- })
1
+ import Route from "../../src/route.js"
2
+
3
+
4
+ describe("Route tests", () => {
5
+ it("should add a single route", () => {
6
+ const route = "/"
7
+ const template = () => "Welcome Page"
8
+
9
+ Route.add(route, template)
10
+
11
+ const routes = Route._routes
12
+
13
+ expect(routes[route]).toBeDefined()
14
+ })
15
+
16
+ it("should add the group route", () => {
17
+ const routePrefix = "/auth"
18
+ const route = "/login"
19
+ const template = () => "Login Page"
20
+
21
+ Route.group(routePrefix, () => {
22
+ Route.add(route, template)
23
+ })
24
+
25
+ const routes = Route._routes
26
+
27
+ expect(routes[routePrefix + route]).toBeDefined()
28
+ expect(typeof routes[routePrefix + route]).toBe("object")
29
+ })
30
+
31
+ it("should add a dynamic route", () => {
32
+ const route = "/users/{id}"
33
+ const template = ({ params: { id } }) => `User #${id}`
34
+
35
+ Route.add(route, template)
36
+
37
+ const routes = Route._routes
38
+
39
+ expect(routes[route]).toBeDefined()
40
+ expect(typeof routes[route]).toBe("object")
41
+ })
42
+
43
+ it("should add middleware to a route", () => {
44
+ const route = "/admin"
45
+ const template = () => "Admin Page"
46
+ const loggerMiddleware = () => { message : "Custom Log Message!" }
47
+
48
+ Route.add(route, template).middleware([loggerMiddleware])
49
+
50
+ const routes = Route._routes
51
+
52
+ expect(routes[route]["middlewares"]).toBeDefined()
53
+ expect(typeof routes[route]["middlewares"]).toBe("object")
54
+ })
55
+
56
+ it("should add page title to a route", () => {
57
+ const route = "/product"
58
+ const template = () => "SPA Page"
59
+
60
+ Route.add(route, template).title("Custom page title (SPA)")
61
+
62
+ const routes = Route._routes
63
+
64
+ expect(routes[route]["title"]).toBeDefined()
65
+ expect(typeof routes[route]["title"]).toBe("string")
66
+ })
67
+
68
+ it("should redirect route", () => {
69
+ const welcomeRoute = "/"
70
+ const redirectionRoute = "/redirection"
71
+ const distRoute = welcomeRoute
72
+
73
+ Route.add("/", () => "Welcome Page")
74
+ Route.add("/redirection", () => Route.redirect(distRoute))
75
+
76
+ const routes = Route._routes
77
+
78
+ expect(routes[redirectionRoute]).toBeDefined()
79
+ expect(typeof routes[redirectionRoute]).toBe("object")
80
+ expect(routes[welcomeRoute]).toBeDefined()
81
+ expect(typeof routes[welcomeRoute]).toBe("object")
82
+ })
83
83
  })
@@ -1,56 +1,56 @@
1
- import { JSDOM } from "jsdom"
2
- import Router from "../../src/router.js"
3
-
4
-
5
- describe("Router tests", () => {
6
- let documentInstance
7
- let windowtInstance
8
- let selectorID
9
-
10
- beforeEach(() => {
11
- const { window } = new JSDOM(`
12
- <html>
13
- <head>
14
- <title>SPA Page</title>
15
- </head>
16
- <body>
17
- <div id='root'></div>
18
- </body>
19
- </html>
20
- `)
21
-
22
- const { document } = window
23
-
24
-
25
- documentInstance = document
26
- windowtInstance = window
27
- selectorID = "#root"
28
- })
29
-
30
-
31
- it("should prevent instantiation of the class", () => {
32
- expect(() => new Router).toThrow()
33
- })
34
-
35
- it("should configure router", () => {
36
- expect(() => {
37
- Router.configure({
38
- window : windowtInstance,
39
- document : documentInstance,
40
- selector : selectorID
41
- })
42
- }).not.toThrow()
43
- })
44
-
45
- it("should run router", () => {
46
- expect(() => {
47
- Router.configure({
48
- window : windowtInstance,
49
- document : documentInstance,
50
- selector : selectorID
51
- })
52
-
53
- Router.run()
54
- }).not.toThrow()
55
- })
1
+ import { JSDOM } from "jsdom"
2
+ import Router from "../../src/router.js"
3
+
4
+
5
+ describe("Router tests", () => {
6
+ let documentInstance
7
+ let windowtInstance
8
+ let selectorID
9
+
10
+ beforeEach(() => {
11
+ const { window } = new JSDOM(`
12
+ <html>
13
+ <head>
14
+ <title>SPA Page</title>
15
+ </head>
16
+ <body>
17
+ <div id='root'></div>
18
+ </body>
19
+ </html>
20
+ `)
21
+
22
+ const { document } = window
23
+
24
+
25
+ documentInstance = document
26
+ windowtInstance = window
27
+ selectorID = "#root"
28
+ })
29
+
30
+
31
+ it("should prevent instantiation of the class", () => {
32
+ expect(() => new Router).toThrow()
33
+ })
34
+
35
+ it("should configure router", () => {
36
+ expect(() => {
37
+ Router.configure({
38
+ window : windowtInstance,
39
+ document : documentInstance,
40
+ selector : selectorID
41
+ })
42
+ }).not.toThrow()
43
+ })
44
+
45
+ it("should run router", () => {
46
+ expect(() => {
47
+ Router.configure({
48
+ window : windowtInstance,
49
+ document : documentInstance,
50
+ selector : selectorID
51
+ })
52
+
53
+ Router.run()
54
+ }).not.toThrow()
55
+ })
56
56
  })