Skip to content

Commit 89c7b5d

Browse files
authored
test(react-router,solid-router): test re-render for preload='viewport' (#4672)
1 parent 3854370 commit 89c7b5d

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

packages/react-router/tests/link.test.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,15 +4157,21 @@ describe('Link', () => {
41574157

41584158
test('Router.preload="viewport", should trigger the IntersectionObserver\'s observe and disconnect methods', async () => {
41594159
const rootRoute = createRootRoute()
4160-
const indexRoute = createRoute({
4161-
getParentRoute: () => rootRoute,
4162-
path: '/',
4163-
component: () => (
4160+
const RouteComponent = () => {
4161+
const [count, setCount] = React.useState(0)
4162+
return (
41644163
<>
41654164
<h1>Index Heading</h1>
4165+
<output>{count}</output>
4166+
<button onClick={() => setCount((c) => c + 1)}>Render</button>
41664167
<Link to="/">Index Link</Link>
41674168
</>
4168-
),
4169+
)
4170+
}
4171+
const indexRoute = createRoute({
4172+
getParentRoute: () => rootRoute,
4173+
path: '/',
4174+
component: RouteComponent,
41694175
})
41704176

41714177
const router = createRouter({
@@ -4184,6 +4190,17 @@ describe('Link', () => {
41844190

41854191
expect(ioDisconnectMock).toBeCalled()
41864192
expect(ioDisconnectMock).toBeCalledTimes(1) // since React.StrictMode is enabled it should have disconnected
4193+
4194+
const output = screen.getByRole('status')
4195+
expect(output).toHaveTextContent('0')
4196+
4197+
const button = screen.getByRole('button', { name: 'Render' })
4198+
fireEvent.click(button)
4199+
await waitFor(() => {
4200+
expect(output).toHaveTextContent('1')
4201+
})
4202+
expect(ioObserveMock).toBeCalledTimes(2) // it should not observe again
4203+
expect(ioDisconnectMock).toBeCalledTimes(1) // it should not disconnect again
41874204
})
41884205

41894206
test("Router.preload='render', should trigger the route loader on render", async () => {

packages/solid-router/tests/link.test.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3716,15 +3716,21 @@ describe('Link', () => {
37163716

37173717
test('Router.preload="viewport", should trigger the IntersectionObserver\'s observe and disconnect methods', async () => {
37183718
const rootRoute = createRootRoute()
3719-
const indexRoute = createRoute({
3720-
getParentRoute: () => rootRoute,
3721-
path: '/',
3722-
component: () => (
3719+
const RouteComponent = () => {
3720+
const [count, setCount] = Solid.createSignal(0)
3721+
return (
37233722
<>
37243723
<h1>Index Heading</h1>
3724+
<output>{count()}</output>
3725+
<button onClick={() => setCount((c) => c + 1)}>Render</button>
37253726
<Link to="/">Index Link</Link>
37263727
</>
3727-
),
3728+
)
3729+
}
3730+
const indexRoute = createRoute({
3731+
getParentRoute: () => rootRoute,
3732+
path: '/',
3733+
component: RouteComponent,
37283734
})
37293735

37303736
const router = createRouter({
@@ -3737,8 +3743,19 @@ describe('Link', () => {
37373743
const indexLink = await screen.findByRole('link', { name: 'Index Link' })
37383744
expect(indexLink).toBeInTheDocument()
37393745

3740-
expect(ioObserveMock).toBeCalled()
3741-
expect(ioObserveMock).toBeCalledTimes(1)
3746+
expect(ioObserveMock).toHaveBeenCalledOnce()
3747+
expect(ioDisconnectMock).not.toHaveBeenCalled()
3748+
3749+
const output = screen.getByRole('status')
3750+
expect(output).toHaveTextContent('0')
3751+
3752+
const button = screen.getByRole('button', { name: 'Render' })
3753+
fireEvent.click(button)
3754+
await waitFor(() => {
3755+
expect(output).toHaveTextContent('1')
3756+
})
3757+
expect(ioObserveMock).toHaveBeenCalledOnce() // it should not observe again
3758+
expect(ioDisconnectMock).not.toHaveBeenCalled() // it should not disconnect again
37423759
})
37433760

37443761
test("Router.preload='render', should trigger the route loader on render", async () => {

0 commit comments

Comments
 (0)