@stanlemon/app-template 0.2.1 → 0.2.2

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.2",
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 { 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
+ 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
@@ -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,
@@ -12,6 +13,7 @@ export default function Input({
12
13
  error,
13
14
  }: {
14
15
  type?: string;
16
+ name: string;
15
17
  value?: string;
16
18
  label?: string;
17
19
  placeholder?: string;
@@ -31,9 +33,10 @@ export default function Input({
31
33
 
32
34
  return (
33
35
  <>
34
- {label && <label>{label}</label>}
36
+ {label && <label htmlFor={name}>{label}</label>}
35
37
  <input
36
38
  type={type}
39
+ id={name}
37
40
  onChange={handleChange}
38
41
  onKeyPress={handleKeyPress}
39
42
  placeholder={placeholder}
package/src/Login.tsx CHANGED
@@ -62,11 +62,13 @@ 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}
package/src/Register.tsx CHANGED
@@ -58,12 +58,14 @@ 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}
65
66
  />
66
67
  <Input
68
+ name="password"
67
69
  type="password"
68
70
  label="Password"
69
71
  value={values.password}
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
- }