Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

link-style.test.tsx

218 lines6.3 KB

1import * as Solid from 'solid-js'
2import { createStore } from 'solid-js/store'
3import { afterEach, expect, test, vi } from 'vitest'
4import {
5 cleanup,
6 fireEvent,
7 render,
8 screen,
9 waitFor,
10} from '@solidjs/testing-library'
11import {
12 Link,
13 Outlet,
14 RouterProvider,
15 createMemoryHistory,
16 createRootRoute,
17 createRoute,
18 createRouter,
19} from '../src'
20​
21const disposers: Array<() => void> = []
22afterEach(() => {
23 cleanup()
24 for (const dispose of disposers.splice(0)) {
25 dispose()
26 }
27})
28​
29function renderLinks(Links: Solid.Component) {
30 const history = createMemoryHistory({ initialEntries: ['/target'] })
31 disposers.push(history.destroy)
32 const root = createRootRoute({
33 component: () => (
34 <>
35 <Links />
36 <Outlet />
37 </>
38 ),
39 })
40 const router = createRouter({
41 routeTree: root.addChildren([
42 createRoute({ getParentRoute: () => root, path: '/' }),
43 createRoute({ getParentRoute: () => root, path: '/target' }),
44 ]),
45 history,
46 scrollRestoration: false,
47 defaultPreload: false,
48 })
49 render(() => <RouterProvider router={router} />)
50 return router
52​
53test.each([true, false])(
54 'selected state props override element props but not routing options (active: %s)',
55 async (active) => {
56 const baseClick = vi.fn()
57 const selectedClick = vi.fn((event: MouseEvent) => event.preventDefault())
58 const unusedClick = vi.fn()
59 const selectedRef = vi.fn()
60 const stateProps = {
61 ref: selectedRef,
62 title: 'state title',
63 onClick: selectedClick,
64 class: 'state-class',
65 style: { color: 'blue' },
66 href: '/state-href',
67 target: '_self',
68 disabled: true,
69 }
70 renderLinks(() => (
71 <Link
72 to={active ? '/target' : '/'}
73 target="_blank"
74 title="base title"
75 onClick={baseClick}
76 class="base"
77 style={{ color: 'red', 'margin-top': '2px' }}
78 activeProps={active ? stateProps : { onClick: unusedClick }}
79 inactiveProps={active ? { onClick: unusedClick } : stateProps}
80 >
81 Override
82 </Link>
83 ))
84 const link = await screen.findByRole('link', { name: 'Override' })
85 expect(selectedRef).toHaveBeenCalledOnce()
86 expect(selectedRef.mock.calls[0]?.[0]).toBe(link)
87 expect(link).toHaveAttribute('title', 'state title')
88 expect(link).toHaveAttribute('href', active ? '/target' : '/')
89 expect(link).toHaveAttribute('target', '_blank')
90 expect(link).not.toHaveAttribute('disabled')
91 expect(link).toHaveClass('base', 'state-class')
92 expect(link.style).toMatchObject({ color: 'blue', marginTop: '2px' })
93 fireEvent.click(link)
94 expect(selectedClick).toHaveBeenCalledOnce()
95 expect(baseClick).not.toHaveBeenCalled()
96 expect(unusedClick).not.toHaveBeenCalled()
97 },
99​
100test('tracks additions to an initially empty state-only style store', async () => {
101 const [style, setStyle] = createStore<Solid.JSX.CSSProperties>({})
102 renderLinks(() => (
103 <Link to="/target" activeProps={() => ({ style })}>
104 Reactive
105 </Link>
106 ))
107 const link = await screen.findByRole('link', { name: 'Reactive' })
108 expect(link.style.color).toBe('')
109 setStyle('color', 'red')
110 await waitFor(() => expect(link.style.color).toBe('red'))
111 setStyle('color', undefined)
112 await waitFor(() => expect(link.style.color).toBe(''))
113})
114​
115test('merges only the selected state and tracks reactive style properties', async () => {
116 const [base, setBase] = createStore<Solid.JSX.CSSProperties>({
117 color: 'red',
118 'margin-top': '2px',
119 })
120 const [stateStyle, setStateStyle] = createStore<Solid.JSX.CSSProperties>({
121 'background-color': 'white',
122 })
123 const [stateClass, setStateClass] = Solid.createSignal('selected')
124 const active = vi.fn(() => ({
125 class: stateClass(),
126 style: stateStyle,
127 title: 'selected',
128 }))
129 const inactive = vi.fn(() => ({
130 class: 'idle',
131 style: { color: 'gray' },
132 title: 'idle',
133 }))
134 const router = renderLinks(() => (
135 <Link
136 to="/target"
137 class="base"
138 style={base}
139 activeProps={active}
140 inactiveProps={inactive}
141 >
142 Styled
143 </Link>
144 ))
145 const link = await screen.findByRole('link', { name: 'Styled' })
146 expect(link).toHaveClass('base', 'selected')
147 expect(link.style).toMatchObject({
148 color: 'red',
149 marginTop: '2px',
150 backgroundColor: 'white',
151 })
152 expect(link).toHaveAttribute('href', '/target')
153 expect(inactive).not.toHaveBeenCalled()
154​
155 setBase('color', 'blue')
156 setStateStyle('background-color', 'black')
157 setStateClass('updated')
158 await waitFor(() => {
159 expect(link).toHaveClass('base', 'updated')
160 expect(link.style).toMatchObject({
161 color: 'blue',
162 backgroundColor: 'black',
163 })
164 })
165​
166 await router.navigate({ to: '/' })
167 await waitFor(() => {
168 expect(link).toHaveClass('base', 'idle')
169 expect(link).toHaveAttribute('title', 'idle')
170 expect(link.style).toMatchObject({ color: 'gray', marginTop: '2px' })
171 expect(link.style.backgroundColor).toBe('')
172 })
173 active.mockClear()
174 inactive.mockClear()
175 setBase('margin-top', '3px')
176 await waitFor(() => expect(link.style).toMatchObject({ marginTop: '3px' }))
177 expect(active).not.toHaveBeenCalled()
178 expect(inactive).toHaveBeenCalled()
179​
180 await router.navigate({ to: '/target' })
181 await waitFor(() => {
182 expect(link).toHaveClass('base', 'updated')
183 expect(link.style).toMatchObject({
184 color: 'blue',
185 backgroundColor: 'black',
186 })
187 })
188})
189​
190test('switches between default and custom state props without stale attributes', async () => {
191 const [custom, setCustom] = Solid.createSignal(false)
192 const router = renderLinks(() => (
193 <Link
194 to="/target"
195 activeProps={
196 custom() ? { title: 'custom', style: { color: 'red' } } : undefined
197 }
198 >
199 Defaults
200 </Link>
201 ))
202 const link = await screen.findByRole('link', { name: 'Defaults' })
203 expect(link).toHaveClass('active')
204 setCustom(true)
205 await waitFor(() => {
206 expect(link).not.toHaveClass('active')
207 expect(link).toHaveAttribute('title', 'custom')
208 expect(link.style).toMatchObject({ color: 'red' })
209 })
210 setCustom(false)
211 await waitFor(() => {
212 expect(link).toHaveClass('active')
213 expect(link).not.toHaveAttribute('title')
214 expect(link.style.color).toBe('')
215 })
216 await router.navigate({ to: '/' })
217 await waitFor(() => expect(link).not.toHaveClass('active'))
218})