-
Notifications
You must be signed in to change notification settings - Fork 454
Allow SWR mutation in useUser hook #2045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7e648e2
add mutate to user SWR
tusharpandey13 44b91d9
lint
tusharpandey13 197ab5e
Merge branch 'main' into feature/mutateUserSWR
tusharpandey13 6b74969
added exhaustive tests for mutation suport in useUser hook
tusharpandey13 897b00e
Merge branch 'feature/mutateUserSWR' of https://github.com/auth0/next…
tusharpandey13 c55f0aa
added @testing-library/react
tusharpandey13 7c08a9a
added jsdom
tusharpandey13 5f5efc2
make useUser return null user data on error (restored original behavi…
tusharpandey13 f97aeb2
split use-user hook test into unit and integration
tusharpandey13 e6967b5
linting fixes
tusharpandey13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Import the mocked SWR module | ||
| import swr from "swr"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import type { User } from "../../types/index.js"; | ||
| import { useUser } from "./use-user.js"; | ||
|
|
||
| // Create a mock for SWR that we can control in our tests | ||
| vi.mock("swr", () => { | ||
| const mockMutate = vi.fn(); | ||
| return { | ||
| __esModule: true, | ||
| default: vi.fn(() => ({ | ||
| data: undefined, | ||
| error: undefined, | ||
| isLoading: true, | ||
| isValidating: false, | ||
| mutate: mockMutate | ||
| })) | ||
| }; | ||
| }); | ||
|
|
||
| describe("useUser", () => { | ||
| const mockUser: User = { | ||
| sub: "user_123", | ||
| name: "Test User", | ||
| email: "test@example.com" | ||
| }; | ||
|
|
||
| // Get a reference to the mocked SWR default function | ||
| const mockSWR = vi.mocked(swr); | ||
|
|
||
| // Get a reference to the mockMutate function inside the mock | ||
| const mockMutate = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| // Reset the default mock implementation | ||
| mockSWR.mockImplementation(() => ({ | ||
| data: undefined, | ||
| error: undefined, | ||
| isLoading: true, | ||
| isValidating: false, | ||
| mutate: mockMutate | ||
| })); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("should return isLoading when no data or error", () => { | ||
| // The default mock implementation has isLoading=true | ||
| const result = useUser(); | ||
|
|
||
| expect(result.isLoading).toBe(true); | ||
| expect(result.user).toBe(undefined); | ||
| expect(result.error).toBe(undefined); | ||
| expect(typeof result.invalidate).toBe("function"); | ||
| }); | ||
|
|
||
| it("should return user data when data is available", () => { | ||
| // Mock SWR to return user data | ||
| mockSWR.mockImplementationOnce(() => ({ | ||
| data: mockUser, | ||
| error: undefined, | ||
| isLoading: false, | ||
| isValidating: false, | ||
| mutate: mockMutate | ||
| })); | ||
|
|
||
| const result = useUser(); | ||
|
|
||
| expect(result.isLoading).toBe(false); | ||
| expect(result.user).toBe(mockUser); | ||
| expect(result.error).toBe(null); | ||
| expect(typeof result.invalidate).toBe("function"); | ||
| }); | ||
|
|
||
| it("should return error when fetch fails", () => { | ||
| const mockError = new Error("Unauthorized"); | ||
|
|
||
| // Mock SWR to return error | ||
| mockSWR.mockImplementationOnce(() => ({ | ||
| data: undefined, | ||
| error: mockError, | ||
| isLoading: false, | ||
| isValidating: false, | ||
| mutate: mockMutate | ||
| })); | ||
|
|
||
| const result = useUser(); | ||
|
|
||
| expect(result.isLoading).toBe(false); | ||
| expect(result.user).toBe(null); | ||
| expect(result.error).toBe(mockError); | ||
| expect(typeof result.invalidate).toBe("function"); | ||
| }); | ||
|
|
||
| it("should call mutate when invalidate is called", () => { | ||
| // Mock SWR with mockMutate for invalidate testing | ||
| mockSWR.mockImplementationOnce(() => ({ | ||
| data: mockUser, | ||
| error: undefined, | ||
| isLoading: false, | ||
| isValidating: false, | ||
| mutate: mockMutate | ||
| })); | ||
|
|
||
| const result = useUser(); | ||
|
|
||
| // Call invalidate function | ||
| result.invalidate(); | ||
|
|
||
| // Verify mutate was called | ||
| expect(mockMutate).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.