返回 slidev
VClicks.test.ts
根目录 / packages / client / builtin / VClicks.test.ts
1 import type { VNode } from 'vue'
2 import { describe, expect, it } from 'vitest'
3 import { createSSRApp, defineComponent, h, provide, ref } from 'vue'
4 import { renderToString } from 'vue/server-renderer'
5 import {
6 injectionClicksContext,
7 injectionCurrentPage,
8 injectionFrontmatter,
9 injectionRenderContext,
10 injectionSlidevContext,
11 } from '../constants'
12 import VClicks from './VClicks'
13
14 function renderClicks(props: Record<string, unknown>, slot: () => VNode[]) {
15 const Root = defineComponent({
16 setup() {
17 provide(injectionClicksContext, ref({
18 register() {},
19 unregister() {},
20 get currentOffset() {
21 return 0
22 },
23 }) as never)
24 provide(injectionSlidevContext, { nav: {} } as never)
25 provide(injectionCurrentPage, ref(1))
26 provide(injectionRenderContext, ref('slide'))
27 provide(injectionFrontmatter, {})
28 return () => h(VClicks, props, slot)
29 },
30 })
31
32 const app = createSSRApp(Root)
33 app.directive('click', {
34 getSSRProps(binding) {
35 return {
36 'data-at': String(binding.value),
37 }
38 },
39 })
40 return renderToString(app)
41 }
42
43 /** Tags that received a click step, in document order, as `tag:at`. */
44 function clickSteps(html: string) {
45 return [
46 ...html.matchAll(/data-at="([^"]*)"/g),
47 ].map((match) => {
48 const tagStart = html.lastIndexOf('<', match.index)
49 const tag = /^[a-z0-9]+/.exec(html.slice(tagStart + 1))?.[0] ?? '?'
50 return `${tag}:${match[1]}`
51 })
52 }
53
54 describe('v-clicks lists and tables', () => {
55 it('reveals nested list items and table rows mixed with paragraphs', async () => {
56 const html = await renderClicks({ lists: 2, tables: 1 }, () => [
57 h('p', 'Here are the first priority items:'),
58 h('ul', [
59 h('li', 'Item 1'),
60 h('li', 'Item 2'),
61 h('li', 'Item 3'),
62 ]),
63 h('p', 'And others group of items:'),
64 h('ul', [
65 h('li', [
66 'Medium:',
67 h('ul', [
68 h('li', 'Sub item 1'),
69 h('li', 'Sub item 2'),
70 ]),
71 ]),
72 h('li', [
73 'Minor:',
74 h('ul', [
75 h('li', 'Sub item 3'),
76 ]),
77 ]),
78 ]),
79 h('table', [
80 h('thead', [
81 h('tr', [
82 h('th', 'Name'),
83 h('th', 'Priority'),
84 ]),
85 ]),
86 h('tbody', [
87 h('tr', [
88 h('td', 'Alpha'),
89 h('td', 'High'),
90 ]),
91 h('tr', [
92 h('td', 'Beta'),
93 h('td', 'Low'),
94 ]),
95 ]),
96 ]),
97 ])
98
99 expect(clickSteps(html)).toEqual([
100 'p:+1',
101 'li:+1',
102 'li:+1',
103 'li:+1',
104 'p:+1',
105 // Relative deltas follow mount order: nested items mount before their parent.
106 'li:-2',
107 'li:+2',
108 'li:+1',
109 'li:-1',
110 'li:+4',
111 'tr:+2',
112 'tr:+1',
113 ])
114 expect(html).not.toMatch(/<th[^>]*data-at=/)
115 })
116
117 it('stops at the requested list depth', async () => {
118 const slot = () => [
119 h('ul', [
120 h('li', [
121 'Parent',
122 h('ul', [
123 h('li', 'Child'),
124 ]),
125 ]),
126 ]),
127 ]
128
129 const shallow = await renderClicks({ lists: 1 }, slot)
130 expect(clickSteps(shallow)).toEqual(['li:+1'])
131 expect(shallow).not.toMatch(/<li[^>]*data-at="\+1"[^>]*>Child/)
132
133 const nested = await renderClicks({ lists: '2' }, slot)
134 expect(clickSteps(nested)).toEqual(['li:-1', 'li:+2'])
135 })
136
137 it('reveals a parent item before its nested items', async () => {
138 const html = await renderClicks({ depth: 3, at: 1 }, () => [
139 h('ul', [
140 h('li', [
141 'A',
142 h('ul', [
143 h('li', [
144 'B',
145 h('ul', [
146 h('li', 'C'),
147 ]),
148 ]),
149 ]),
150 ]),
151 h('li', 'D'),
152 ]),
153 ])
154
155 expect(clickSteps(html)).toEqual(['li:1', 'li:2', 'li:3', 'li:4'])
156 expect(html).toMatch(/data-at="1"[^>]*>A/)
157 expect(html).toMatch(/data-at="4"[^>]*>D/)
158
159 // Relative deltas follow Vue's mount order, which mounts children first.
160 const relative = await renderClicks({ depth: 3 }, () => [
161 h('ul', [
162 h('li', [
163 'A',
164 h('ul', [
165 h('li', 'B'),
166 ]),
167 ]),
168 h('li', 'C'),
169 ]),
170 ])
171 expect(clickSteps(relative)).toEqual(['li:-1', 'li:+2', 'li:+2'])
172 })
173
174 it('reveals body cells when tables is 2', async () => {
175 const html = await renderClicks({ tables: 2 }, () => [
176 h('p', 'Scores'),
177 h('table', [
178 h('tbody', [
179 h('tr', [
180 h('td', 'A'),
181 h('td', 'B'),
182 ]),
183 h('tr', [
184 h('td', 'C'),
185 ]),
186 ]),
187 ]),
188 ])
189
190 expect(clickSteps(html)).toEqual([
191 'p:+1',
192 'td:+1',
193 'td:+1',
194 'td:+1',
195 ])
196 })
197
198 it('keeps a lone list on the depth prop', async () => {
199 const slot = () => [
200 h('ul', [
201 h('li', 'A'),
202 h('li', [
203 h('ul', [
204 h('li', 'B'),
205 ]),
206 ]),
207 ]),
208 ]
209
210 expect(clickSteps(await renderClicks({ depth: 1 }, slot))).toEqual(['li:+1', 'li:+1'])
211 expect(clickSteps(await renderClicks({ depth: 2 }, slot))).toEqual(['li:+1', 'li:-1', 'li:+2'])
212 })
213
214 it('treats a whole list as one step when lists is omitted', async () => {
215 const html = await renderClicks({}, () => [
216 h('p', 'Intro'),
217 h('ul', [
218 h('li', 'Item 1'),
219 h('li', 'Item 2'),
220 ]),
221 ])
222
223 expect(clickSteps(html)).toEqual(['p:+1', 'ul:+1'])
224 })
225
226 it('groups steps with every', async () => {
227 const html = await renderClicks({ every: 2 }, () => [
228 h('ul', [
229 h('li', '1'),
230 h('li', '2'),
231 h('li', '3'),
232 h('li', '4'),
233 ]),
234 ])
235
236 expect(clickSteps(html)).toEqual(['li:+1', 'li:+0', 'li:+1', 'li:+0'])
237 })
238 })
239
239 lines TYPESCRIPT