Skip to content

Commit

Permalink
test(react-router): onEnter should have the router context defined (#…
Browse files Browse the repository at this point in the history
…2295)

* test(react-router): `onEnter` should have the router context defined

* test(react-router): remove the `waitFor` call
  • Loading branch information
SeanCassiere committed Sep 9, 2024
1 parent 282f83a commit 0e16429
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions packages/react-router/tests/route.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
/* eslint-disable */
import { describe, it, expect } from 'vitest'
import React from 'react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'

import {
getRouteApi,
createRoute,
RouterProvider,
createRootRoute,
createRoute,
createRouter,
getRouteApi,
useNavigate,
} from '../src'
import React from 'react'

afterEach(() => {
vi.resetAllMocks()
window.history.replaceState(null, 'root', '/')
cleanup()
})

describe('getRouteApi', () => {
it('should have the useMatch hook', () => {
Expand Down Expand Up @@ -139,3 +147,50 @@ describe('throws invariant exception when trying to access properties before `cr
})
})
*/

describe('onEnter event', () => {
it('should have router context defined in router.load()', async () => {
const fn = vi.fn()
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return <h1>Index</h1>
},
onEnter: ({ context }) => {
fn(context)
},
})
const routeTree = rootRoute.addChildren([indexRoute])
const router = createRouter({ routeTree, context: { foo: 'bar' } })

await router.load()

expect(fn).toHaveBeenCalledWith({ foo: 'bar' })
})

it('should have router context defined in <RouterProvider router={router} />', async () => {
const fn = vi.fn()
const rootRoute = createRootRoute()
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => {
return <h1>Index</h1>
},
onEnter: ({ context }) => {
fn(context)
},
})
const routeTree = rootRoute.addChildren([indexRoute])
const router = createRouter({ routeTree, context: { foo: 'bar' } })

render(<RouterProvider router={router} />)

const indexElem = await screen.findByText('Index')
expect(indexElem).toBeInTheDocument()

expect(fn).toHaveBeenCalledWith({ foo: 'bar' })
})
})

0 comments on commit 0e16429

Please sign in to comment.