@appica/ui-react 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/agent-rules.md +3 -0
- package/appica.css +1 -1
- package/dist/components/autocomplete/autocomplete.d.ts.map +1 -1
- package/dist/components/autocomplete/autocomplete.js +1 -5
- package/dist/components/autocomplete/autocomplete.js.map +1 -1
- package/dist/components/breadcrumb/breadcrumb.js +1 -1
- package/dist/components/breadcrumb/breadcrumb.js.map +1 -1
- package/dist/components/carousel/carousel.d.ts +25 -4
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +277 -48
- package/dist/components/carousel/carousel.js.map +1 -1
- package/dist/components/combobox/combobox.d.ts.map +1 -1
- package/dist/components/combobox/combobox.js +1 -5
- package/dist/components/combobox/combobox.js.map +1 -1
- package/dist/components/copy-button/copy-button.d.ts +2 -2
- package/dist/components/copy-button/copy-button.d.ts.map +1 -1
- package/dist/components/copy-button/copy-button.js +49 -46
- package/dist/components/copy-button/copy-button.js.map +1 -1
- package/dist/components/date-field/date-field.d.ts.map +1 -1
- package/dist/components/date-field/date-field.js +7 -0
- package/dist/components/date-field/date-field.js.map +1 -1
- package/dist/components/date-picker/date-picker.d.ts +10 -0
- package/dist/components/date-picker/date-picker.d.ts.map +1 -1
- package/dist/components/date-picker/date-picker.js +14 -0
- package/dist/components/date-picker/date-picker.js.map +1 -1
- package/dist/components/field/field.d.ts.map +1 -1
- package/dist/components/field/field.js +1 -0
- package/dist/components/field/field.js.map +1 -1
- package/dist/components/loader/loader.d.ts +1 -1
- package/dist/components/loader/loader.d.ts.map +1 -1
- package/dist/components/loader/loader.js +7 -5
- package/dist/components/loader/loader.js.map +1 -1
- package/dist/components/number-field/number-field.d.ts +1 -1
- package/dist/components/number-field/number-field.d.ts.map +1 -1
- package/dist/components/number-field/number-field.js +7 -0
- package/dist/components/number-field/number-field.js.map +1 -1
- package/dist/components/pagination/pagination.d.ts.map +1 -1
- package/dist/components/pagination/pagination.js +1 -2
- package/dist/components/pagination/pagination.js.map +1 -1
- package/dist/components/spinner/spinner.d.ts +1 -1
- package/dist/components/spinner/spinner.d.ts.map +1 -1
- package/dist/components/spinner/spinner.js +7 -5
- package/dist/components/spinner/spinner.js.map +1 -1
- package/dist/components/time-field/time-field.d.ts.map +1 -1
- package/dist/components/time-field/time-field.js +7 -0
- package/dist/components/time-field/time-field.js.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +11 -11
- package/src/components/autocomplete/autocomplete.test.tsx +33 -0
- package/src/components/autocomplete/autocomplete.tsx +1 -5
- package/src/components/breadcrumb/breadcrumb.test.tsx +57 -2
- package/src/components/breadcrumb/breadcrumb.tsx +1 -1
- package/src/components/carousel/carousel.test.tsx +918 -13
- package/src/components/carousel/carousel.tsx +338 -54
- package/src/components/chip/chip.test.tsx +4 -5
- package/src/components/combobox/combobox.test.tsx +33 -0
- package/src/components/combobox/combobox.tsx +1 -5
- package/src/components/context-menu/context-menu.test.tsx +2 -1
- package/src/components/copy-button/copy-button.test.tsx +68 -3
- package/src/components/copy-button/copy-button.tsx +49 -43
- package/src/components/date-field/date-field.test.tsx +82 -0
- package/src/components/date-field/date-field.tsx +7 -0
- package/src/components/date-picker/date-picker.test.tsx +70 -0
- package/src/components/date-picker/date-picker.tsx +26 -0
- package/src/components/dropdown-menu/dropdown-menu.test.tsx +2 -1
- package/src/components/field/field.test.tsx +34 -0
- package/src/components/field/field.tsx +1 -0
- package/src/components/loader/loader.test.tsx +17 -4
- package/src/components/loader/loader.tsx +2 -2
- package/src/components/number-field/number-field.test.tsx +59 -0
- package/src/components/number-field/number-field.tsx +8 -0
- package/src/components/pagination/pagination.test.tsx +83 -0
- package/src/components/pagination/pagination.tsx +1 -2
- package/src/components/spinner/spinner.test.tsx +17 -4
- package/src/components/spinner/spinner.tsx +2 -2
- package/src/components/time-field/time-field.test.tsx +64 -0
- package/src/components/time-field/time-field.tsx +7 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { act, fireEvent, render, screen } from '@testing-library/react'
|
|
2
3
|
import userEvent from '@testing-library/user-event'
|
|
3
4
|
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
|
4
5
|
import { axe } from 'vitest-axe'
|
|
@@ -65,10 +66,10 @@ vi.mock('embla-carousel-react', () => ({
|
|
|
65
66
|
// Plugin factories: each mock factory inlines its stub so vitest hoisting
|
|
66
67
|
// doesn't trip on a shared helper reference.
|
|
67
68
|
vi.mock('embla-carousel-accessibility', () => ({
|
|
68
|
-
default: () => ({ name: 'accessibility', options: {}, init: () => {} }),
|
|
69
|
+
default: (options?: object) => ({ name: 'accessibility', options: options ?? {}, init: () => {} }),
|
|
69
70
|
}))
|
|
70
71
|
vi.mock('embla-carousel-autoplay', () => ({
|
|
71
|
-
default: () => ({ name: 'autoplay', options: {}, init: () => {} }),
|
|
72
|
+
default: (options?: object) => ({ name: 'autoplay', options: options ?? {}, init: () => {} }),
|
|
72
73
|
}))
|
|
73
74
|
vi.mock('embla-carousel-auto-scroll', () => ({
|
|
74
75
|
default: () => ({ name: 'autoScroll', options: {}, init: () => {} }),
|
|
@@ -95,11 +96,13 @@ import {
|
|
|
95
96
|
CarouselThumb,
|
|
96
97
|
CarouselPrev,
|
|
97
98
|
CarouselNext,
|
|
99
|
+
CarouselPlayToggle,
|
|
98
100
|
CarouselPagination,
|
|
99
101
|
CarouselProgress,
|
|
100
102
|
useCarousel,
|
|
101
103
|
useLinkedCarousels,
|
|
102
104
|
type CarouselApi,
|
|
105
|
+
type CarouselOptions,
|
|
103
106
|
type CarouselProps,
|
|
104
107
|
type CarouselThumbsProps,
|
|
105
108
|
} from './carousel'
|
|
@@ -110,6 +113,75 @@ import useEmblaCarousel from 'embla-carousel-react'
|
|
|
110
113
|
// eslint-disable-next-line import/first
|
|
111
114
|
import { renderHook } from '@testing-library/react'
|
|
112
115
|
|
|
116
|
+
// eslint-disable-next-line import/first
|
|
117
|
+
import { Button } from '../button/button'
|
|
118
|
+
|
|
119
|
+
// eslint-disable-next-line import/first
|
|
120
|
+
import { ReducedMotionProvider } from '../../providers/reduced-motion-provider'
|
|
121
|
+
|
|
122
|
+
/** Calls every handler still subscribed to an Embla event on the stub api. */
|
|
123
|
+
function dispatch(event: string, payload?: unknown) {
|
|
124
|
+
const removed = new Set(stubApi.off.mock.calls.filter((c: unknown[]) => c[0] === event).map((c: unknown[]) => c[1]))
|
|
125
|
+
stubApi.on.mock.calls
|
|
126
|
+
.filter((c: unknown[]) => c[0] === event && !removed.has(c[1]))
|
|
127
|
+
.forEach((c: unknown[]) => (c[1] as (api: StubApi, payload?: unknown) => void)(stubApi, payload))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function emit(event: string, payload?: unknown) {
|
|
131
|
+
act(() => dispatch(event, payload))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function mockDomSlides() {
|
|
135
|
+
stubApi.slideNodes.mockImplementation(() =>
|
|
136
|
+
Array.from(document.querySelectorAll<HTMLElement>('[data-slot="carousel-slide"]')),
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A stand-in for the Autoplay plugin that reports play and stop through the stub's events. */
|
|
141
|
+
function mockAutoplay(options: object = {}) {
|
|
142
|
+
let playing = false
|
|
143
|
+
const autoplay = {
|
|
144
|
+
options: { delay: 4000, ...options },
|
|
145
|
+
isPlaying: vi.fn(() => playing),
|
|
146
|
+
play: vi.fn(() => {
|
|
147
|
+
if (!playing) {
|
|
148
|
+
playing = true
|
|
149
|
+
dispatch('autoplay:play')
|
|
150
|
+
}
|
|
151
|
+
dispatch('autoplay:timerset')
|
|
152
|
+
}),
|
|
153
|
+
stop: vi.fn(() => {
|
|
154
|
+
if (!playing) return
|
|
155
|
+
playing = false
|
|
156
|
+
dispatch('autoplay:stop')
|
|
157
|
+
}),
|
|
158
|
+
reset: vi.fn(),
|
|
159
|
+
}
|
|
160
|
+
stubApi.plugins.mockReturnValue({ autoplay })
|
|
161
|
+
return autoplay
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A stand-in for the Auto Scroll plugin, in the same shape as `mockAutoplay`. */
|
|
165
|
+
function mockAutoScroll() {
|
|
166
|
+
let playing = false
|
|
167
|
+
const autoScroll = {
|
|
168
|
+
options: {},
|
|
169
|
+
isPlaying: vi.fn(() => playing),
|
|
170
|
+
play: vi.fn(() => {
|
|
171
|
+
if (playing) return
|
|
172
|
+
playing = true
|
|
173
|
+
dispatch('autoscroll:play')
|
|
174
|
+
}),
|
|
175
|
+
stop: vi.fn(() => {
|
|
176
|
+
if (!playing) return
|
|
177
|
+
playing = false
|
|
178
|
+
dispatch('autoscroll:stop')
|
|
179
|
+
}),
|
|
180
|
+
}
|
|
181
|
+
stubApi.plugins.mockReturnValue({ autoScroll })
|
|
182
|
+
return autoScroll
|
|
183
|
+
}
|
|
184
|
+
|
|
113
185
|
function Basic(props?: Partial<CarouselProps>) {
|
|
114
186
|
return (
|
|
115
187
|
<Carousel {...props}>
|
|
@@ -136,22 +208,63 @@ beforeEach(() => {
|
|
|
136
208
|
stubApi.plugins.mockReturnValue({})
|
|
137
209
|
stubApi.slideNodes.mockReturnValue([])
|
|
138
210
|
stubApi.containerNode.mockReturnValue(undefined)
|
|
211
|
+
stubApi.rootNode.mockReturnValue(undefined)
|
|
212
|
+
stubApi.internalEngine.mockReturnValue({
|
|
213
|
+
options: { active: true, axis: 'x' },
|
|
214
|
+
scrollSnapList: { slidesBySnap: [[0], [1], [2], [3]] },
|
|
215
|
+
})
|
|
216
|
+
stubApi.on.mockClear()
|
|
217
|
+
stubApi.off.mockClear()
|
|
139
218
|
})
|
|
140
219
|
|
|
141
220
|
describe('Carousel', () => {
|
|
142
|
-
it('renders
|
|
221
|
+
it('renders an unlabeled root as a carousel group with no default name', () => {
|
|
143
222
|
const { container } = render(<Basic />)
|
|
144
223
|
const root = container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
145
224
|
expect(root).not.toBeNull()
|
|
146
225
|
expect(root.getAttribute('data-orientation')).toBe('horizontal')
|
|
147
|
-
expect(root.getAttribute('role')).toBe('
|
|
226
|
+
expect(root.getAttribute('role')).toBe('group')
|
|
148
227
|
expect(root.getAttribute('aria-roledescription')).toBe('carousel')
|
|
149
|
-
expect(root.
|
|
228
|
+
expect(root.hasAttribute('aria-label')).toBe(false)
|
|
150
229
|
})
|
|
151
230
|
|
|
152
|
-
it('
|
|
231
|
+
it('becomes a region when labeled with aria-label', () => {
|
|
232
|
+
render(<Basic aria-label="Gallery" />)
|
|
233
|
+
expect(screen.getByRole('region', { name: 'Gallery' })).toHaveAttribute('data-slot', 'carousel')
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
it('becomes a region when labeled with aria-labelledby', () => {
|
|
237
|
+
render(
|
|
238
|
+
<>
|
|
239
|
+
<h2 id="gallery-heading">New arrivals</h2>
|
|
240
|
+
<Basic aria-labelledby="gallery-heading" />
|
|
241
|
+
</>,
|
|
242
|
+
)
|
|
243
|
+
expect(screen.getByRole('region', { name: 'New arrivals' })).toHaveAttribute('data-slot', 'carousel')
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
it('lets a consumer role and roledescription win', () => {
|
|
247
|
+
const { container } = render(<Basic role="presentation" aria-roledescription="gallery" />)
|
|
248
|
+
const root = container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
249
|
+
expect(root.getAttribute('role')).toBe('presentation')
|
|
250
|
+
expect(root.getAttribute('aria-roledescription')).toBe('gallery')
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('merges a consumer ref with its own', () => {
|
|
254
|
+
const ref = React.createRef<HTMLDivElement>()
|
|
255
|
+
const { container } = render(<Basic {...({ ref } as Partial<CarouselProps>)} />)
|
|
256
|
+
expect(ref.current).toBe(container.querySelector('[data-slot="carousel"]'))
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('leaves the viewport without a role or name, so each carousel is one landmark at most', () => {
|
|
153
260
|
const { container } = render(<Basic aria-label="Gallery" />)
|
|
154
|
-
|
|
261
|
+
const viewport = container.querySelector('[data-slot="carousel-viewport"]') as HTMLElement
|
|
262
|
+
expect(viewport.hasAttribute('role')).toBe(false)
|
|
263
|
+
expect(viewport.hasAttribute('aria-label')).toBe(false)
|
|
264
|
+
const plugins = (useEmblaCarousel as unknown as ReturnType<typeof vi.fn>).mock.calls.at(-1)?.[1] as {
|
|
265
|
+
name: string
|
|
266
|
+
}[]
|
|
267
|
+
expect(plugins.map((plugin) => plugin.name)).not.toContain('accessibility')
|
|
155
268
|
})
|
|
156
269
|
|
|
157
270
|
it('renders viewport + content + slides with the expected data-slots and roles', () => {
|
|
@@ -208,6 +321,169 @@ describe('Carousel', () => {
|
|
|
208
321
|
})
|
|
209
322
|
})
|
|
210
323
|
|
|
324
|
+
describe('Carousel slides', () => {
|
|
325
|
+
function slides(container: HTMLElement) {
|
|
326
|
+
return Array.from(container.querySelectorAll<HTMLElement>('[data-slot="carousel-slide"]'))
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
it('makes slides outside the viewport inert, and keeps the visible ones reachable', () => {
|
|
330
|
+
mockDomSlides()
|
|
331
|
+
stubApi.slidesInView.mockReturnValue([0, 1])
|
|
332
|
+
const { container } = render(<Basic />)
|
|
333
|
+
const [first, second, third, fourth] = slides(container)
|
|
334
|
+
expect(first).not.toHaveAttribute('inert')
|
|
335
|
+
expect(second).not.toHaveAttribute('inert')
|
|
336
|
+
expect(third).toHaveAttribute('inert')
|
|
337
|
+
expect(fourth).toHaveAttribute('inert')
|
|
338
|
+
slides(container).forEach((slide) => expect(slide).not.toHaveAttribute('aria-hidden'))
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
it('labels each slide with its position', () => {
|
|
342
|
+
mockDomSlides()
|
|
343
|
+
const { container } = render(<Basic />)
|
|
344
|
+
expect(slides(container).map((slide) => slide.getAttribute('aria-label'))).toEqual([
|
|
345
|
+
'1 of 4',
|
|
346
|
+
'2 of 4',
|
|
347
|
+
'3 of 4',
|
|
348
|
+
'4 of 4',
|
|
349
|
+
])
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
it('keeps a slide label or labelledby of its own', () => {
|
|
353
|
+
mockDomSlides()
|
|
354
|
+
const { container } = render(
|
|
355
|
+
<Carousel>
|
|
356
|
+
<CarouselContent>
|
|
357
|
+
<CarouselSlide aria-label="Intro">A</CarouselSlide>
|
|
358
|
+
<CarouselSlide aria-labelledby="slide-2-title">
|
|
359
|
+
<h3 id="slide-2-title">Spring sale</h3>
|
|
360
|
+
</CarouselSlide>
|
|
361
|
+
<CarouselSlide>C</CarouselSlide>
|
|
362
|
+
</CarouselContent>
|
|
363
|
+
</Carousel>,
|
|
364
|
+
)
|
|
365
|
+
const [first, second, third] = slides(container)
|
|
366
|
+
expect(first).toHaveAttribute('aria-label', 'Intro')
|
|
367
|
+
expect(second).not.toHaveAttribute('aria-label')
|
|
368
|
+
expect(third).toHaveAttribute('aria-label', '3 of 3')
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
it('updates as slides scroll into view', () => {
|
|
372
|
+
mockDomSlides()
|
|
373
|
+
stubApi.slidesInView.mockReturnValue([0])
|
|
374
|
+
const { container } = render(<Basic />)
|
|
375
|
+
expect(slides(container)[1]).toHaveAttribute('inert')
|
|
376
|
+
stubApi.slidesInView.mockReturnValue([1, 2])
|
|
377
|
+
emit('slidesinview')
|
|
378
|
+
const [first, second, third] = slides(container)
|
|
379
|
+
expect(first).toHaveAttribute('inert')
|
|
380
|
+
expect(second).not.toHaveAttribute('inert')
|
|
381
|
+
expect(third).not.toHaveAttribute('inert')
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
it('falls back to the selected snap until the engine reports slides in view', () => {
|
|
385
|
+
mockDomSlides()
|
|
386
|
+
stubApi.slidesInView.mockReturnValue([])
|
|
387
|
+
stubApi.selectedSnap.mockReturnValue(2)
|
|
388
|
+
const { container } = render(<Basic />)
|
|
389
|
+
expect(slides(container).map((slide) => slide.hasAttribute('inert'))).toEqual([true, true, false, true])
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
it('follows the selected snap under fade, where every slide shares one place', () => {
|
|
393
|
+
mockDomSlides()
|
|
394
|
+
stubApi.plugins.mockReturnValue({ fade: {} })
|
|
395
|
+
stubApi.slidesInView.mockReturnValue([0, 1, 2, 3])
|
|
396
|
+
stubApi.selectedSnap.mockReturnValue(1)
|
|
397
|
+
const { container } = render(<Basic fade />)
|
|
398
|
+
expect(slides(container).map((slide) => slide.hasAttribute('inert'))).toEqual([true, false, true, true])
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
it('leaves every slide reachable while the engine is inactive', () => {
|
|
402
|
+
mockDomSlides()
|
|
403
|
+
stubApi.internalEngine.mockReturnValue({
|
|
404
|
+
options: { active: false },
|
|
405
|
+
scrollSnapList: { slidesBySnap: [[0], [1], [2], [3]] },
|
|
406
|
+
})
|
|
407
|
+
const { container } = render(<Basic active={false} />)
|
|
408
|
+
slides(container).forEach((slide) => expect(slide).not.toHaveAttribute('inert'))
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
it('never makes the slide holding focus inert, and re-checks once focus leaves', () => {
|
|
412
|
+
mockDomSlides()
|
|
413
|
+
stubApi.rootNode.mockImplementation(() => document.querySelector('[data-slot="carousel-viewport"]'))
|
|
414
|
+
stubApi.slidesInView.mockReturnValue([0, 1])
|
|
415
|
+
render(
|
|
416
|
+
<>
|
|
417
|
+
<Carousel>
|
|
418
|
+
<CarouselContent>
|
|
419
|
+
<CarouselSlide>A</CarouselSlide>
|
|
420
|
+
<CarouselSlide>
|
|
421
|
+
<button type="button">Shop now</button>
|
|
422
|
+
</CarouselSlide>
|
|
423
|
+
<CarouselSlide>C</CarouselSlide>
|
|
424
|
+
</CarouselContent>
|
|
425
|
+
</Carousel>
|
|
426
|
+
<button type="button">Outside</button>
|
|
427
|
+
</>,
|
|
428
|
+
)
|
|
429
|
+
const cta = screen.getByRole('button', { name: 'Shop now' })
|
|
430
|
+
const slide = cta.closest('[data-slot="carousel-slide"]') as HTMLElement
|
|
431
|
+
act(() => cta.focus())
|
|
432
|
+
|
|
433
|
+
stubApi.slidesInView.mockReturnValue([2])
|
|
434
|
+
emit('select')
|
|
435
|
+
expect(slide).not.toHaveAttribute('inert')
|
|
436
|
+
expect(document.activeElement).toBe(cta)
|
|
437
|
+
|
|
438
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
439
|
+
expect(slide).toHaveAttribute('inert')
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
it('removes what it set on unmount', () => {
|
|
443
|
+
mockDomSlides()
|
|
444
|
+
stubApi.slidesInView.mockReturnValue([0])
|
|
445
|
+
const { container, unmount } = render(<Basic />)
|
|
446
|
+
const nodes = slides(container)
|
|
447
|
+
unmount()
|
|
448
|
+
nodes.forEach((slide) => {
|
|
449
|
+
expect(slide).not.toHaveAttribute('inert')
|
|
450
|
+
expect(slide).not.toHaveAttribute('aria-label')
|
|
451
|
+
})
|
|
452
|
+
})
|
|
453
|
+
|
|
454
|
+
it('leaves slides alone with accessibility={false}', () => {
|
|
455
|
+
mockDomSlides()
|
|
456
|
+
const { container } = render(<Basic accessibility={false} />)
|
|
457
|
+
slides(container).forEach((slide) => {
|
|
458
|
+
expect(slide).not.toHaveAttribute('inert')
|
|
459
|
+
expect(slide).not.toHaveAttribute('aria-label')
|
|
460
|
+
})
|
|
461
|
+
})
|
|
462
|
+
|
|
463
|
+
it('hands slides to the Embla plugin for an options object, still without naming the viewport', () => {
|
|
464
|
+
mockDomSlides()
|
|
465
|
+
const { container } = render(<Basic accessibility={{ slideRole: 'listitem', carouselAriaRoleDescription: 'x' }} />)
|
|
466
|
+
slides(container).forEach((slide) => expect(slide).not.toHaveAttribute('inert'))
|
|
467
|
+
const plugins = (useEmblaCarousel as unknown as ReturnType<typeof vi.fn>).mock.calls.at(-1)?.[1] as {
|
|
468
|
+
name: string
|
|
469
|
+
options: object
|
|
470
|
+
}[]
|
|
471
|
+
expect(plugins.find((plugin) => plugin.name === 'accessibility')?.options).toEqual({
|
|
472
|
+
carouselRole: '',
|
|
473
|
+
carouselAriaLabel: '',
|
|
474
|
+
carouselAriaRoleDescription: 'x',
|
|
475
|
+
slideRole: 'listitem',
|
|
476
|
+
})
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
it('has no accessibility violations with off-screen slides inert', async () => {
|
|
480
|
+
mockDomSlides()
|
|
481
|
+
stubApi.slidesInView.mockReturnValue([0])
|
|
482
|
+
const { container } = render(<Basic aria-label="Featured" />)
|
|
483
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
484
|
+
})
|
|
485
|
+
})
|
|
486
|
+
|
|
211
487
|
describe('CarouselPrev / CarouselNext', () => {
|
|
212
488
|
it('renders as buttons with the right data-slot and aria-label', () => {
|
|
213
489
|
render(<Basic />)
|
|
@@ -271,14 +547,17 @@ describe('CarouselPrev / CarouselNext', () => {
|
|
|
271
547
|
expect(link.getAttribute('href')).toBe('#prev')
|
|
272
548
|
})
|
|
273
549
|
|
|
274
|
-
it('
|
|
550
|
+
it('marks prev aria-disabled, not natively disabled, when canGoToPrev() reports false', () => {
|
|
275
551
|
render(<Basic />)
|
|
276
552
|
const prev = screen.getByRole('button', { name: 'Previous slide' })
|
|
277
|
-
expect(prev).
|
|
553
|
+
expect(prev).toHaveAttribute('aria-disabled', 'true')
|
|
554
|
+
expect(prev).not.toHaveAttribute('disabled')
|
|
278
555
|
expect(prev.getAttribute('data-disabled')).not.toBeNull()
|
|
279
556
|
})
|
|
280
557
|
|
|
281
|
-
it('honors a consumer-supplied disabled prop on next', () => {
|
|
558
|
+
it('honors a consumer-supplied disabled prop on next', async () => {
|
|
559
|
+
const user = userEvent.setup()
|
|
560
|
+
stubApi.goToNext.mockClear()
|
|
282
561
|
render(
|
|
283
562
|
<Carousel>
|
|
284
563
|
<CarouselContent>
|
|
@@ -288,7 +567,34 @@ describe('CarouselPrev / CarouselNext', () => {
|
|
|
288
567
|
</Carousel>,
|
|
289
568
|
)
|
|
290
569
|
const next = screen.getByRole('button', { name: 'Next slide' })
|
|
291
|
-
expect(next).
|
|
570
|
+
expect(next).toHaveAttribute('aria-disabled', 'true')
|
|
571
|
+
await user.click(next)
|
|
572
|
+
expect(stubApi.goToNext).not.toHaveBeenCalled()
|
|
573
|
+
})
|
|
574
|
+
|
|
575
|
+
it('keeps focus on an arrow once it reaches the end', () => {
|
|
576
|
+
render(<Basic />)
|
|
577
|
+
const next = screen.getByRole('button', { name: 'Next slide' })
|
|
578
|
+
act(() => next.focus())
|
|
579
|
+
stubApi.canGoToNext.mockReturnValue(false)
|
|
580
|
+
emit('select')
|
|
581
|
+
expect(next).toHaveAttribute('aria-disabled', 'true')
|
|
582
|
+
expect(document.activeElement).toBe(next)
|
|
583
|
+
})
|
|
584
|
+
|
|
585
|
+
it('passes aria-disabled through a rendered Button without disabling it natively', () => {
|
|
586
|
+
render(
|
|
587
|
+
<Carousel>
|
|
588
|
+
<CarouselContent>
|
|
589
|
+
<CarouselSlide>1</CarouselSlide>
|
|
590
|
+
</CarouselContent>
|
|
591
|
+
<CarouselPrev render={<Button variant="outline" size="icon-md" />} />
|
|
592
|
+
</Carousel>,
|
|
593
|
+
)
|
|
594
|
+
const prev = screen.getByRole('button', { name: 'Previous slide' })
|
|
595
|
+
expect(prev).toHaveAttribute('aria-disabled', 'true')
|
|
596
|
+
expect(prev).toHaveAttribute('data-disabled')
|
|
597
|
+
expect(prev).not.toHaveAttribute('disabled')
|
|
292
598
|
})
|
|
293
599
|
|
|
294
600
|
it('calls api.goToNext when next is clicked', async () => {
|
|
@@ -350,6 +656,39 @@ describe('CarouselPagination', () => {
|
|
|
350
656
|
expect(stubApi.goTo).toHaveBeenCalledWith(2)
|
|
351
657
|
})
|
|
352
658
|
|
|
659
|
+
it('slides the autoplay indicator in rather than resizing it', () => {
|
|
660
|
+
mockAutoplay()
|
|
661
|
+
const { container } = render(<Basic autoplay />)
|
|
662
|
+
const indicator = container.querySelector('[data-slot="carousel-pagination-indicator"]') as HTMLElement
|
|
663
|
+
expect(indicator).not.toBeNull()
|
|
664
|
+
expect(indicator.style.width).toBe('')
|
|
665
|
+
expect(indicator.style.height).toBe('')
|
|
666
|
+
expect(indicator.style.transitionDuration).toBe('4000ms')
|
|
667
|
+
})
|
|
668
|
+
|
|
669
|
+
it('slides the vertical autoplay indicator in rather than resizing it', () => {
|
|
670
|
+
mockAutoplay()
|
|
671
|
+
const { container } = render(
|
|
672
|
+
<Carousel autoplay>
|
|
673
|
+
<CarouselContent>
|
|
674
|
+
<CarouselSlide>1</CarouselSlide>
|
|
675
|
+
<CarouselSlide>2</CarouselSlide>
|
|
676
|
+
<CarouselSlide>3</CarouselSlide>
|
|
677
|
+
</CarouselContent>
|
|
678
|
+
<CarouselPagination orientation="vertical" />
|
|
679
|
+
<CarouselProgress source="autoplay" orientation="vertical" />
|
|
680
|
+
</Carousel>,
|
|
681
|
+
)
|
|
682
|
+
const indicators = container.querySelectorAll<HTMLElement>(
|
|
683
|
+
'[data-slot="carousel-pagination-indicator"][data-orientation="vertical"]',
|
|
684
|
+
)
|
|
685
|
+
expect(indicators).toHaveLength(2)
|
|
686
|
+
indicators.forEach((indicator) => {
|
|
687
|
+
expect(indicator.style.width).toBe('')
|
|
688
|
+
expect(indicator.style.height).toBe('')
|
|
689
|
+
})
|
|
690
|
+
})
|
|
691
|
+
|
|
353
692
|
it('renders nothing when there is only one snap', () => {
|
|
354
693
|
stubApi.snapList.mockReturnValue([0])
|
|
355
694
|
render(
|
|
@@ -491,6 +830,34 @@ describe('CarouselThumbs / CarouselThumb', () => {
|
|
|
491
830
|
expect(options.containScroll).toBe('keepSnaps')
|
|
492
831
|
})
|
|
493
832
|
|
|
833
|
+
it('keeps thumbnails as plain buttons, outside any slide management', () => {
|
|
834
|
+
const mocked = vi.mocked(useEmblaCarousel)
|
|
835
|
+
const thumbsApi = {
|
|
836
|
+
...stubApi,
|
|
837
|
+
slideNodes: vi.fn(() => Array.from(document.querySelectorAll<HTMLElement>('[data-slot="carousel-thumb"]'))),
|
|
838
|
+
}
|
|
839
|
+
mockDomSlides()
|
|
840
|
+
mocked.mockImplementation(((options?: CarouselOptions) => {
|
|
841
|
+
const api = options?.slides ? thumbsApi : stubApi
|
|
842
|
+
return [vi.fn(), api, api]
|
|
843
|
+
}) as unknown as typeof useEmblaCarousel)
|
|
844
|
+
try {
|
|
845
|
+
const { container } = render(<WithThumbs />)
|
|
846
|
+
const thumbs = Array.from(container.querySelectorAll<HTMLElement>('[data-slot="carousel-thumb"]'))
|
|
847
|
+
expect(thumbs).toHaveLength(3)
|
|
848
|
+
thumbs.forEach((thumb, index) => {
|
|
849
|
+
expect(screen.getByRole('button', { name: `Go to slide ${index + 1}` })).toBe(thumb)
|
|
850
|
+
expect(thumb).not.toHaveAttribute('aria-hidden')
|
|
851
|
+
expect(thumb).not.toHaveAttribute('aria-roledescription')
|
|
852
|
+
expect(thumb).not.toHaveAttribute('inert')
|
|
853
|
+
})
|
|
854
|
+
const railPlugins = mocked.mock.calls.findLast(([options]) => options?.slides)?.[1] ?? []
|
|
855
|
+
expect(railPlugins.map((plugin) => plugin.name)).not.toContain('accessibility')
|
|
856
|
+
} finally {
|
|
857
|
+
mocked.mockImplementation((() => [vi.fn(), stubApi, stubApi]) as unknown as typeof useEmblaCarousel)
|
|
858
|
+
}
|
|
859
|
+
})
|
|
860
|
+
|
|
494
861
|
it('throws when a thumbnail is used outside CarouselThumbs', () => {
|
|
495
862
|
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
496
863
|
expect(() => render(<CarouselThumb>Orphan</CarouselThumb>)).toThrow(/CarouselThumb/)
|
|
@@ -572,7 +939,10 @@ describe('useCarousel + lifecycle', () => {
|
|
|
572
939
|
}
|
|
573
940
|
stubApi.plugins.mockReturnValue({ wheelGestures: {}, autoplay })
|
|
574
941
|
stubApi.rootNode.mockReturnValue(viewport)
|
|
575
|
-
stubApi.internalEngine.mockReturnValue({
|
|
942
|
+
stubApi.internalEngine.mockReturnValue({
|
|
943
|
+
options: { active: true, axis: 'x' },
|
|
944
|
+
scrollSnapList: { slidesBySnap: [] },
|
|
945
|
+
})
|
|
576
946
|
try {
|
|
577
947
|
render(<Basic autoplay wheelGestures />)
|
|
578
948
|
viewport.dispatchEvent(new WheelEvent('wheel', { deltaX: 2, deltaY: 40 }))
|
|
@@ -595,6 +965,506 @@ describe('useCarousel + lifecycle', () => {
|
|
|
595
965
|
})
|
|
596
966
|
})
|
|
597
967
|
|
|
968
|
+
describe('Carousel autoplay', () => {
|
|
969
|
+
function root(container: HTMLElement) {
|
|
970
|
+
return container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
it('starts rotation on mount', () => {
|
|
974
|
+
const autoplay = mockAutoplay()
|
|
975
|
+
render(<Basic autoplay />)
|
|
976
|
+
expect(autoplay.play).toHaveBeenCalledTimes(1)
|
|
977
|
+
})
|
|
978
|
+
|
|
979
|
+
it('stays off under reduced motion', () => {
|
|
980
|
+
const autoplay = mockAutoplay()
|
|
981
|
+
render(
|
|
982
|
+
<ReducedMotionProvider disableAnimations>
|
|
983
|
+
<Basic autoplay />
|
|
984
|
+
</ReducedMotionProvider>,
|
|
985
|
+
)
|
|
986
|
+
expect(autoplay.play).not.toHaveBeenCalled()
|
|
987
|
+
})
|
|
988
|
+
|
|
989
|
+
it('stops when reduced motion turns on, and starts again when it turns off', () => {
|
|
990
|
+
const autoplay = mockAutoplay()
|
|
991
|
+
const { rerender } = render(
|
|
992
|
+
<ReducedMotionProvider>
|
|
993
|
+
<Basic autoplay />
|
|
994
|
+
</ReducedMotionProvider>,
|
|
995
|
+
)
|
|
996
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
997
|
+
rerender(
|
|
998
|
+
<ReducedMotionProvider disableAnimations>
|
|
999
|
+
<Basic autoplay />
|
|
1000
|
+
</ReducedMotionProvider>,
|
|
1001
|
+
)
|
|
1002
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1003
|
+
rerender(
|
|
1004
|
+
<ReducedMotionProvider>
|
|
1005
|
+
<Basic autoplay />
|
|
1006
|
+
</ReducedMotionProvider>,
|
|
1007
|
+
)
|
|
1008
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1009
|
+
})
|
|
1010
|
+
|
|
1011
|
+
it('pauses while hovered and resumes when the pointer leaves', () => {
|
|
1012
|
+
const autoplay = mockAutoplay()
|
|
1013
|
+
const { container } = render(<Basic autoplay />)
|
|
1014
|
+
fireEvent.pointerEnter(root(container))
|
|
1015
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1016
|
+
expect(container.querySelector('[data-slot="carousel-pagination-indicator"]')).toBeNull()
|
|
1017
|
+
fireEvent.pointerLeave(root(container))
|
|
1018
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1019
|
+
expect(autoplay.play).toHaveBeenCalledTimes(2)
|
|
1020
|
+
})
|
|
1021
|
+
|
|
1022
|
+
it('pauses while keyboard focus is inside and resumes once it leaves the carousel', async () => {
|
|
1023
|
+
const user = userEvent.setup()
|
|
1024
|
+
const autoplay = mockAutoplay()
|
|
1025
|
+
render(
|
|
1026
|
+
<>
|
|
1027
|
+
<Basic autoplay />
|
|
1028
|
+
<button type="button">Outside</button>
|
|
1029
|
+
</>,
|
|
1030
|
+
)
|
|
1031
|
+
await user.tab()
|
|
1032
|
+
expect(screen.getByRole('button', { name: 'Previous slide' })).toHaveFocus()
|
|
1033
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1034
|
+
await user.tab()
|
|
1035
|
+
expect(screen.getByRole('button', { name: 'Next slide' })).toHaveFocus()
|
|
1036
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1037
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1038
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1039
|
+
})
|
|
1040
|
+
|
|
1041
|
+
it('keeps playing after a mouse click on an arrow, and pauses once Tab lands on it', async () => {
|
|
1042
|
+
const user = userEvent.setup()
|
|
1043
|
+
const autoplay = mockAutoplay()
|
|
1044
|
+
render(
|
|
1045
|
+
<>
|
|
1046
|
+
<button type="button">Outside</button>
|
|
1047
|
+
<Basic autoplay />
|
|
1048
|
+
</>,
|
|
1049
|
+
)
|
|
1050
|
+
const next = screen.getByRole('button', { name: 'Next slide' })
|
|
1051
|
+
await user.click(next)
|
|
1052
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1053
|
+
await user.unhover(next)
|
|
1054
|
+
expect(next).toHaveFocus()
|
|
1055
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1056
|
+
|
|
1057
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1058
|
+
await user.tab()
|
|
1059
|
+
await user.tab()
|
|
1060
|
+
expect(next).toHaveFocus()
|
|
1061
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1062
|
+
})
|
|
1063
|
+
|
|
1064
|
+
it('hands rotation back when a mouse click follows keyboard focus inside', async () => {
|
|
1065
|
+
const user = userEvent.setup()
|
|
1066
|
+
const autoplay = mockAutoplay()
|
|
1067
|
+
render(<Basic autoplay={{ pauseOnHover: false }} />)
|
|
1068
|
+
await user.tab()
|
|
1069
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1070
|
+
await user.click(screen.getByRole('button', { name: 'Next slide' }))
|
|
1071
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1072
|
+
})
|
|
1073
|
+
|
|
1074
|
+
it('starts again with mouse focus still inside when reduced motion turns off', async () => {
|
|
1075
|
+
const user = userEvent.setup()
|
|
1076
|
+
const autoplay = mockAutoplay()
|
|
1077
|
+
const { rerender } = render(
|
|
1078
|
+
<ReducedMotionProvider>
|
|
1079
|
+
<Basic autoplay />
|
|
1080
|
+
</ReducedMotionProvider>,
|
|
1081
|
+
)
|
|
1082
|
+
const next = screen.getByRole('button', { name: 'Next slide' })
|
|
1083
|
+
await user.click(next)
|
|
1084
|
+
await user.unhover(next)
|
|
1085
|
+
expect(next).toHaveFocus()
|
|
1086
|
+
rerender(
|
|
1087
|
+
<ReducedMotionProvider disableAnimations>
|
|
1088
|
+
<Basic autoplay />
|
|
1089
|
+
</ReducedMotionProvider>,
|
|
1090
|
+
)
|
|
1091
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1092
|
+
rerender(
|
|
1093
|
+
<ReducedMotionProvider>
|
|
1094
|
+
<Basic autoplay />
|
|
1095
|
+
</ReducedMotionProvider>,
|
|
1096
|
+
)
|
|
1097
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1098
|
+
})
|
|
1099
|
+
|
|
1100
|
+
it('waits for both the pointer and focus to leave', async () => {
|
|
1101
|
+
const user = userEvent.setup()
|
|
1102
|
+
const autoplay = mockAutoplay()
|
|
1103
|
+
const { container } = render(
|
|
1104
|
+
<>
|
|
1105
|
+
<Basic autoplay />
|
|
1106
|
+
<button type="button">Outside</button>
|
|
1107
|
+
</>,
|
|
1108
|
+
)
|
|
1109
|
+
fireEvent.pointerEnter(root(container))
|
|
1110
|
+
await user.tab()
|
|
1111
|
+
fireEvent.pointerLeave(root(container))
|
|
1112
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1113
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1114
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1115
|
+
})
|
|
1116
|
+
|
|
1117
|
+
it('keeps playing while hovered with pauseOnHover: false', async () => {
|
|
1118
|
+
const user = userEvent.setup()
|
|
1119
|
+
const autoplay = mockAutoplay()
|
|
1120
|
+
const { container } = render(<Basic autoplay={{ pauseOnHover: false }} />)
|
|
1121
|
+
fireEvent.pointerEnter(root(container))
|
|
1122
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1123
|
+
await user.tab()
|
|
1124
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1125
|
+
})
|
|
1126
|
+
|
|
1127
|
+
it('keeps playing with keyboard focus inside with pauseOnFocus: false', async () => {
|
|
1128
|
+
const user = userEvent.setup()
|
|
1129
|
+
const autoplay = mockAutoplay()
|
|
1130
|
+
const { container } = render(<Basic autoplay={{ pauseOnFocus: false }} />)
|
|
1131
|
+
await user.tab()
|
|
1132
|
+
expect(screen.getByRole('button', { name: 'Previous slide' })).toHaveFocus()
|
|
1133
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1134
|
+
fireEvent.pointerEnter(root(container))
|
|
1135
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1136
|
+
})
|
|
1137
|
+
|
|
1138
|
+
it('keeps pauseOnHover and pauseOnFocus away from the Embla plugin', () => {
|
|
1139
|
+
render(<Basic autoplay={{ delay: 3000, resumeAfter: 1000, pauseOnHover: false, pauseOnFocus: false }} />)
|
|
1140
|
+
const plugins = (useEmblaCarousel as unknown as ReturnType<typeof vi.fn>).mock.calls.at(-1)?.[1] as {
|
|
1141
|
+
name: string
|
|
1142
|
+
options: object
|
|
1143
|
+
}[]
|
|
1144
|
+
expect(plugins.find((plugin) => plugin.name === 'autoplay')?.options).toEqual({
|
|
1145
|
+
delay: 3000,
|
|
1146
|
+
defaultInteraction: false,
|
|
1147
|
+
})
|
|
1148
|
+
})
|
|
1149
|
+
|
|
1150
|
+
it('keeps a pointerdown stop after the pointer leaves, without resumeAfter', () => {
|
|
1151
|
+
const autoplay = mockAutoplay()
|
|
1152
|
+
const { container } = render(<Basic autoplay />)
|
|
1153
|
+
fireEvent.pointerEnter(root(container))
|
|
1154
|
+
emit('pointerdown')
|
|
1155
|
+
fireEvent.pointerLeave(root(container))
|
|
1156
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1157
|
+
expect(autoplay.play).toHaveBeenCalledTimes(1)
|
|
1158
|
+
})
|
|
1159
|
+
|
|
1160
|
+
it('holds the resumeAfter restart while hovered or focused', async () => {
|
|
1161
|
+
const user = userEvent.setup()
|
|
1162
|
+
const autoplay = mockAutoplay({ defaultInteraction: false })
|
|
1163
|
+
const { container } = render(
|
|
1164
|
+
<>
|
|
1165
|
+
<Basic autoplay={{ resumeAfter: 1000 }} />
|
|
1166
|
+
<button type="button">Outside</button>
|
|
1167
|
+
</>,
|
|
1168
|
+
)
|
|
1169
|
+
await user.tab()
|
|
1170
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1171
|
+
try {
|
|
1172
|
+
fireEvent.pointerEnter(root(container))
|
|
1173
|
+
emit('pointerdown')
|
|
1174
|
+
emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1175
|
+
act(() => vi.advanceTimersByTime(1000))
|
|
1176
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1177
|
+
|
|
1178
|
+
fireEvent.pointerLeave(root(container))
|
|
1179
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1180
|
+
|
|
1181
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1182
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1183
|
+
} finally {
|
|
1184
|
+
vi.useRealTimers()
|
|
1185
|
+
}
|
|
1186
|
+
})
|
|
1187
|
+
|
|
1188
|
+
it('restarts after a drag with resumeAfter while the pointer and focus stay, with both pauses off', async () => {
|
|
1189
|
+
const user = userEvent.setup()
|
|
1190
|
+
const autoplay = mockAutoplay({ defaultInteraction: false })
|
|
1191
|
+
const { container } = render(<Basic autoplay={{ resumeAfter: 1000, pauseOnHover: false, pauseOnFocus: false }} />)
|
|
1192
|
+
fireEvent.pointerEnter(root(container))
|
|
1193
|
+
await user.tab()
|
|
1194
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1195
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1196
|
+
try {
|
|
1197
|
+
emit('pointerdown')
|
|
1198
|
+
emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1199
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1200
|
+
act(() => vi.advanceTimersByTime(1000))
|
|
1201
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1202
|
+
} finally {
|
|
1203
|
+
vi.useRealTimers()
|
|
1204
|
+
}
|
|
1205
|
+
})
|
|
1206
|
+
|
|
1207
|
+
it('restarts after resumeAfter once nothing is inside', () => {
|
|
1208
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1209
|
+
try {
|
|
1210
|
+
const autoplay = mockAutoplay({ defaultInteraction: false })
|
|
1211
|
+
render(<Basic autoplay={{ resumeAfter: 1000 }} />)
|
|
1212
|
+
emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1213
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1214
|
+
act(() => vi.advanceTimersByTime(999))
|
|
1215
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1216
|
+
act(() => vi.advanceTimersByTime(1))
|
|
1217
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1218
|
+
} finally {
|
|
1219
|
+
vi.useRealTimers()
|
|
1220
|
+
}
|
|
1221
|
+
})
|
|
1222
|
+
})
|
|
1223
|
+
|
|
1224
|
+
describe('CarouselPlayToggle', () => {
|
|
1225
|
+
function WithToggle({ children, ...props }: Partial<CarouselProps>) {
|
|
1226
|
+
return (
|
|
1227
|
+
<Carousel {...props}>
|
|
1228
|
+
<CarouselPlayToggle>
|
|
1229
|
+
<svg data-icon="pause" aria-hidden="true" className="in-data-paused:hidden" />
|
|
1230
|
+
<svg data-icon="play" aria-hidden="true" className="not-in-data-paused:hidden" />
|
|
1231
|
+
</CarouselPlayToggle>
|
|
1232
|
+
<CarouselContent>
|
|
1233
|
+
<CarouselSlide>Slide 1</CarouselSlide>
|
|
1234
|
+
<CarouselSlide>Slide 2</CarouselSlide>
|
|
1235
|
+
<CarouselSlide>Slide 3</CarouselSlide>
|
|
1236
|
+
</CarouselContent>
|
|
1237
|
+
<CarouselPrev />
|
|
1238
|
+
<CarouselNext />
|
|
1239
|
+
<CarouselPagination />
|
|
1240
|
+
{children}
|
|
1241
|
+
</Carousel>
|
|
1242
|
+
)
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
function CycleProbe() {
|
|
1246
|
+
const { autoplay } = useCarousel()
|
|
1247
|
+
return <output data-testid="cycle">{autoplay?.cycleId}</output>
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
function root(container: HTMLElement) {
|
|
1251
|
+
return container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
const toggle = () => screen.getByRole('button', { name: 'Pause slides' })
|
|
1255
|
+
|
|
1256
|
+
it('renders nothing without Autoplay or Auto Scroll', () => {
|
|
1257
|
+
render(<WithToggle />)
|
|
1258
|
+
expect(screen.queryByRole('button', { name: 'Pause slides' })).toBeNull()
|
|
1259
|
+
})
|
|
1260
|
+
|
|
1261
|
+
it('renders nothing under reduced motion', () => {
|
|
1262
|
+
mockAutoplay()
|
|
1263
|
+
render(
|
|
1264
|
+
<ReducedMotionProvider disableAnimations>
|
|
1265
|
+
<WithToggle autoplay />
|
|
1266
|
+
</ReducedMotionProvider>,
|
|
1267
|
+
)
|
|
1268
|
+
expect(screen.queryByRole('button', { name: 'Pause slides' })).toBeNull()
|
|
1269
|
+
})
|
|
1270
|
+
|
|
1271
|
+
it('renders both icons as children and marks the pause with aria-pressed and data-paused', async () => {
|
|
1272
|
+
const user = userEvent.setup()
|
|
1273
|
+
const autoplay = mockAutoplay()
|
|
1274
|
+
render(<WithToggle autoplay />)
|
|
1275
|
+
const button = toggle()
|
|
1276
|
+
expect(button).toHaveAttribute('data-slot', 'carousel-play-toggle')
|
|
1277
|
+
expect(button).toHaveAttribute('aria-pressed', 'false')
|
|
1278
|
+
expect(button).not.toHaveAttribute('data-paused')
|
|
1279
|
+
expect(button.querySelector('[data-icon="pause"]')).not.toBeNull()
|
|
1280
|
+
expect(button.querySelector('[data-icon="play"]')).not.toBeNull()
|
|
1281
|
+
|
|
1282
|
+
await user.click(button)
|
|
1283
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1284
|
+
expect(toggle()).toHaveAttribute('data-paused')
|
|
1285
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1286
|
+
|
|
1287
|
+
await user.click(toggle())
|
|
1288
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'false')
|
|
1289
|
+
expect(toggle()).not.toHaveAttribute('data-paused')
|
|
1290
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1291
|
+
})
|
|
1292
|
+
|
|
1293
|
+
it('passes paused to a render function', async () => {
|
|
1294
|
+
const user = userEvent.setup()
|
|
1295
|
+
mockAutoplay()
|
|
1296
|
+
render(
|
|
1297
|
+
<Carousel autoplay>
|
|
1298
|
+
<CarouselPlayToggle render={(props, state) => <button {...props}>{state.paused ? 'Play' : 'Pause'}</button>} />
|
|
1299
|
+
<CarouselContent>
|
|
1300
|
+
<CarouselSlide>Slide 1</CarouselSlide>
|
|
1301
|
+
<CarouselSlide>Slide 2</CarouselSlide>
|
|
1302
|
+
</CarouselContent>
|
|
1303
|
+
</Carousel>,
|
|
1304
|
+
)
|
|
1305
|
+
expect(toggle()).toHaveTextContent('Pause')
|
|
1306
|
+
await user.click(toggle())
|
|
1307
|
+
expect(toggle()).toHaveTextContent('Play')
|
|
1308
|
+
})
|
|
1309
|
+
|
|
1310
|
+
it('keeps a pause through hover and focus leaving, resumeAfter, a drag, slide focus and the wheel', async () => {
|
|
1311
|
+
const user = userEvent.setup()
|
|
1312
|
+
const viewport = document.createElement('div')
|
|
1313
|
+
const autoplay = mockAutoplay({ defaultInteraction: false })
|
|
1314
|
+
stubApi.plugins.mockReturnValue({ autoplay, wheelGestures: {} })
|
|
1315
|
+
stubApi.rootNode.mockReturnValue(viewport)
|
|
1316
|
+
render(
|
|
1317
|
+
<>
|
|
1318
|
+
<WithToggle autoplay={{ resumeAfter: 1000 }} />
|
|
1319
|
+
<button type="button">Outside</button>
|
|
1320
|
+
</>,
|
|
1321
|
+
)
|
|
1322
|
+
await user.click(toggle())
|
|
1323
|
+
await user.tab()
|
|
1324
|
+
expect(screen.getByRole('button', { name: 'Previous slide' })).toHaveFocus()
|
|
1325
|
+
await user.unhover(toggle())
|
|
1326
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1327
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1328
|
+
|
|
1329
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1330
|
+
try {
|
|
1331
|
+
emit('pointerdown')
|
|
1332
|
+
emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1333
|
+
emit('slidefocus')
|
|
1334
|
+
emit('autoplay:interaction', { detail: { interaction: 'slidefocus' } })
|
|
1335
|
+
act(() => {
|
|
1336
|
+
viewport.dispatchEvent(new WheelEvent('wheel', { deltaX: 40, deltaY: 2 }))
|
|
1337
|
+
})
|
|
1338
|
+
act(() => vi.advanceTimersByTime(1000))
|
|
1339
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1340
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1341
|
+
} finally {
|
|
1342
|
+
vi.useRealTimers()
|
|
1343
|
+
}
|
|
1344
|
+
})
|
|
1345
|
+
|
|
1346
|
+
it('stays paused when paused during a hold, once the hold ends', () => {
|
|
1347
|
+
const autoplay = mockAutoplay()
|
|
1348
|
+
const { container } = render(<WithToggle autoplay />)
|
|
1349
|
+
fireEvent.pointerEnter(root(container))
|
|
1350
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1351
|
+
fireEvent.click(toggle())
|
|
1352
|
+
fireEvent.pointerLeave(root(container))
|
|
1353
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1354
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1355
|
+
})
|
|
1356
|
+
|
|
1357
|
+
it('keeps the pause when new options restart the plugin wiring', () => {
|
|
1358
|
+
const autoplay = mockAutoplay()
|
|
1359
|
+
const { rerender } = render(<WithToggle autoplay />)
|
|
1360
|
+
fireEvent.click(toggle())
|
|
1361
|
+
rerender(<WithToggle autoplay={{ resumeAfter: 500, pauseOnHover: false }} />)
|
|
1362
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1363
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1364
|
+
})
|
|
1365
|
+
|
|
1366
|
+
it('starts a fresh cycle on play, even while hovered', () => {
|
|
1367
|
+
const autoplay = mockAutoplay()
|
|
1368
|
+
const { container } = render(
|
|
1369
|
+
<WithToggle autoplay>
|
|
1370
|
+
<CycleProbe />
|
|
1371
|
+
</WithToggle>,
|
|
1372
|
+
)
|
|
1373
|
+
fireEvent.click(toggle())
|
|
1374
|
+
fireEvent.pointerEnter(root(container))
|
|
1375
|
+
const cycle = Number(screen.getByTestId('cycle').textContent)
|
|
1376
|
+
autoplay.play.mockClear()
|
|
1377
|
+
fireEvent.click(toggle())
|
|
1378
|
+
expect(autoplay.play).toHaveBeenCalledTimes(1)
|
|
1379
|
+
expect(Number(screen.getByTestId('cycle').textContent)).toBe(cycle + 1)
|
|
1380
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1381
|
+
})
|
|
1382
|
+
|
|
1383
|
+
it('toggles with Enter and Space', async () => {
|
|
1384
|
+
const user = userEvent.setup()
|
|
1385
|
+
const autoplay = mockAutoplay()
|
|
1386
|
+
render(<WithToggle autoplay />)
|
|
1387
|
+
await user.tab()
|
|
1388
|
+
expect(toggle()).toHaveFocus()
|
|
1389
|
+
await user.keyboard('{Enter}')
|
|
1390
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1391
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1392
|
+
await user.keyboard(' ')
|
|
1393
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'false')
|
|
1394
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1395
|
+
})
|
|
1396
|
+
|
|
1397
|
+
it('reads a drag that stops autoplay for good as a pause', () => {
|
|
1398
|
+
mockAutoplay()
|
|
1399
|
+
render(<WithToggle autoplay />)
|
|
1400
|
+
emit('pointerdown')
|
|
1401
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1402
|
+
})
|
|
1403
|
+
|
|
1404
|
+
it('stays unpressed after a drag when autoplay keeps running through it', () => {
|
|
1405
|
+
mockAutoplay({ defaultInteraction: false })
|
|
1406
|
+
render(<WithToggle autoplay={{ defaultInteraction: false }} />)
|
|
1407
|
+
emit('pointerdown')
|
|
1408
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'false')
|
|
1409
|
+
})
|
|
1410
|
+
|
|
1411
|
+
it('clears the pause when the plugin is played directly', () => {
|
|
1412
|
+
const autoplay = mockAutoplay()
|
|
1413
|
+
render(<WithToggle autoplay />)
|
|
1414
|
+
fireEvent.click(toggle())
|
|
1415
|
+
act(() => autoplay.play())
|
|
1416
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'false')
|
|
1417
|
+
})
|
|
1418
|
+
|
|
1419
|
+
it('drives Auto Scroll, and resumeAfter leaves its pause alone', () => {
|
|
1420
|
+
const autoScroll = mockAutoScroll()
|
|
1421
|
+
render(<WithToggle autoScroll={{ resumeAfter: 1000 }} />)
|
|
1422
|
+
expect(autoScroll.isPlaying()).toBe(true)
|
|
1423
|
+
fireEvent.click(toggle())
|
|
1424
|
+
expect(autoScroll.isPlaying()).toBe(false)
|
|
1425
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'true')
|
|
1426
|
+
|
|
1427
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1428
|
+
try {
|
|
1429
|
+
emit('autoscroll:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1430
|
+
act(() => vi.advanceTimersByTime(1000))
|
|
1431
|
+
expect(autoScroll.isPlaying()).toBe(false)
|
|
1432
|
+
} finally {
|
|
1433
|
+
vi.useRealTimers()
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
fireEvent.click(toggle())
|
|
1437
|
+
expect(autoScroll.isPlaying()).toBe(true)
|
|
1438
|
+
expect(toggle()).toHaveAttribute('aria-pressed', 'false')
|
|
1439
|
+
})
|
|
1440
|
+
|
|
1441
|
+
it('gives a custom control the same paused, pause and play through useCarousel()', async () => {
|
|
1442
|
+
const user = userEvent.setup()
|
|
1443
|
+
const autoplay = mockAutoplay()
|
|
1444
|
+
function Custom() {
|
|
1445
|
+
const { paused, pause, play } = useCarousel()
|
|
1446
|
+
return (
|
|
1447
|
+
<button type="button" onClick={paused ? play : pause}>
|
|
1448
|
+
{paused ? 'Resume' : 'Hold'}
|
|
1449
|
+
</button>
|
|
1450
|
+
)
|
|
1451
|
+
}
|
|
1452
|
+
render(
|
|
1453
|
+
<Carousel autoplay={{ pauseOnHover: false }}>
|
|
1454
|
+
<Custom />
|
|
1455
|
+
<CarouselContent>
|
|
1456
|
+
<CarouselSlide>Slide 1</CarouselSlide>
|
|
1457
|
+
<CarouselSlide>Slide 2</CarouselSlide>
|
|
1458
|
+
</CarouselContent>
|
|
1459
|
+
</Carousel>,
|
|
1460
|
+
)
|
|
1461
|
+
await user.click(screen.getByRole('button', { name: 'Hold' }))
|
|
1462
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1463
|
+
await user.click(screen.getByRole('button', { name: 'Resume' }))
|
|
1464
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1465
|
+
})
|
|
1466
|
+
})
|
|
1467
|
+
|
|
598
1468
|
describe('useLinkedCarousels', () => {
|
|
599
1469
|
function makeApi() {
|
|
600
1470
|
const stub = {
|
|
@@ -664,6 +1534,41 @@ describe('a11y', () => {
|
|
|
664
1534
|
expect(await axe(container)).toHaveNoViolations()
|
|
665
1535
|
})
|
|
666
1536
|
|
|
1537
|
+
it('reports no landmark violations with several carousels on one page', async () => {
|
|
1538
|
+
const { container } = render(
|
|
1539
|
+
<main>
|
|
1540
|
+
<Basic />
|
|
1541
|
+
<Basic />
|
|
1542
|
+
<Basic aria-label="New arrivals" />
|
|
1543
|
+
<Basic aria-label="Best sellers" />
|
|
1544
|
+
</main>,
|
|
1545
|
+
)
|
|
1546
|
+
const results = await axe(container, {
|
|
1547
|
+
runOnly: ['landmark-unique', 'aria-allowed-role', 'aria-hidden-focus'],
|
|
1548
|
+
})
|
|
1549
|
+
expect(results).toHaveNoViolations()
|
|
1550
|
+
})
|
|
1551
|
+
|
|
1552
|
+
it('has no accessibility violations with a play toggle, playing or paused', async () => {
|
|
1553
|
+
mockAutoplay()
|
|
1554
|
+
const { container } = render(
|
|
1555
|
+
<Carousel autoplay aria-label="Featured">
|
|
1556
|
+
<CarouselPlayToggle>
|
|
1557
|
+
<svg aria-hidden="true" />
|
|
1558
|
+
</CarouselPlayToggle>
|
|
1559
|
+
<CarouselContent>
|
|
1560
|
+
<CarouselSlide>Slide 1</CarouselSlide>
|
|
1561
|
+
<CarouselSlide>Slide 2</CarouselSlide>
|
|
1562
|
+
</CarouselContent>
|
|
1563
|
+
<CarouselPrev />
|
|
1564
|
+
<CarouselNext />
|
|
1565
|
+
</Carousel>,
|
|
1566
|
+
)
|
|
1567
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
1568
|
+
fireEvent.click(screen.getByRole('button', { name: 'Pause slides' }))
|
|
1569
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
1570
|
+
})
|
|
1571
|
+
|
|
667
1572
|
it('has no accessibility violations with a thumbnail rail', async () => {
|
|
668
1573
|
const { container } = render(
|
|
669
1574
|
<Carousel>
|