@stanlemon/app-template 0.2.1 → 0.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stanlemon/app-template",
3
- "version": "0.2.1",
3
+ "version": "0.2.4",
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",
@@ -8,9 +8,11 @@
8
8
  "scripts": {
9
9
  "start": "node app.js",
10
10
  "build": "npm run webpack:build",
11
+ "tsc": "tsc",
11
12
  "webpack:serve": "webpack serve",
12
13
  "webpack:build": "NODE_ENV=production webpack",
13
14
  "test": "jest",
15
+ "test:watch": "jest -w",
14
16
  "test:coverage": "jest --coverage",
15
17
  "lint": "eslint --ext js,jsx,ts,tsx ./src/",
16
18
  "lint:format": "eslint --fix --ext js,jsx,ts,tsx ./src/"
package/src/App.test.tsx CHANGED
@@ -1,18 +1,45 @@
1
- import { render, screen, fireEvent } from "@testing-library/react";
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
- test.skip("<App/>", () => {
6
- render(<App />);
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
- // The header is present
9
- expect(screen.getByRole("heading")).toHaveTextContent("Hello World!");
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.getByRole("textbox"), "The first item");
39
+ await userEvent.type(screen.getByLabelText("Item"), "The first item");
13
40
 
14
41
  // Click the add button
15
- fireEvent.click(screen.getByRole("button"));
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
@@ -22,6 +22,7 @@ export type Session = {
22
22
  token: string | null;
23
23
  user: User | null;
24
24
  };
25
+
25
26
  export type User = {
26
27
  name: string | null;
27
28
  email: string | null;
@@ -30,6 +31,7 @@ export type User = {
30
31
  };
31
32
 
32
33
  export default function App() {
34
+ const [initialized, setInitialized] = useState<boolean>(false);
33
35
  const [session, setSession] = useState<Session | null>(null);
34
36
  const [value, setValue] = useState<string>("");
35
37
  const [items, setItems] = useState<string[]>([]);
@@ -45,6 +47,8 @@ export default function App() {
45
47
  },
46
48
  })
47
49
  .then((response) => {
50
+ setInitialized(true);
51
+
48
52
  if (!response.ok) {
49
53
  throw new Error(response.statusText);
50
54
  }
@@ -57,51 +61,86 @@ export default function App() {
57
61
  .catch((err) => {
58
62
  console.error(err);
59
63
  });
60
- }, [session?.token]);
64
+ }, [session?.token, initialized]);
61
65
 
62
66
  const addItem = () => {
63
67
  setItems([...items, value]);
64
68
  setValue("");
65
69
  };
66
70
 
71
+ if (!initialized) {
72
+ return (
73
+ <div>
74
+ <em>Loading...</em>
75
+ </div>
76
+ );
77
+ }
78
+
67
79
  return (
68
80
  <SessionContext.Provider value={contextValue}>
69
81
  <Header />
70
- <div>
71
- {!session && (
72
- <>
73
- <p>
74
- <em>You are not currently logged in.</em>
75
- </p>
82
+ {!session && (
83
+ <Row>
84
+ <Column>
85
+ <h2>Login</h2>
76
86
  <Login />
77
- <Spacer />
87
+ </Column>
88
+ <Column>
89
+ <h2>Register</h2>
78
90
  <Register />
79
- <Spacer />
80
- </>
81
- )}
82
- {session?.user && (
91
+ </Column>
92
+ </Row>
93
+ )}
94
+ {session?.user && (
95
+ <>
83
96
  <p>
84
97
  <em>You logged in as {session.user.username}.</em>
85
98
  </p>
86
- )}
87
- </div>
88
- <Input
89
- label="Item"
90
- value={value}
91
- onChange={(value) => setValue(value)}
92
- onEnter={addItem}
93
- />
94
- <button onClick={addItem}>Add</button>
95
-
96
- <ul>
97
- {items.map((item, i) => (
98
- <li key={i}>{item}</li>
99
- ))}
100
- </ul>
99
+ <Input
100
+ label="Item"
101
+ name="item"
102
+ value={value}
103
+ onChange={(value) => setValue(value)}
104
+ onEnter={addItem}
105
+ />
106
+ <button onClick={addItem}>Add</button>
107
+ <ul>
108
+ {items.map((item, i) => (
109
+ <li key={i}>{item}</li>
110
+ ))}
111
+ </ul>
112
+ </>
113
+ )}
101
114
  </SessionContext.Provider>
102
115
  );
103
116
  }
