-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.test.tsx
More file actions
67 lines (60 loc) · 2 KB
/
Copy pathApp.test.tsx
File metadata and controls
67 lines (60 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
const mockUseAuth = vi.fn();
const mockSigninSilent = vi.fn();
vi.mock("react-oidc-context", () => ({
AuthProvider: ({ children }: { children: React.ReactNode }) => (
<>{children}</>
),
useAuth: () => mockUseAuth(),
}));
import App from "./App";
describe("App (Token Refresh)", () => {
afterEach(() => cleanup());
it("renders sign in button when not authenticated", () => {
mockUseAuth.mockReturnValue({
isAuthenticated: false,
isLoading: false,
});
render(<App />);
expect(screen.getByText("Sign in")).toBeInTheDocument();
});
it("shows token info and refresh button when authenticated", () => {
mockUseAuth.mockReturnValue({
isAuthenticated: true,
isLoading: false,
signinSilent: mockSigninSilent,
user: {
profile: { given_name: "Jane" },
expires_at: Math.floor(Date.now() / 1000) + 3600,
},
});
render(<App />);
expect(screen.getByText("Welcome, Jane")).toBeInTheDocument();
expect(screen.getByText("Refresh token now")).toBeInTheDocument();
});
it("renders error message when authentication fails", () => {
mockUseAuth.mockReturnValue({
isAuthenticated: false,
isLoading: false,
error: new Error("Unable to refresh token"),
});
render(<App />);
expect(screen.getByText(/Unable to refresh token/)).toBeInTheDocument();
expect(screen.getByText("Try again")).toBeInTheDocument();
});
it("calls signinSilent when refresh button is clicked", () => {
mockUseAuth.mockReturnValue({
isAuthenticated: true,
isLoading: false,
signinSilent: mockSigninSilent,
user: {
profile: { given_name: "Jane" },
expires_at: Math.floor(Date.now() / 1000) + 3600,
},
});
render(<App />);
fireEvent.click(screen.getByText("Refresh token now"));
expect(mockSigninSilent).toHaveBeenCalled();
});
});