Skip to content

Commit 5f5efc2

Browse files
make useUser return null user data on error (restored original behaviour)
1 parent 7c08a9a commit 5f5efc2

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/client/hooks/use-user.test.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
*/
44

55
import React from "react";
6-
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from "vitest";
76
import { act, renderHook, waitFor } from "@testing-library/react";
87
import * as swrModule from "swr";
8+
import {
9+
afterEach,
10+
beforeEach,
11+
describe,
12+
expect,
13+
it,
14+
vi,
15+
type MockInstance
16+
} from "vitest";
917

1018
import type { User } from "../../types/index.js";
1119
import { useUser } from "./use-user.js";
@@ -137,7 +145,10 @@ describe.only("useUser Integration with SWR Cache", () => {
137145

138146
// Explicitly type fetchSpy using MockInstance and the global fetch signature
139147
let fetchSpy: MockInstance<
140-
(input: RequestInfo | URL, init?: RequestInit | undefined) => Promise<Response>
148+
(
149+
input: RequestInfo | URL,
150+
init?: RequestInit | undefined
151+
) => Promise<Response>
141152
>;
142153

143154
beforeEach(() => {
@@ -159,7 +170,9 @@ describe.only("useUser Integration with SWR Cache", () => {
159170
);
160171

161172
const wrapper = ({ children }: { children: React.ReactNode }) => (
162-
<swrModule.SWRConfig value={{ provider: () => new Map() }}>{children}</swrModule.SWRConfig>
173+
<swrModule.SWRConfig value={{ provider: () => new Map() }}>
174+
{children}
175+
</swrModule.SWRConfig>
163176
);
164177

165178
const { result } = renderHook(() => useUser(), { wrapper });
@@ -233,12 +246,12 @@ describe.only("useUser Integration with SWR Cache", () => {
233246
result.current.invalidate();
234247
});
235248

236-
// Wait for the hook to reflect the error state, user should still be the initial one before error
249+
// Wait for the hook to reflect the error state, user should still be the initial one before error
237250
await waitFor(() => expect(result.current.error).not.toBeNull());
238251

239252
// Assert error state - SWR catches the rejection from fetch itself.
240253
// Check for the message of the error we explicitly rejected with.
241-
expect(result.current.user).toEqual(initialUser); // SWR might keep stale data upon rejection
254+
expect(result.current.user).toBeNull(); // Expect null now, not stale data
242255
expect(result.current.error?.message).toBe(fetchError.message); // Correct assertion
243256
expect(result.current.isLoading).toBe(false);
244257

src/client/hooks/use-user.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export function useUser() {
1010
(...args) =>
1111
fetch(...args).then((res) => {
1212
if (!res.ok) {
13-
throw new Error(res.statusText || "Fetch error");
13+
throw new Error("Unauthorized");
1414
}
1515
return res.json();
1616
})
1717
);
1818

1919
if (error) {
2020
return {
21-
user: data,
21+
user: null,
2222
isLoading: false,
2323
error,
2424
invalidate: () => mutate()
@@ -35,9 +35,9 @@ export function useUser() {
3535
}
3636

3737
return {
38-
user: null,
38+
user: data,
3939
isLoading,
40-
error: null,
40+
error,
4141
invalidate: () => mutate()
4242
};
4343
}

0 commit comments

Comments
 (0)