@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.
- package/README.md +332 -205
- package/example/assets/css/styles.css +45 -45
- package/example/assets/js/app.js +7 -7
- package/example/assets/js/router.min.js +1 -1
- package/example/index.html +26 -26
- package/example/package.json +18 -18
- package/example/server.js +17 -17
- package/index.js +6 -4
- package/package.json +48 -42
- package/src/dto/route.js +13 -13
- package/src/exception.js +5 -5
- package/src/page.js +91 -91
- package/src/route.js +103 -103
- package/src/router.js +207 -207
- package/src/utils/element.js +23 -23
- package/src/utils/selector.js +59 -59
- package/src/view.js +24 -24
- package/tests/unit/element.test.js +32 -32
- package/tests/unit/page.test.js +48 -48
- package/tests/unit/route.test.js +82 -82
- package/tests/unit/router.test.js +55 -55
- package/tests/unit/selector.test.js +42 -42
- package/tests/unit/view.test.js +46 -46
- package/webpack.config.js +23 -0
- package/.babelrc +0 -3
|
@@ -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
|
+
});
|
package/tests/unit/view.test.js
CHANGED
|
@@ -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