|
1 |
| -import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest' |
2 | 1 | import { cleanup, render, screen } from '@solidjs/testing-library'
|
| 2 | +import { afterEach, beforeEach, describe, expect, it, test, vi } from 'vitest' |
3 | 3 |
|
4 | 4 | import {
|
5 | 5 | RouterProvider,
|
@@ -204,6 +204,51 @@ describe('onEnter event', () => {
|
204 | 204 | })
|
205 | 205 | })
|
206 | 206 |
|
| 207 | +describe('useLoaderDeps', () => { |
| 208 | + test('returns an Accessor', async () => { |
| 209 | + const rootRoute = createRootRoute() |
| 210 | + const indexRoute = createRoute({ |
| 211 | + getParentRoute: () => rootRoute, |
| 212 | + path: '/', |
| 213 | + loaderDeps: ({ search }) => ({ testDep: 'value' }), |
| 214 | + component: () => { |
| 215 | + const deps = indexRoute.useLoaderDeps() |
| 216 | + // deps should be an Accessor, so we need to call it to get the value |
| 217 | + expect(typeof deps).toBe('function') |
| 218 | + expect(deps()).toEqual({ testDep: 'value' }) |
| 219 | + return <div>Index</div> |
| 220 | + }, |
| 221 | + }) |
| 222 | + const routeTree = rootRoute.addChildren([indexRoute]) |
| 223 | + const router = createRouter({ routeTree, history }) |
| 224 | + render(() => <RouterProvider router={router} />) |
| 225 | + const indexElem = await screen.findByText('Index') |
| 226 | + expect(indexElem).toBeInTheDocument() |
| 227 | + }) |
| 228 | + |
| 229 | + test('returns an Accessor via Route API', async () => { |
| 230 | + const rootRoute = createRootRoute() |
| 231 | + const indexRoute = createRoute({ |
| 232 | + getParentRoute: () => rootRoute, |
| 233 | + path: '/', |
| 234 | + loaderDeps: ({ search }) => ({ testDep: 'api-value' }), |
| 235 | + component: () => { |
| 236 | + const api = getRouteApi('/') |
| 237 | + const deps = api.useLoaderDeps() |
| 238 | + // deps should be an Accessor, so we need to call it to get the value |
| 239 | + expect(typeof deps).toBe('function') |
| 240 | + expect(deps()).toEqual({ testDep: 'api-value' }) |
| 241 | + return <div>Index with API</div> |
| 242 | + }, |
| 243 | + }) |
| 244 | + const routeTree = rootRoute.addChildren([indexRoute]) |
| 245 | + const router = createRouter({ routeTree, history }) |
| 246 | + render(() => <RouterProvider router={router} />) |
| 247 | + const indexElem = await screen.findByText('Index with API') |
| 248 | + expect(indexElem).toBeInTheDocument() |
| 249 | + }) |
| 250 | +}) |
| 251 | + |
207 | 252 | describe('route.head', () => {
|
208 | 253 | test('meta', async () => {
|
209 | 254 | const rootRoute = createRootRoute({
|
|
0 commit comments