@mblaney/holster-browser 2.0.1 → 2.0.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.
@@ -0,0 +1,62 @@
1
+ import {describe, it, expect, beforeEach, vi} from "vitest"
2
+ import {render, screen, act} from "@testing-library/react"
3
+ import userEvent from "@testing-library/user-event"
4
+ import RequestCode from "../RequestCode.jsx"
5
+
6
+ function mockFetchResponse(ok, text) {
7
+ return Promise.resolve({ok, text: () => Promise.resolve(text)})
8
+ }
9
+
10
+ describe("RequestCode Component", () => {
11
+ beforeEach(() => {
12
+ vi.stubGlobal("fetch", vi.fn())
13
+ })
14
+
15
+ it("renders the request form", () => {
16
+ render(<RequestCode />)
17
+ expect(screen.getByText("Request login code")).toBeTruthy()
18
+ expect(screen.getByLabelText("Email")).toBeTruthy()
19
+ })
20
+
21
+ it("requires an email", async () => {
22
+ render(<RequestCode />)
23
+ await act(async () => {
24
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
25
+ })
26
+ expect(screen.getByText("Please provide your email")).toBeTruthy()
27
+ expect(fetch).not.toHaveBeenCalled()
28
+ })
29
+
30
+ it("requests a code and clears the email on success", async () => {
31
+ fetch.mockReturnValue(mockFetchResponse(true, "Login code sent"))
32
+ render(<RequestCode />)
33
+
34
+ await act(async () => {
35
+ await userEvent.type(screen.getByLabelText("Email"), "alice@test.com")
36
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
37
+ })
38
+
39
+ expect(fetch).toHaveBeenCalledWith(
40
+ expect.stringContaining("/request-login-code"),
41
+ expect.objectContaining({
42
+ method: "POST",
43
+ body: JSON.stringify({email: "alice@test.com"}),
44
+ }),
45
+ )
46
+ expect(screen.getByText("Login code sent")).toBeTruthy()
47
+ expect(screen.getByLabelText("Email").value).toBe("")
48
+ })
49
+
50
+ it("shows the error and keeps the email on failure", async () => {
51
+ fetch.mockReturnValue(mockFetchResponse(false, "Email not found"))
52
+ render(<RequestCode />)
53
+
54
+ await act(async () => {
55
+ await userEvent.type(screen.getByLabelText("Email"), "alice@test.com")
56
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
57
+ })
58
+
59
+ expect(screen.getByText("Email not found")).toBeTruthy()
60
+ expect(screen.getByLabelText("Email").value).toBe("alice@test.com")
61
+ })
62
+ })
@@ -0,0 +1,56 @@
1
+ import {describe, it, expect, beforeEach, vi} from "vitest"
2
+ import {render, screen, act} from "@testing-library/react"
3
+ import userEvent from "@testing-library/user-event"
4
+ import ResetPassword from "../ResetPassword.jsx"
5
+
6
+ function mockFetchResponse(ok, text) {
7
+ return Promise.resolve({ok, text: () => Promise.resolve(text)})
8
+ }
9
+
10
+ describe("ResetPassword Component", () => {
11
+ beforeEach(() => {
12
+ vi.stubGlobal("fetch", vi.fn())
13
+ })
14
+
15
+ it("renders the reset form", () => {
16
+ render(<ResetPassword />)
17
+ expect(screen.getByText("Reset Password")).toBeTruthy()
18
+ expect(screen.getByLabelText("Email")).toBeTruthy()
19
+ expect(screen.getByLabelText("Login code")).toBeTruthy()
20
+ })
21
+
22
+ it("shows already logged in and disables submit when loggedIn is true", () => {
23
+ render(<ResetPassword loggedIn={true} />)
24
+ expect(screen.getByText("Already logged in")).toBeTruthy()
25
+ expect(screen.getByRole("button", {name: "Submit"})).toBeDisabled()
26
+ })
27
+
28
+ it("requires an email", async () => {
29
+ render(<ResetPassword />)
30
+ await act(async () => {
31
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
32
+ })
33
+ expect(screen.getByText("Please provide your email")).toBeTruthy()
34
+ expect(fetch).not.toHaveBeenCalled()
35
+ })
36
+
37
+ it("submits the email and code, and shows the response message", async () => {
38
+ fetch.mockReturnValue(mockFetchResponse(true, "Check your email"))
39
+ render(<ResetPassword />)
40
+
41
+ await act(async () => {
42
+ await userEvent.type(screen.getByLabelText("Email"), "alice@test.com")
43
+ await userEvent.type(screen.getByLabelText("Login code"), "abc123")
44
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
45
+ })
46
+
47
+ expect(fetch).toHaveBeenCalledWith(
48
+ expect.stringContaining("/reset-password"),
49
+ expect.objectContaining({
50
+ method: "POST",
51
+ body: JSON.stringify({email: "alice@test.com", code: "abc123"}),
52
+ }),
53
+ )
54
+ expect(screen.getByText("Check your email")).toBeTruthy()
55
+ })
56
+ })
@@ -0,0 +1,172 @@
1
+ import {describe, it, expect, beforeEach, vi} from "vitest"
2
+ import {render, screen, act} from "@testing-library/react"
3
+ import userEvent from "@testing-library/user-event"
4
+ import HomeIcon from "@mui/icons-material/Home"
5
+ import SearchAppBar from "../SearchAppBar.jsx"
6
+
7
+ describe("SearchAppBar Component", () => {
8
+ beforeEach(() => {
9
+ delete window.location
10
+ window.location = {href: ""}
11
+ })
12
+
13
+ it("renders the app name when no title is given", () => {
14
+ render(<SearchAppBar name="MyApp" mode="light" setMode={() => {}} />)
15
+ expect(screen.getByText("MyApp")).toBeTruthy()
16
+ })
17
+
18
+ it("renders a clickable title instead of the name when given", async () => {
19
+ const onTitleClick = vi.fn()
20
+ render(
21
+ <SearchAppBar
22
+ name="MyApp"
23
+ title="My Project"
24
+ onTitleClick={onTitleClick}
25
+ mode="light"
26
+ setMode={() => {}}
27
+ />,
28
+ )
29
+ expect(screen.queryByText("MyApp")).toBeNull()
30
+ await act(async () => {
31
+ await userEvent.click(screen.getByText("My Project"))
32
+ })
33
+ expect(onTitleClick).toHaveBeenCalled()
34
+ })
35
+
36
+ it("navigates home on icon click by default, or calls onHomeClick when given", async () => {
37
+ const onHomeClick = vi.fn()
38
+ const {rerender} = render(
39
+ <SearchAppBar
40
+ name="MyApp"
41
+ icon={HomeIcon}
42
+ onHomeClick={onHomeClick}
43
+ mode="light"
44
+ setMode={() => {}}
45
+ />,
46
+ )
47
+ await act(async () => {
48
+ await userEvent.click(screen.getByLabelText("home"))
49
+ })
50
+ expect(onHomeClick).toHaveBeenCalled()
51
+ expect(window.location).toEqual({href: ""})
52
+
53
+ rerender(
54
+ <SearchAppBar
55
+ name="MyApp"
56
+ icon={HomeIcon}
57
+ mode="light"
58
+ setMode={() => {}}
59
+ />,
60
+ )
61
+ await act(async () => {
62
+ await userEvent.click(screen.getByLabelText("home"))
63
+ })
64
+ expect(window.location).toBe("/")
65
+ })
66
+
67
+ it("calls onSearch only once the query reaches 3 characters", async () => {
68
+ const onSearch = vi.fn()
69
+ render(
70
+ <SearchAppBar
71
+ name="MyApp"
72
+ onSearch={onSearch}
73
+ mode="light"
74
+ setMode={() => {}}
75
+ />,
76
+ )
77
+ const input = screen.getByLabelText("search")
78
+
79
+ await act(async () => {
80
+ await userEvent.type(input, "ab{Enter}")
81
+ })
82
+ expect(onSearch).not.toHaveBeenCalled()
83
+
84
+ await act(async () => {
85
+ await userEvent.type(input, "c{Enter}")
86
+ })
87
+ expect(onSearch).toHaveBeenCalledWith("abc")
88
+ })
89
+
90
+ it("navigates to a search URL when no onSearch handler is given", async () => {
91
+ render(<SearchAppBar name="MyApp" mode="light" setMode={() => {}} />)
92
+ await act(async () => {
93
+ await userEvent.type(screen.getByLabelText("search"), "abc{Enter}")
94
+ })
95
+ expect(window.location).toBe("/?search=abc")
96
+ })
97
+
98
+ it("clears the search on Escape", async () => {
99
+ const onSearch = vi.fn()
100
+ render(
101
+ <SearchAppBar
102
+ name="MyApp"
103
+ onSearch={onSearch}
104
+ mode="light"
105
+ setMode={() => {}}
106
+ />,
107
+ )
108
+ const input = screen.getByLabelText("search")
109
+ await act(async () => {
110
+ await userEvent.type(input, "abc{Escape}")
111
+ })
112
+ expect(input.value).toBe("")
113
+ expect(onSearch).toHaveBeenCalledWith("")
114
+ })
115
+
116
+ it("clears the search via the clear button", async () => {
117
+ const onSearch = vi.fn()
118
+ render(
119
+ <SearchAppBar
120
+ name="MyApp"
121
+ onSearch={onSearch}
122
+ searchQuery="existing"
123
+ mode="light"
124
+ setMode={() => {}}
125
+ />,
126
+ )
127
+ await act(async () => {
128
+ await userEvent.click(screen.getByLabelText("clear search"))
129
+ })
130
+ expect(screen.getByLabelText("search").value).toBe("")
131
+ expect(onSearch).toHaveBeenCalledWith("")
132
+ })
133
+
134
+ it("toggles light/dark mode via the switch and persists it", async () => {
135
+ const setMode = vi.fn()
136
+ const {container} = render(
137
+ <SearchAppBar name="MyApp" mode="light" setMode={setMode} />,
138
+ )
139
+ // The switch lives in a Box hidden below the "md" breakpoint - jsdom
140
+ // doesn't evaluate that media query, so getByRole treats it as
141
+ // inaccessible. Reach the underlying input directly instead.
142
+ const checkbox = container.querySelector('input[type="checkbox"]')
143
+ await act(async () => {
144
+ await userEvent.click(checkbox)
145
+ })
146
+ expect(setMode).toHaveBeenCalledWith("dark")
147
+ expect(sessionStorage.getItem("mode")).toBe("dark")
148
+ })
149
+
150
+ it("renders menu items and fires their onClick when selected", async () => {
151
+ const onClick = vi.fn()
152
+ render(
153
+ <SearchAppBar
154
+ name="MyApp"
155
+ mode="light"
156
+ setMode={() => {}}
157
+ menuItems={[{label: "Log out", onClick}]}
158
+ />,
159
+ )
160
+ // The mobile menu (kept mounted but hidden) also contains an "Account"
161
+ // item with the same label - the desktop icon button is the first one.
162
+ const [accountButton] = screen.getAllByLabelText("account of current user")
163
+ await act(async () => {
164
+ await userEvent.click(accountButton)
165
+ })
166
+ const logOutItem = await screen.findByRole("menuitem", {name: "Log out"})
167
+ await act(async () => {
168
+ await userEvent.click(logOutItem)
169
+ })
170
+ expect(onClick).toHaveBeenCalled()
171
+ })
172
+ })
@@ -0,0 +1,157 @@
1
+ import {describe, it, expect, beforeEach, vi} from "vitest"
2
+ import {render, screen, waitFor, act} from "@testing-library/react"
3
+ import userEvent from "@testing-library/user-event"
4
+ import {createTestUser} from "../../__tests__/setup.js"
5
+ import Settings from "../Settings.jsx"
6
+
7
+ describe("Settings Component", () => {
8
+ beforeEach(() => {
9
+ sessionStorage.clear()
10
+ localStorage.clear()
11
+ delete window.location
12
+ window.location = {href: ""}
13
+ })
14
+
15
+ it("greets the user by the name stored in sessionStorage", async () => {
16
+ sessionStorage.setItem("name", "Alice")
17
+ const {user} = await createTestUser()
18
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
19
+ expect(screen.getByText("Hello Alice")).toBeTruthy()
20
+ })
21
+
22
+ it("falls back to localStorage for the name", async () => {
23
+ localStorage.setItem("name", "Bob")
24
+ const {user} = await createTestUser()
25
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
26
+ expect(screen.getByText("Hello Bob")).toBeTruthy()
27
+ })
28
+
29
+ it("shows account not found when no name is stored", async () => {
30
+ const {user} = await createTestUser()
31
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
32
+ expect(
33
+ screen.getByText("Account not found. Please try logging in again."),
34
+ ).toBeTruthy()
35
+ })
36
+
37
+ it("shows the app bar only when a user is logged in", async () => {
38
+ const {user} = await createTestUser()
39
+ const {rerender} = render(
40
+ <Settings user={{is: null}} mode="light" setMode={() => {}} />,
41
+ )
42
+ expect(screen.queryByLabelText("search")).toBeNull()
43
+
44
+ rerender(<Settings user={user} mode="light" setMode={() => {}} />)
45
+ expect(screen.getByLabelText("search")).toBeTruthy()
46
+ })
47
+
48
+ it("shows the build date when given", async () => {
49
+ const {user} = await createTestUser()
50
+ render(
51
+ <Settings
52
+ user={user}
53
+ mode="light"
54
+ setMode={() => {}}
55
+ buildDate="2026-01-01"
56
+ />,
57
+ )
58
+ expect(screen.getByText("Build date: 2026-01-01")).toBeTruthy()
59
+ })
60
+
61
+ it("requires the current password", async () => {
62
+ const {user} = await createTestUser()
63
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
64
+ await act(async () => {
65
+ await userEvent.type(screen.getByLabelText("New Password"), "newpass1")
66
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
67
+ })
68
+ expect(
69
+ screen.getByText("Please provide your current password"),
70
+ ).toBeTruthy()
71
+ })
72
+
73
+ it("requires a new password", async () => {
74
+ const {user} = await createTestUser()
75
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
76
+ await act(async () => {
77
+ await userEvent.type(
78
+ screen.getByLabelText("Current Password"),
79
+ "testpassword",
80
+ )
81
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
82
+ })
83
+ expect(screen.getByText("Please provide a new password")).toBeTruthy()
84
+ })
85
+
86
+ it("changes the password when the current one is correct", async () => {
87
+ const {user} = await createTestUser()
88
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
89
+
90
+ await act(async () => {
91
+ await userEvent.type(
92
+ screen.getByLabelText("Current Password"),
93
+ "testpassword",
94
+ )
95
+ await userEvent.type(screen.getByLabelText("New Password"), "newpass1")
96
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
97
+ })
98
+
99
+ await waitFor(() => {
100
+ expect(screen.getByText("Password updated")).toBeTruthy()
101
+ })
102
+
103
+ // Prove the password was actually changed, both ways.
104
+ await new Promise(resolve => {
105
+ user.auth("testuser", "newpass1", err => {
106
+ expect(err).toBeNull()
107
+ resolve()
108
+ })
109
+ })
110
+ await waitFor(async () => {
111
+ const err = await new Promise(resolve => {
112
+ user.auth("testuser", "testpassword", resolve)
113
+ })
114
+ expect(err).toBe("Wrong username or password")
115
+ })
116
+ })
117
+
118
+ it("shows an error and leaves the password unchanged when the current password is wrong", async () => {
119
+ const {user} = await createTestUser()
120
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
121
+
122
+ await act(async () => {
123
+ await userEvent.type(screen.getByLabelText("Current Password"), "wrong")
124
+ await userEvent.type(screen.getByLabelText("New Password"), "newpass1")
125
+ await userEvent.click(screen.getByRole("button", {name: "Submit"}))
126
+ })
127
+
128
+ await waitFor(() => {
129
+ expect(screen.getByText("Wrong username or password")).toBeTruthy()
130
+ })
131
+
132
+ await waitFor(async () => {
133
+ const err = await new Promise(resolve => {
134
+ user.auth("testuser", "testpassword", resolve)
135
+ })
136
+ expect(err).toBeNull()
137
+ })
138
+ })
139
+
140
+ it("logs out, clears stored session info, and redirects to /login", async () => {
141
+ const {user} = await createTestUser()
142
+ sessionStorage.setItem("name", "Alice")
143
+ sessionStorage.setItem("code", "abc")
144
+ localStorage.setItem("name", "Alice")
145
+ localStorage.setItem("code", "abc")
146
+
147
+ render(<Settings user={user} mode="light" setMode={() => {}} />)
148
+ await userEvent.click(screen.getByRole("button", {name: "Logout"}))
149
+
150
+ expect(user.is).toBeFalsy()
151
+ expect(sessionStorage.getItem("name")).toBeNull()
152
+ expect(sessionStorage.getItem("code")).toBeNull()
153
+ expect(localStorage.getItem("name")).toBeNull()
154
+ expect(localStorage.getItem("code")).toBeNull()
155
+ expect(window.location).toBe("/login")
156
+ })
157
+ })