|
1 | | -import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
2 | 2 | import { render, screen, act } from "@testing-library/react"; |
3 | 3 | import { Router } from "../Router.js"; |
4 | 4 | import { Outlet } from "../Outlet.js"; |
@@ -136,4 +136,110 @@ describe("Router", () => { |
136 | 136 |
|
137 | 137 | expect(screen.getByText("About")).toBeInTheDocument(); |
138 | 138 | }); |
| 139 | + |
| 140 | + describe("onNavigate callback", () => { |
| 141 | + it("calls onNavigate with NavigateEvent and matched routes before navigation", () => { |
| 142 | + const onNavigate = vi.fn(); |
| 143 | + |
| 144 | + const routes: RouteDefinition[] = [ |
| 145 | + { path: "/", component: () => <div>Home</div> }, |
| 146 | + { path: "/about", component: () => <div>About</div> }, |
| 147 | + ]; |
| 148 | + |
| 149 | + render(<Router routes={routes} onNavigate={onNavigate} />); |
| 150 | + |
| 151 | + act(() => { |
| 152 | + const { proceed } = mockNavigation.__simulateNavigationWithEvent( |
| 153 | + "http://localhost/about", |
| 154 | + ); |
| 155 | + proceed(); |
| 156 | + }); |
| 157 | + |
| 158 | + expect(onNavigate).toHaveBeenCalledTimes(1); |
| 159 | + expect(onNavigate).toHaveBeenCalledWith( |
| 160 | + expect.objectContaining({ |
| 161 | + destination: expect.objectContaining({ |
| 162 | + url: "http://localhost/about", |
| 163 | + }), |
| 164 | + }), |
| 165 | + expect.arrayContaining([ |
| 166 | + expect.objectContaining({ |
| 167 | + pathname: "/about", |
| 168 | + }), |
| 169 | + ]), |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it("prevents navigation when onNavigate calls event.preventDefault()", () => { |
| 174 | + const onNavigate = vi.fn((event: NavigateEvent) => { |
| 175 | + event.preventDefault(); |
| 176 | + }); |
| 177 | + |
| 178 | + const routes: RouteDefinition[] = [ |
| 179 | + { path: "/", component: () => <div>Home</div> }, |
| 180 | + { path: "/about", component: () => <div>About</div> }, |
| 181 | + ]; |
| 182 | + |
| 183 | + render(<Router routes={routes} onNavigate={onNavigate} />); |
| 184 | + |
| 185 | + act(() => { |
| 186 | + const { proceed } = mockNavigation.__simulateNavigationWithEvent( |
| 187 | + "http://localhost/about", |
| 188 | + ); |
| 189 | + proceed(); |
| 190 | + }); |
| 191 | + |
| 192 | + // Should still show Home because navigation was prevented |
| 193 | + expect(screen.getByText("Home")).toBeInTheDocument(); |
| 194 | + expect(screen.queryByText("About")).not.toBeInTheDocument(); |
| 195 | + }); |
| 196 | + |
| 197 | + it("allows navigation when onNavigate does not call preventDefault()", () => { |
| 198 | + const onNavigate = vi.fn(); // Does not call preventDefault |
| 199 | + |
| 200 | + const routes: RouteDefinition[] = [ |
| 201 | + { path: "/", component: () => <div>Home</div> }, |
| 202 | + { path: "/about", component: () => <div>About</div> }, |
| 203 | + ]; |
| 204 | + |
| 205 | + render(<Router routes={routes} onNavigate={onNavigate} />); |
| 206 | + |
| 207 | + act(() => { |
| 208 | + const { proceed } = mockNavigation.__simulateNavigationWithEvent( |
| 209 | + "http://localhost/about", |
| 210 | + ); |
| 211 | + proceed(); |
| 212 | + }); |
| 213 | + |
| 214 | + expect(screen.getByText("About")).toBeInTheDocument(); |
| 215 | + }); |
| 216 | + |
| 217 | + it("calls onNavigate with null for unmatched routes", () => { |
| 218 | + const onNavigate = vi.fn(); |
| 219 | + |
| 220 | + const routes: RouteDefinition[] = [ |
| 221 | + { path: "/", component: () => <div>Home</div> }, |
| 222 | + ]; |
| 223 | + |
| 224 | + render(<Router routes={routes} onNavigate={onNavigate} />); |
| 225 | + |
| 226 | + act(() => { |
| 227 | + const { proceed } = mockNavigation.__simulateNavigationWithEvent( |
| 228 | + "http://localhost/unknown", |
| 229 | + ); |
| 230 | + proceed(); |
| 231 | + }); |
| 232 | + |
| 233 | + // onNavigate should be called with null for unmatched routes |
| 234 | + expect(onNavigate).toHaveBeenCalledTimes(1); |
| 235 | + expect(onNavigate).toHaveBeenCalledWith( |
| 236 | + expect.objectContaining({ |
| 237 | + destination: expect.objectContaining({ |
| 238 | + url: "http://localhost/unknown", |
| 239 | + }), |
| 240 | + }), |
| 241 | + null, |
| 242 | + ); |
| 243 | + }); |
| 244 | + }); |
139 | 245 | }); |
0 commit comments