@stanlemon/app-template 0.2.0 → 0.2.3
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/app.js +1 -6
- package/package.json +2 -1
- package/src/App.test.tsx +34 -7
- package/src/App.tsx +13 -1
- package/src/Input.tsx +7 -1
- package/src/Login.tsx +3 -0
- package/src/Register.tsx +8 -20
- package/src/index.tsx +15 -2
- package/db.json +0 -10
- package/index.html +0 -18
package/app.js
CHANGED
|
@@ -4,12 +4,7 @@ import {
|
|
|
4
4
|
SimpleUsersDao,
|
|
5
5
|
} from "@stanlemon/server-with-auth";
|
|
6
6
|
|
|
7
|
-
const users = new SimpleUsersDao(
|
|
8
|
-
{
|
|
9
|
-
username: "user",
|
|
10
|
-
password: "password",
|
|
11
|
-
},
|
|
12
|
-
]);
|
|
7
|
+
const users = new SimpleUsersDao();
|
|
13
8
|
|
|
14
9
|
const app = createAppServer({
|
|
15
10
|
webpack: "http://localhost:8080",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stanlemon/app-template",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "A template for creating apps using the webdev package.",
|
|
5
5
|
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"webpack:serve": "webpack serve",
|
|
12
12
|
"webpack:build": "NODE_ENV=production webpack",
|
|
13
13
|
"test": "jest",
|
|
14
|
+
"test:watch": "jest -w",
|
|
14
15
|
"test:coverage": "jest --coverage",
|
|
15
16
|
"lint": "eslint --ext js,jsx,ts,tsx ./src/",
|
|
16
17
|
"lint:format": "eslint --fix --ext js,jsx,ts,tsx ./src/"
|
package/src/App.test.tsx
CHANGED
|
@@ -1,18 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
render,
|
|
3
|
+
screen,
|
|
4
|
+
fireEvent,
|
|
5
|
+
waitFor,
|
|
6
|
+
act,
|
|
7
|
+
} from "@testing-library/react";
|
|
2
8
|
import userEvent from "@testing-library/user-event";
|
|
3
9
|
import App from "./App";
|
|
4
10
|
|
|
5
|
-
|
|
6
|
-
|
|
11
|
+
global.fetch = jest.fn(() =>
|
|
12
|
+
Promise.resolve({
|
|
13
|
+
ok: true,
|
|
14
|
+
json: () =>
|
|
15
|
+
Promise.resolve({
|
|
16
|
+
token: "token",
|
|
17
|
+
user: {
|
|
18
|
+
name: "Test Tester",
|
|
19
|
+
email: "test@test.com",
|
|
20
|
+
username: "test",
|
|
21
|
+
password: "password",
|
|
22
|
+
},
|
|
23
|
+
}),
|
|
24
|
+
})
|
|
25
|
+
) as jest.Mock;
|
|
7
26
|
|
|
8
|
-
|
|
9
|
-
|
|
27
|
+
test("<App/>", async () => {
|
|
28
|
+
act(() => {
|
|
29
|
+
render(<App />);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// A fetch request will be made, and then the page will be initialized, wait for that
|
|
33
|
+
await waitFor(() => {
|
|
34
|
+
// The header is present
|
|
35
|
+
expect(screen.getByRole("heading")).toHaveTextContent("Hello World!");
|
|
36
|
+
});
|
|
10
37
|
|
|
11
38
|
// Type some data into the input
|
|
12
|
-
userEvent.type(screen.
|
|
39
|
+
await userEvent.type(screen.getByLabelText("Item"), "The first item");
|
|
13
40
|
|
|
14
41
|
// Click the add button
|
|
15
|
-
fireEvent.click(screen.
|
|
42
|
+
fireEvent.click(screen.getByText("Add", { selector: "button" }));
|
|
16
43
|
|
|
17
44
|
// Now we should have a list item with the text we entered
|
|
18
45
|
expect(screen.getByRole("listitem")).toHaveTextContent("The first item");
|
package/src/App.tsx
CHANGED
|
@@ -30,6 +30,7 @@ export type User = {
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
export default function App() {
|
|
33
|
+
const [initialized, setInitialized] = useState<boolean>(false);
|
|
33
34
|
const [session, setSession] = useState<Session | null>(null);
|
|
34
35
|
const [value, setValue] = useState<string>("");
|
|
35
36
|
const [items, setItems] = useState<string[]>([]);
|
|
@@ -45,6 +46,8 @@ export default function App() {
|
|
|
45
46
|
},
|
|
46
47
|
})
|
|
47
48
|
.then((response) => {
|
|
49
|
+
setInitialized(true);
|
|
50
|
+
|
|
48
51
|
if (!response.ok) {
|
|
49
52
|
throw new Error(response.statusText);
|
|
50
53
|
}
|
|
@@ -57,13 +60,21 @@ export default function App() {
|
|
|
57
60
|
.catch((err) => {
|
|
58
61
|
console.error(err);
|
|
59
62
|
});
|
|
60
|
-
}, [session?.token]);
|
|
63
|
+
}, [session?.token, initialized]);
|
|
61
64
|
|
|
62
65
|
const addItem = () => {
|
|
63
66
|
setItems([...items, value]);
|
|
64
67
|
setValue("");
|
|
65
68
|
};
|
|
66
69
|
|
|
70
|
+
if (!initialized) {
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<em>Loading...</em>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
67
78
|
return (
|
|
68
79
|
<SessionContext.Provider value={contextValue}>
|
|
69
80
|
<Header />
|
|
@@ -87,6 +98,7 @@ export default function App() {
|
|
|
87
98
|
</div>
|
|
88
99
|
<Input
|
|
89
100
|
label="Item"
|
|
101
|
+
name="item"
|
|
90
102
|
value={value}
|
|
91
103
|
onChange={(value) => setValue(value)}
|
|
92
104
|
onEnter={addItem}
|
package/src/Input.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export default function Input({
|
|
2
2
|
type = "text",
|
|
3
|
+
name,
|
|
3
4
|
value = "",
|
|
4
5
|
label,
|
|
5
6
|
placeholder,
|
|
@@ -9,13 +10,16 @@ export default function Input({
|
|
|
9
10
|
onEnter = () => {
|
|
10
11
|
/* noop */
|
|
11
12
|
},
|
|
13
|
+
error,
|
|
12
14
|
}: {
|
|
13
15
|
type?: string;
|
|
16
|
+
name: string;
|
|
14
17
|
value?: string;
|
|
15
18
|
label?: string;
|
|
16
19
|
placeholder?: string;
|
|
17
20
|
onChange?: (value: string) => void;
|
|
18
21
|
onEnter?: () => void;
|
|
22
|
+
error?: string;
|
|
19
23
|
}) {
|
|
20
24
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
|
|
21
25
|
onChange(e.currentTarget.value);
|
|
@@ -29,14 +33,16 @@ export default function Input({
|
|
|
29
33
|
|
|
30
34
|
return (
|
|
31
35
|
<>
|
|
32
|
-
{label && <label>{label}</label>}
|
|
36
|
+
{label && <label htmlFor={name}>{label}</label>}
|
|
33
37
|
<input
|
|
34
38
|
type={type}
|
|
39
|
+
id={name}
|
|
35
40
|
onChange={handleChange}
|
|
36
41
|
onKeyPress={handleKeyPress}
|
|
37
42
|
placeholder={placeholder}
|
|
38
43
|
value={value}
|
|
39
44
|
/>
|
|
45
|
+
{error && <div>{error}</div>}
|
|
40
46
|
</>
|
|
41
47
|
);
|
|
42
48
|
}
|
package/src/Login.tsx
CHANGED
|
@@ -62,15 +62,18 @@ export default function Login() {
|
|
|
62
62
|
</div>
|
|
63
63
|
)}
|
|
64
64
|
<Input
|
|
65
|
+
name="username"
|
|
65
66
|
label="Username"
|
|
66
67
|
value={values.username}
|
|
67
68
|
onChange={(value) => setValues({ ...values, username: value })}
|
|
68
69
|
/>
|
|
69
70
|
<Input
|
|
71
|
+
name="password"
|
|
70
72
|
type="password"
|
|
71
73
|
label="Password"
|
|
72
74
|
value={values.password}
|
|
73
75
|
onChange={(value) => setValues({ ...values, password: value })}
|
|
76
|
+
onEnter={onSubmit}
|
|
74
77
|
/>
|
|
75
78
|
<button onClick={onSubmit}>Login</button>
|
|
76
79
|
</div>
|
package/src/Register.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import Input from "./Input";
|
|
|
4
4
|
|
|
5
5
|
// eslint-disable-next-line max-lines-per-function
|
|
6
6
|
export default function Register() {
|
|
7
|
-
const [
|
|
7
|
+
const [errors, setErrors] = useState<Record<string, string>>({});
|
|
8
8
|
const [values, setValues] = useState<User>({
|
|
9
9
|
name: "",
|
|
10
10
|
email: "",
|
|
@@ -17,7 +17,7 @@ export default function Register() {
|
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
const onSubmit = () => {
|
|
20
|
-
|
|
20
|
+
setErrors({});
|
|
21
21
|
fetch("/auth/register", {
|
|
22
22
|
headers: {
|
|
23
23
|
Accept: "application/json",
|
|
@@ -43,10 +43,10 @@ export default function Register() {
|
|
|
43
43
|
status: number;
|
|
44
44
|
data: Record<string, unknown>;
|
|
45
45
|
}) => {
|
|
46
|
-
console.log(ok, status, data);
|
|
47
|
-
|
|
48
46
|
if (ok) {
|
|
49
47
|
setSession(data as Session);
|
|
48
|
+
} else {
|
|
49
|
+
setErrors((data as FormErrors).errors);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
)
|
|
@@ -57,32 +57,20 @@ export default function Register() {
|
|
|
57
57
|
|
|
58
58
|
return (
|
|
59
59
|
<div>
|
|
60
|
-
{error && (
|
|
61
|
-
<div>
|
|
62
|
-
<strong>{error}</strong>
|
|
63
|
-
</div>
|
|
64
|
-
)}
|
|
65
|
-
<Input
|
|
66
|
-
label="Name"
|
|
67
|
-
value={values.name || ""}
|
|
68
|
-
onChange={(value) => setValues({ ...values, name: value })}
|
|
69
|
-
/>
|
|
70
|
-
<Input
|
|
71
|
-
type="email"
|
|
72
|
-
label="Email"
|
|
73
|
-
value={values.email || ""}
|
|
74
|
-
onChange={(value) => setValues({ ...values, email: value })}
|
|
75
|
-
/>
|
|
76
60
|
<Input
|
|
61
|
+
name="username"
|
|
77
62
|
label="Username"
|
|
78
63
|
value={values.username}
|
|
79
64
|
onChange={(value) => setValues({ ...values, username: value })}
|
|
65
|
+
error={errors.username}
|
|
80
66
|
/>
|
|
81
67
|
<Input
|
|
68
|
+
name="password"
|
|
82
69
|
type="password"
|
|
83
70
|
label="Password"
|
|
84
71
|
value={values.password}
|
|
85
72
|
onChange={(value) => setValues({ ...values, password: value })}
|
|
73
|
+
error={errors.password}
|
|
86
74
|
/>
|
|
87
75
|
<button onClick={onSubmit}>Register</button>
|
|
88
76
|
</div>
|
package/src/index.tsx
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createRoot } from "react-dom/client";
|
|
2
2
|
import App from "./App";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const root = createRoot(
|
|
5
|
+
document.body.appendChild(document.createElement("div"))
|
|
6
|
+
);
|
|
7
|
+
root.render(<App />);
|
|
8
|
+
|
|
9
|
+
const link = document.createElement("link");
|
|
10
|
+
link.setAttribute("rel", "stylesheet");
|
|
11
|
+
link.setAttribute("type", "text/css");
|
|
12
|
+
link.setAttribute(
|
|
13
|
+
"href",
|
|
14
|
+
"https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css"
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
document.getElementsByTagName("head")[0].appendChild(link);
|
package/db.json
DELETED
package/index.html
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
|
|
4
|
-
<head>
|
|
5
|
-
<meta charset="utf-8">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
7
|
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
|
|
8
|
-
<title>App</title>
|
|
9
|
-
</head>
|
|
10
|
-
|
|
11
|
-
<body>
|
|
12
|
-
<noscript>
|
|
13
|
-
You need to enable JavaScript to run this app.
|
|
14
|
-
</noscript>
|
|
15
|
-
<div id="root"></div>
|
|
16
|
-
</body>
|
|
17
|
-
|
|
18
|
-
</html>
|