@aref-shojaei/router 1.0.1 → 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,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.addRoute(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.addRoute(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.addRoute(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.addRoute(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.addRoute(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.addRoute("/", () => "Welcome Page")
74
- Route.addRoute("/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
  })
@@ -1,42 +1,42 @@
1
- import { JSDOM } from "jsdom";
2
- import Selector from "../../src/utils/selector.js";
3
-
4
-
5
- describe("Selector tests", () => {
6
- let documentInstance
7
-
8
- beforeEach(() => {
9
- const { window : { document } } = new JSDOM(`
10
- <html>
11
- <head></head>
12
- <body>
13
- <a href='/'>Home page</a>
14
- <a href='/products'>Products page</a>
15
- <a href='/blog'>Blog 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 Selector).toThrow();
26
- });
27
-
28
- it("should get all anchor elements", () => {
29
- const links = Selector.findAll("a", documentInstance)._getElements();
30
-
31
-
32
- expect(typeof links).toBe("object")
33
- });
34
-
35
- it("should get title of link", () => {
36
- Selector.findAll("a", documentInstance).each(anchor => {
37
- const link = anchor.getAttribute("href")
38
-
39
- expect(typeof link).toBe("string")
40
- })
41
- })
42
- });
1
+ import { JSDOM } from "jsdom";
2
+ import Selector from "../../src/utils/selector.js";
3
+
4
+
5
+ describe("Selector tests", () => {
6
+ let documentInstance
7
+
8
+ beforeEach(() => {
9
+ const { window : { document } } = new JSDOM(`
10
+ <html>
11
+ <head></head>
12
+ <body>
13
+ <a href='/'>Home page</a>
14
+ <a href='/products'>Products page</a>
15
+ <a href='/blog'>Blog 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 Selector).toThrow();
26
+ });
27
+
28
+ it("should get all anchor elements", () => {
29
+ const links = Selector.findAll("a", documentInstance)._getElements();
30
+
31
+
32
+ expect(typeof links).toBe("object")
33
+ });
34
+
35
+ it("should get title of link", () => {
36
+ Selector.findAll("a", documentInstance).each(anchor => {
37
+ const link = anchor.getAttribute("href")
38
+
39
+ expect(typeof link).toBe("string")
40
+ })
41
+ })
42
+ });
@@ -1,47 +1,47 @@
1
- import View from "../../src/view.js"
2
-
3
-
4
- describe("View tests", () => {
5
- it("should prevent instantiation of the class", () => {
6
- expect(() => new View).toThrow()
7
- })
8
-
9
- it("should render a template without params", () => {
10
- const template = () => {
11
- return `
12
- <div>
13
- <h1>Welcome Page</h1>
14
- <p>This is SPA page!</p>
15
- </div>
16
- `
17
- }
18
-
19
- const renderedTemplate = View.render(template)
20
-
21
- expect(typeof renderedTemplate).toBe("string")
22
- expect(renderedTemplate).toContain("<h1>Welcome Page</h1>")
23
- expect(renderedTemplate).toContain("<p>This is SPA page!</p>")
24
- })
25
-
26
- it("should render a template with params", () => {
27
- const template = ({ id, name }) => {
28
- return `
29
- <div>
30
- <h1>User Page</h1>
31
- <p>ID: ${id} - Name: ${name}</p>
32
- </div>
33
- `
34
- }
35
-
36
- const user = {
37
- id : 1,
38
- name : "Robert"
39
- }
40
-
41
- const renderedTemplate = View.render(template, user)
42
-
43
- expect(typeof renderedTemplate).toBe("string")
44
- expect(renderedTemplate).toContain(`<h1>User Page</h1>`)
45
- expect(renderedTemplate).toContain(`<p>ID: ${user.id} - Name: ${user.name}</p>`)
46
- })
1
+ import View from "../../src/view.js"
2
+
3
+
4
+ describe("View tests", () => {
5
+ it("should prevent instantiation of the class", () => {
6
+ expect(() => new View).toThrow()
7
+ })
8
+
9
+ it("should render a template without params", () => {
10
+ const template = () => {
11
+ return `
12
+ <div>
13
+ <h1>Welcome Page</h1>
14
+ <p>This is SPA page!</p>
15
+ </div>
16
+ `
17
+ }
18
+
19
+ const renderedTemplate = View.render(template)
20
+
21
+ expect(typeof renderedTemplate).toBe("string")
22
+ expect(renderedTemplate).toContain("<h1>Welcome Page</h1>")
23
+ expect(renderedTemplate).toContain("<p>This is SPA page!</p>")
24
+ })
25
+
26
+ it("should render a template with params", () => {
27
+ const template = ({ id, name }) => {
28
+ return `
29
+ <div>
30
+ <h1>User Page</h1>
31
+ <p>ID: ${id} - Name: ${name}</p>
32
+ </div>
33
+ `
34
+ }
35
+
36
+ const user = {
37
+ id : 1,
38
+ name : "Robert"
39
+ }
40
+
41
+ const renderedTemplate = View.render(template, user)
42
+
43
+ expect(typeof renderedTemplate).toBe("string")
44
+ expect(renderedTemplate).toContain(`<h1>User Page</h1>`)
45
+ expect(renderedTemplate).toContain(`<p>ID: ${user.id} - Name: ${user.name}</p>`)
46
+ })
47
47
  })
@@ -0,0 +1,23 @@
1
+ const path = require("path");
2
+
3
+ module.exports = {
4
+ entry : path.join(__dirname, "index.js"),
5
+ output : {
6
+ path : path.join(__dirname, "dist"),
7
+ filename : "router.min.js",
8
+ },
9
+ module : {
10
+ rules : [
11
+ {
12
+ test: /\.(js)$/,
13
+ exclude: /node_modules/,
14
+ use : {
15
+ loader : "babel-loader",
16
+ options : {
17
+ presets : ["@babel/preset-env"]
18
+ }
19
+ }
20
+ }
21
+ ]
22
+ }
23
+ }
package/.babelrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "presets": ["@babel/preset-env"]
3
- }