-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg-provider.test.tsx
More file actions
158 lines (137 loc) · 6.05 KB
/
Copy pathorg-provider.test.tsx
File metadata and controls
158 lines (137 loc) · 6.05 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// @vitest-environment jsdom
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { render, screen, fireEvent, waitFor, cleanup } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
vi.mock("@/components/providers/auth-provider", () => ({ useAuth: () => ({ accessToken: "tok" }) }));
vi.mock("@/lib/api/endpoints", () => ({ getOrganizations: vi.fn() }));
import { getOrganizations } from "@/lib/api/endpoints";
import { OrgProvider, useActiveOrg } from "@/components/providers/org-provider";
import { OrgSwitcher } from "@/components/layout/org-switcher";
afterEach(() => {
vi.clearAllMocks();
localStorage.clear();
cleanup();
});
const ORG_A = { id: "org-a", name: "Org A", join_code: "aaa", role: null, created_at: null };
const ORG_B = { id: "org-b", name: "Org B", join_code: "bbb", role: null, created_at: null };
function renderWithClient(ui: React.ReactNode) {
const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
return render(<QueryClientProvider client={client}>{ui}</QueryClientProvider>);
}
function OrgIdDisplay() {
const { activeOrgId, isLoading } = useActiveOrg();
return (
<>
<span data-testid="active-org">{activeOrgId}</span>
<span data-testid="is-loading">{String(isLoading)}</span>
</>
);
}
function OrgIdSetter() {
const { activeOrgId, setActiveOrgId } = useActiveOrg();
return (
<>
<span data-testid="active-org">{activeOrgId}</span>
<button onClick={() => setActiveOrgId(ORG_B.id)}>Switch to B</button>
</>
);
}
describe("OrgProvider", () => {
it("defaults to the first org once loaded", async () => {
(getOrganizations as ReturnType<typeof vi.fn>).mockResolvedValue([ORG_A, ORG_B]);
renderWithClient(
<OrgProvider>
<OrgIdDisplay />
</OrgProvider>,
);
await waitFor(() => expect(screen.getByTestId("active-org").textContent).toBe(ORG_A.id));
});
it("setActiveOrgId updates state and persists to localStorage", async () => {
(getOrganizations as ReturnType<typeof vi.fn>).mockResolvedValue([ORG_A, ORG_B]);
renderWithClient(
<OrgProvider>
<OrgIdSetter />
</OrgProvider>,
);
await waitFor(() => expect(screen.getByTestId("active-org").textContent).toBe(ORG_A.id));
fireEvent.click(screen.getByRole("button", { name: /switch to b/i }));
await waitFor(() => expect(screen.getByTestId("active-org").textContent).toBe(ORG_B.id));
expect(localStorage.getItem("mnemo.activeOrgId")).toBe(ORG_B.id);
});
it("restores a valid persisted org from localStorage", async () => {
localStorage.setItem("mnemo.activeOrgId", ORG_B.id);
(getOrganizations as ReturnType<typeof vi.fn>).mockResolvedValue([ORG_A, ORG_B]);
renderWithClient(
<OrgProvider>
<OrgIdDisplay />
</OrgProvider>,
);
await waitFor(() => expect(screen.getByTestId("active-org").textContent).toBe(ORG_B.id));
});
// Optimistic: stored id is shown BEFORE the query resolves
it("O1 — muestra el id guardado en localStorage antes de que /v2/orgs resuelva", async () => {
localStorage.setItem("mnemo.activeOrgId", "org-guardada");
let resolveOrgs!: (value: typeof ORG_A[]) => void;
const slowPromise = new Promise<typeof ORG_A[]>((res) => { resolveOrgs = res; });
(getOrganizations as ReturnType<typeof vi.fn>).mockReturnValue(slowPromise);
renderWithClient(
<OrgProvider>
<OrgIdDisplay />
</OrgProvider>,
);
// Before the query resolves, the stored id must already be visible
await waitFor(() =>
expect(screen.getByTestId("active-org").textContent).toBe("org-guardada"),
);
// After resolving with a list that does NOT contain the stored id, correct to orgs[0]
resolveOrgs([ORG_A]); // ORG_A.id = "org-a", not "org-guardada"
await waitFor(() =>
expect(screen.getByTestId("active-org").textContent).toBe(ORG_A.id),
);
});
// I2: isLoading is exposed and transitions false→true→false as query resolves
it("I2 — isLoading es true mientras la query está pendiente y false cuando resuelve", async () => {
let resolveOrgs!: (value: typeof ORG_A[]) => void;
const slowPromise = new Promise<typeof ORG_A[]>((res) => { resolveOrgs = res; });
(getOrganizations as ReturnType<typeof vi.fn>).mockReturnValue(slowPromise);
renderWithClient(
<OrgProvider>
<OrgIdDisplay />
</OrgProvider>,
);
// Initially isLoading should be true while the query is in-flight
await waitFor(() => expect(screen.getByTestId("is-loading").textContent).toBe("true"));
// Resolve and isLoading should become false
resolveOrgs([ORG_A]);
await waitFor(() => expect(screen.getByTestId("is-loading").textContent).toBe("false"));
expect(screen.getByTestId("active-org").textContent).toBe(ORG_A.id);
});
});
describe("OrgSwitcher", () => {
it("renders a select with both orgs and switching updates the active org", async () => {
(getOrganizations as ReturnType<typeof vi.fn>).mockResolvedValue([ORG_A, ORG_B]);
renderWithClient(
<OrgProvider>
<OrgSwitcher />
<OrgIdDisplay />
</OrgProvider>,
);
const select = await screen.findByRole("combobox", { name: /organización/i });
expect(select).toBeInTheDocument();
expect(screen.getByRole("option", { name: "Org A" })).toBeInTheDocument();
expect(screen.getByRole("option", { name: "Org B" })).toBeInTheDocument();
await waitFor(() => expect(screen.getByTestId("active-org").textContent).toBe(ORG_A.id));
fireEvent.change(select, { target: { value: ORG_B.id } });
await waitFor(() => expect(screen.getByTestId("active-org").textContent).toBe(ORG_B.id));
});
it("renders single org name as plain text (no select)", async () => {
(getOrganizations as ReturnType<typeof vi.fn>).mockResolvedValue([ORG_A]);
renderWithClient(
<OrgProvider>
<OrgSwitcher />
</OrgProvider>,
);
await waitFor(() => expect(screen.getByText("Org A")).toBeInTheDocument());
expect(screen.queryByRole("combobox")).not.toBeInTheDocument();
});
});