@mblaney/holster-browser 2.0.0 → 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.
- package/README.md +8 -8
- package/package.json +3 -1
- package/src/__tests__/setup.js +76 -0
- package/src/components/Register.jsx +6 -3
- package/src/components/SearchAppBar.jsx +30 -2
- package/src/components/Signup.jsx +6 -3
- package/src/components/UpdatePassword.jsx +49 -15
- package/src/components/__tests__/Login.test.jsx +214 -0
- package/src/components/__tests__/LoginCodes.test.jsx +120 -0
- package/src/components/__tests__/Register.test.jsx +223 -0
- package/src/components/__tests__/RequestCode.test.jsx +62 -0
- package/src/components/__tests__/ResetPassword.test.jsx +56 -0
- package/src/components/__tests__/SearchAppBar.test.jsx +172 -0
- package/src/components/__tests__/Settings.test.jsx +157 -0
- package/src/components/__tests__/Signup.test.jsx +110 -63
- package/src/components/__tests__/UpdatePassword.test.jsx +200 -0
- package/src/components/__tests__/ValidateEmail.test.jsx +63 -0
- package/src/hooks/__tests__/useAccountSync.test.js +67 -0
- package/src/hooks/useAccountSync.js +2 -2
- package/src/setupTests.js +8 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {describe, it, expect, 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 LoginCodes from "../LoginCodes.jsx"
|
|
6
|
+
|
|
7
|
+
// The host and the logged-in user are the same real account here - the
|
|
8
|
+
// component addresses "the host's own epub/shared data" via [host, key],
|
|
9
|
+
// which resolves correctly whether or not host is your own pub. Using one
|
|
10
|
+
// account for both avoids needing a second, separately-keyed identity just
|
|
11
|
+
// to exercise the ECDH secret computation.
|
|
12
|
+
async function shareCode(user, code, key, value) {
|
|
13
|
+
const secret = await user.SEA.secret({epub: user.is.epub}, user.is)
|
|
14
|
+
const enc = value === null ? null : await user.SEA.encrypt(value, secret)
|
|
15
|
+
await new Promise(resolve => {
|
|
16
|
+
user
|
|
17
|
+
.get([user.is.pub, "shared"])
|
|
18
|
+
.next("login_codes")
|
|
19
|
+
.next(code)
|
|
20
|
+
.put({[key]: enc}, resolve)
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("LoginCodes Component", () => {
|
|
25
|
+
it("renders nothing when there are no shared codes", async () => {
|
|
26
|
+
const {user} = await createTestUser()
|
|
27
|
+
const {container} = render(
|
|
28
|
+
<LoginCodes user={user} host={user.is.pub} code="my-code" />,
|
|
29
|
+
)
|
|
30
|
+
expect(container.firstChild).toBeNull()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it("renders nothing when host or code is missing", async () => {
|
|
34
|
+
const {user} = await createTestUser()
|
|
35
|
+
const {container} = render(<LoginCodes user={user} host={user.is.pub} />)
|
|
36
|
+
expect(container.firstChild).toBeNull()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it("shows a pre-existing shared login code, decrypted", async () => {
|
|
40
|
+
const {user} = await createTestUser()
|
|
41
|
+
await shareCode(user, "my-code", "codeA", "ABC123")
|
|
42
|
+
|
|
43
|
+
const {container} = render(
|
|
44
|
+
<LoginCodes user={user} host={user.is.pub} code="my-code" />,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
await waitFor(() => {
|
|
48
|
+
expect(screen.getByDisplayValue("ABC123")).toBeTruthy()
|
|
49
|
+
})
|
|
50
|
+
expect(container.textContent).toContain("You have")
|
|
51
|
+
expect(container.textContent).toContain("1")
|
|
52
|
+
expect(container.textContent).not.toContain("login codes")
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("shows the plural count for more than one code", async () => {
|
|
56
|
+
const {user} = await createTestUser()
|
|
57
|
+
await shareCode(user, "my-code", "codeA", "ABC123")
|
|
58
|
+
await shareCode(user, "my-code", "codeB", "DEF456")
|
|
59
|
+
|
|
60
|
+
const {container} = render(
|
|
61
|
+
<LoginCodes user={user} host={user.is.pub} code="my-code" />,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
await waitFor(() => {
|
|
65
|
+
expect(screen.getByDisplayValue("ABC123")).toBeTruthy()
|
|
66
|
+
expect(screen.getByDisplayValue("DEF456")).toBeTruthy()
|
|
67
|
+
})
|
|
68
|
+
expect(container.textContent).toContain("login codes you can share")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("live-updates when a new code is shared after mount", async () => {
|
|
72
|
+
const {user} = await createTestUser()
|
|
73
|
+
await shareCode(user, "my-code", "codeA", "ABC123")
|
|
74
|
+
|
|
75
|
+
render(<LoginCodes user={user} host={user.is.pub} code="my-code" />)
|
|
76
|
+
await waitFor(() => {
|
|
77
|
+
expect(screen.getByDisplayValue("ABC123")).toBeTruthy()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
await shareCode(user, "my-code", "codeB", "DEF456")
|
|
81
|
+
|
|
82
|
+
await waitFor(() => {
|
|
83
|
+
expect(screen.getByDisplayValue("DEF456")).toBeTruthy()
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it("removes a code from the list once it's revoked", async () => {
|
|
88
|
+
const {user} = await createTestUser()
|
|
89
|
+
await shareCode(user, "my-code", "codeA", "ABC123")
|
|
90
|
+
|
|
91
|
+
render(<LoginCodes user={user} host={user.is.pub} code="my-code" />)
|
|
92
|
+
await waitFor(() => {
|
|
93
|
+
expect(screen.getByDisplayValue("ABC123")).toBeTruthy()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
await shareCode(user, "my-code", "codeA", null)
|
|
97
|
+
|
|
98
|
+
await waitFor(() => {
|
|
99
|
+
expect(screen.queryByDisplayValue("ABC123")).toBeNull()
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it("selects the code's text when its copy button is clicked", async () => {
|
|
104
|
+
const {user} = await createTestUser()
|
|
105
|
+
await shareCode(user, "my-code", "codeA", "ABC123")
|
|
106
|
+
const selectSpy = vi.spyOn(HTMLInputElement.prototype, "select")
|
|
107
|
+
|
|
108
|
+
render(<LoginCodes user={user} host={user.is.pub} code="my-code" />)
|
|
109
|
+
await waitFor(() => {
|
|
110
|
+
expect(screen.getByDisplayValue("ABC123")).toBeTruthy()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
await act(async () => {
|
|
114
|
+
await userEvent.click(screen.getByLabelText("copy login code"))
|
|
115
|
+
})
|
|
116
|
+
expect(selectSpy).toHaveBeenCalled()
|
|
117
|
+
|
|
118
|
+
selectSpy.mockRestore()
|
|
119
|
+
})
|
|
120
|
+
})
|
|
@@ -0,0 +1,223 @@
|
|
|
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 {createTestHolster} from "../../__tests__/setup.js"
|
|
5
|
+
import Register from "../Register.jsx"
|
|
6
|
+
|
|
7
|
+
function mockFetchResponse(ok, text) {
|
|
8
|
+
return Promise.resolve({ok, text: () => Promise.resolve(text)})
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function fillAndSubmit({username, password, email, code}) {
|
|
12
|
+
if (username !== undefined) {
|
|
13
|
+
await userEvent.type(screen.getByLabelText("Username"), username)
|
|
14
|
+
}
|
|
15
|
+
if (password !== undefined) {
|
|
16
|
+
await userEvent.type(screen.getByLabelText("Password"), password)
|
|
17
|
+
}
|
|
18
|
+
if (email !== undefined) {
|
|
19
|
+
await userEvent.type(screen.getByLabelText("Email"), email)
|
|
20
|
+
}
|
|
21
|
+
if (code !== undefined) {
|
|
22
|
+
await userEvent.type(screen.getByLabelText("Login code"), code)
|
|
23
|
+
}
|
|
24
|
+
await userEvent.click(screen.getByRole("button", {name: "Submit"}))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("Register Component", () => {
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
vi.stubGlobal("fetch", vi.fn())
|
|
30
|
+
delete window.location
|
|
31
|
+
window.location = {href: ""}
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("renders the register form", () => {
|
|
35
|
+
const {holster} = createTestHolster()
|
|
36
|
+
render(<Register user={holster.user()} />)
|
|
37
|
+
expect(screen.getByText("Register")).toBeTruthy()
|
|
38
|
+
expect(screen.getByLabelText("Username")).toBeTruthy()
|
|
39
|
+
expect(screen.getByLabelText("Email")).toBeTruthy()
|
|
40
|
+
expect(screen.getByLabelText("Login code")).toBeTruthy()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("shows already logged in and disables submit when a user is present", async () => {
|
|
44
|
+
const {holster} = createTestHolster()
|
|
45
|
+
const user = holster.user()
|
|
46
|
+
await new Promise(resolve => {
|
|
47
|
+
user.create("alice", "hunter2", () =>
|
|
48
|
+
user.auth("alice", "hunter2", resolve),
|
|
49
|
+
)
|
|
50
|
+
})
|
|
51
|
+
render(<Register user={user} />)
|
|
52
|
+
expect(screen.getByText("Already logged in")).toBeTruthy()
|
|
53
|
+
expect(screen.getByRole("button", {name: "Submit"})).toBeDisabled()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it("requires a username", async () => {
|
|
57
|
+
const {holster} = createTestHolster()
|
|
58
|
+
const user = holster.user()
|
|
59
|
+
render(<Register user={user} />)
|
|
60
|
+
await act(async () => {
|
|
61
|
+
await fillAndSubmit({email: "alice@test.com"})
|
|
62
|
+
})
|
|
63
|
+
expect(screen.getByText("Please choose a username")).toBeTruthy()
|
|
64
|
+
expect(user.is).toBeFalsy()
|
|
65
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it("rejects a username with invalid characters", async () => {
|
|
69
|
+
const {holster} = createTestHolster()
|
|
70
|
+
const user = holster.user()
|
|
71
|
+
render(<Register user={user} />)
|
|
72
|
+
await act(async () => {
|
|
73
|
+
await fillAndSubmit({username: "alice!", email: "alice@test.com"})
|
|
74
|
+
})
|
|
75
|
+
expect(
|
|
76
|
+
screen.getByText(
|
|
77
|
+
"Username must contain only numbers, letters and underscore",
|
|
78
|
+
),
|
|
79
|
+
).toBeTruthy()
|
|
80
|
+
expect(user.is).toBeFalsy()
|
|
81
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it("requires an email", async () => {
|
|
85
|
+
const {holster} = createTestHolster()
|
|
86
|
+
const user = holster.user()
|
|
87
|
+
render(<Register user={user} />)
|
|
88
|
+
await act(async () => {
|
|
89
|
+
await fillAndSubmit({username: "alice"})
|
|
90
|
+
})
|
|
91
|
+
expect(screen.getByText("Please provide your email")).toBeTruthy()
|
|
92
|
+
expect(user.is).toBeFalsy()
|
|
93
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it("checks the login code first, and stops without creating an account when it's rejected", async () => {
|
|
97
|
+
const {holster} = createTestHolster()
|
|
98
|
+
const user = holster.user()
|
|
99
|
+
fetch.mockReturnValue(mockFetchResponse(false, "Invalid login code"))
|
|
100
|
+
render(<Register user={user} />)
|
|
101
|
+
|
|
102
|
+
await act(async () => {
|
|
103
|
+
await fillAndSubmit({
|
|
104
|
+
username: "alice",
|
|
105
|
+
password: "hunter2",
|
|
106
|
+
email: "alice@test.com",
|
|
107
|
+
code: "bad-code",
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
112
|
+
expect.stringContaining("/check-login-code"),
|
|
113
|
+
expect.objectContaining({
|
|
114
|
+
method: "POST",
|
|
115
|
+
body: JSON.stringify({code: "bad-code"}),
|
|
116
|
+
}),
|
|
117
|
+
)
|
|
118
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
119
|
+
expect(screen.getByText("Invalid login code")).toBeTruthy()
|
|
120
|
+
expect(user.is).toBeFalsy()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it("creates the account, verifies the login code, and redirects on success", async () => {
|
|
124
|
+
const {holster} = createTestHolster()
|
|
125
|
+
const user = holster.user()
|
|
126
|
+
fetch
|
|
127
|
+
.mockReturnValueOnce(mockFetchResponse(true, ""))
|
|
128
|
+
.mockReturnValueOnce(mockFetchResponse(true, ""))
|
|
129
|
+
render(<Register user={user} />)
|
|
130
|
+
|
|
131
|
+
await act(async () => {
|
|
132
|
+
await fillAndSubmit({
|
|
133
|
+
username: "alice",
|
|
134
|
+
password: "hunter2",
|
|
135
|
+
email: "alice@test.com",
|
|
136
|
+
code: "good-code",
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
await waitFor(() => expect(user.is).toBeTruthy())
|
|
141
|
+
const {pub, epub} = user.is
|
|
142
|
+
|
|
143
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
144
|
+
expect(fetch).toHaveBeenNthCalledWith(
|
|
145
|
+
1,
|
|
146
|
+
expect.stringContaining("/check-login-code"),
|
|
147
|
+
expect.objectContaining({body: JSON.stringify({code: "good-code"})}),
|
|
148
|
+
)
|
|
149
|
+
const secondCallBody = JSON.parse(fetch.mock.calls[1][1].body)
|
|
150
|
+
expect(secondCallBody).toEqual({
|
|
151
|
+
code: "good-code",
|
|
152
|
+
pub,
|
|
153
|
+
epub,
|
|
154
|
+
signed: expect.anything(),
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const verified = await holster.SEA.verify(secondCallBody.signed, {pub})
|
|
158
|
+
expect(verified).toEqual({
|
|
159
|
+
username: "alice",
|
|
160
|
+
email: "alice@test.com",
|
|
161
|
+
timestamp: expect.any(Number),
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
expect(screen.getByText("Account created")).toBeTruthy()
|
|
165
|
+
expect(window.location).toBe("/login")
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it("deletes the local account and shows the error when verification fails", async () => {
|
|
169
|
+
const {holster} = createTestHolster()
|
|
170
|
+
const user = holster.user()
|
|
171
|
+
fetch
|
|
172
|
+
.mockReturnValueOnce(mockFetchResponse(true, ""))
|
|
173
|
+
.mockReturnValueOnce(mockFetchResponse(false, "Login code already used"))
|
|
174
|
+
render(<Register user={user} />)
|
|
175
|
+
|
|
176
|
+
await act(async () => {
|
|
177
|
+
await fillAndSubmit({
|
|
178
|
+
username: "alice",
|
|
179
|
+
password: "hunter2",
|
|
180
|
+
email: "alice@test.com",
|
|
181
|
+
code: "used-code",
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
await waitFor(() => {
|
|
186
|
+
expect(screen.getByText("Login code already used")).toBeTruthy()
|
|
187
|
+
})
|
|
188
|
+
expect(window.location).toEqual({href: ""})
|
|
189
|
+
|
|
190
|
+
// Prove the account was actually deleted locally, not just left behind.
|
|
191
|
+
await waitFor(async () => {
|
|
192
|
+
const err = await new Promise(resolve => {
|
|
193
|
+
user.auth("alice", "hunter2", resolve)
|
|
194
|
+
})
|
|
195
|
+
expect(err).toBe("Wrong username or password")
|
|
196
|
+
})
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
it("shows the error and stops when user.create fails", async () => {
|
|
200
|
+
const {holster} = createTestHolster()
|
|
201
|
+
const user = holster.user()
|
|
202
|
+
await new Promise(resolve => {
|
|
203
|
+
user.create("alice", "existing-password", resolve)
|
|
204
|
+
})
|
|
205
|
+
fetch.mockReturnValue(mockFetchResponse(true, ""))
|
|
206
|
+
render(<Register user={user} />)
|
|
207
|
+
|
|
208
|
+
await act(async () => {
|
|
209
|
+
await fillAndSubmit({
|
|
210
|
+
username: "alice",
|
|
211
|
+
password: "hunter2",
|
|
212
|
+
email: "alice@test.com",
|
|
213
|
+
code: "good-code",
|
|
214
|
+
})
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
await waitFor(() => {
|
|
218
|
+
expect(screen.getByText("Username already exists")).toBeTruthy()
|
|
219
|
+
})
|
|
220
|
+
expect(user.is).toBeFalsy()
|
|
221
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
222
|
+
})
|
|
223
|
+
})
|
|
@@ -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
|
+
})
|