104
117
 
105
- function Spacer() {
106
- return <div style={{ minHeight: "2em" }} />;
118
+ function Row({ children }: { children: React.ReactNode }) {
119
+ return (
120
+ <div
121
+ style={{
122
+ display: "flex",
123
+ flexDirection: "row",
124
+ flexWrap: "wrap",
125
+ width: "100%",
126
+ }}
127
+ >
128
+ {children}
129
+ </div>
130
+ );
131
+ }
132
+
133
+ function Column({ children }: { children: React.ReactNode }) {
134
+ return (
135
+ <div
136
+ style={{
137
+ display: "flex",
138
+ flexDirection: "column",
139
+ flexBasis: "100%",
140
+ flex: 1,
141
+ }}
142
+ >
143
+ {children}
144
+ </div>
145
+ );
107
146
  }
package/src/Input.tsx CHANGED
@@ -1,5 +1,19 @@
1
+ export interface Props
2
+ // Inherit everything from input except onChange which we simplify here
3
+ extends Omit<React.ComponentPropsWithRef<"input">, "onChange"> {
4
+ type?: string;
5
+ name: string;
6
+ value?: string;
7
+ label?: string;
8
+ placeholder?: string;
9
+ onChange?: (value: string) => void;
10
+ onEnter?: () => void;
11
+ error?: string;
12
+ }
13
+
1
14
  export default function Input({
2
15
  type = "text",
16
+ name,
3
17
  value = "",
4
18
  label,
5
19
  placeholder,
@@ -10,15 +24,8 @@ export default function Input({
10
24
  /* noop */
11
25
  },
12
26
  error,
13
- }: {
14
- type?: string;
15
- value?: string;
16
- label?: string;
17
- placeholder?: string;
18
- onChange?: (value: string) => void;
19
- onEnter?: () => void;
20
- error?: string;
21
- }) {
27
+ ...attrs
28
+ }: Props) {
22
29
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
23
30
  onChange(e.currentTarget.value);
24
31
  };
@@ -31,13 +38,15 @@ export default function Input({
31
38
 
32
39
  return (
33
40
  <>
34
- {label && <label>{label}</label>}
41
+ {label && <label htmlFor={name}>{label}</label>}
35
42
  <input
36
43
  type={type}
44
+ id={name}
37
45
  onChange={handleChange}
38
46
  onKeyPress={handleKeyPress}
39
47
  placeholder={placeholder}
40
48
  value={value}
49
+ {...attrs}
41
50
  />
42
51
  {error && <div>{error}</div>}
43
52
  </>
package/src/Login.tsx CHANGED
@@ -62,11 +62,14 @@ 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 })}
69
+ autoCapitalize="off"
68
70
  />
69
71
  <Input
72
+ name="password"
70
73
  type="password"
71
74
  label="Password"
72
75
  value={values.password}
package/src/Register.tsx CHANGED
@@ -58,12 +58,15 @@ export default function Register() {
58
58
  return (
59
59
  <div>
60
60
  <Input
61
+ name="username"
61
62
  label="Username"
62
63
  value={values.username}
63
64
  onChange={(value) => setValues({ ...values, username: value })}
64
65
  error={errors.username}
66
+ autoCapitalize="off"
65
67
  />
66
68
  <Input
69
+ name="password"
67
70
  type="password"
68
71
  label="Password"
69
72
  value={values.password}
package/src/index.tsx CHANGED
@@ -1,4 +1,17 @@
1
- import ReactDOM from "react-dom";
1
+ import { createRoot } from "react-dom/client";
2
2
  import App from "./App";
3
3
 
4
- ReactDOM.render(<App />, document.getElementById("root"));
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
@@ -1,12 +0,0 @@
1
- {
2
- "users": [
3
- {
4
- "username": "user",
5
- "password": "$2a$10$5oHnfhEv5ezdqcVf7k1KNeQ2VVX4Jv0p5tZmhroqbS117umUBrIJO",
6
- "id": "defb1788-f2b9-4f74-9a72-f397788592bb",
7
- "verification_token": "BkWPUmVb9",
8
- "created_at": "2022-03-08T00:42:01.041Z",
9
- "last_updated": "2022-03-08T00:42:01.041Z"
10
- }
11
- ]
12
- }
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>