@appica/ui-react 1.3.0 → 1.3.1
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 +24 -0
- package/agent-rules.md +2 -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 +2 -2
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +151 -32
- 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/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 +512 -12
- package/src/components/carousel/carousel.tsx +171 -33
- 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,7 +66,7 @@ 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
72
|
default: () => ({ name: 'autoplay', options: {}, init: () => {} }),
|
|
@@ -100,6 +101,7 @@ import {
|
|
|
100
101
|
useCarousel,
|
|
101
102
|
useLinkedCarousels,
|
|
102
103
|
type CarouselApi,
|
|
104
|
+
type CarouselOptions,
|
|
103
105
|
type CarouselProps,
|
|
104
106
|
type CarouselThumbsProps,
|
|
105
107
|
} from './carousel'
|
|
@@ -110,6 +112,51 @@ import useEmblaCarousel from 'embla-carousel-react'
|
|
|
110
112
|
// eslint-disable-next-line import/first
|
|
111
113
|
import { renderHook } from '@testing-library/react'
|
|
112
114
|
|
|
115
|
+
// eslint-disable-next-line import/first
|
|
116
|
+
import { Button } from '../button/button'
|
|
117
|
+
|
|
118
|
+
// eslint-disable-next-line import/first
|
|
119
|
+
import { ReducedMotionProvider } from '../../providers/reduced-motion-provider'
|
|
120
|
+
|
|
121
|
+
/** Calls every handler still subscribed to an Embla event on the stub api. */
|
|
122
|
+
function dispatch(event: string, payload?: unknown) {
|
|
123
|
+
const removed = new Set(stubApi.off.mock.calls.filter((c: unknown[]) => c[0] === event).map((c: unknown[]) => c[1]))
|
|
124
|
+
stubApi.on.mock.calls
|
|
125
|
+
.filter((c: unknown[]) => c[0] === event && !removed.has(c[1]))
|
|
126
|
+
.forEach((c: unknown[]) => (c[1] as (api: StubApi, payload?: unknown) => void)(stubApi, payload))
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function emit(event: string, payload?: unknown) {
|
|
130
|
+
act(() => dispatch(event, payload))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function mockDomSlides() {
|
|
134
|
+
stubApi.slideNodes.mockImplementation(() =>
|
|
135
|
+
Array.from(document.querySelectorAll<HTMLElement>('[data-slot="carousel-slide"]')),
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** A stand-in for the Autoplay plugin that reports play and stop through the stub's events. */
|
|
140
|
+
function mockAutoplay() {
|
|
141
|
+
let playing = false
|
|
142
|
+
const autoplay = {
|
|
143
|
+
options: { delay: 4000 },
|
|
144
|
+
isPlaying: vi.fn(() => playing),
|
|
145
|
+
play: vi.fn(() => {
|
|
146
|
+
playing = true
|
|
147
|
+
dispatch('autoplay:play')
|
|
148
|
+
}),
|
|
149
|
+
stop: vi.fn(() => {
|
|
150
|
+
if (!playing) return
|
|
151
|
+
playing = false
|
|
152
|
+
dispatch('autoplay:stop')
|
|
153
|
+
}),
|
|
154
|
+
reset: vi.fn(),
|
|
155
|
+
}
|
|
156
|
+
stubApi.plugins.mockReturnValue({ autoplay })
|
|
157
|
+
return autoplay
|
|
158
|
+
}
|
|
159
|
+
|
|
113
160
|
function Basic(props?: Partial<CarouselProps>) {
|
|
114
161
|
return (
|
|
115
162
|
<Carousel {...props}>
|
|
@@ -136,22 +183,63 @@ beforeEach(() => {
|
|
|
136
183
|
stubApi.plugins.mockReturnValue({})
|
|
137
184
|
stubApi.slideNodes.mockReturnValue([])
|
|
138
185
|
stubApi.containerNode.mockReturnValue(undefined)
|
|
186
|
+
stubApi.rootNode.mockReturnValue(undefined)
|
|
187
|
+
stubApi.internalEngine.mockReturnValue({
|
|
188
|
+
options: { active: true, axis: 'x' },
|
|
189
|
+
scrollSnapList: { slidesBySnap: [[0], [1], [2], [3]] },
|
|
190
|
+
})
|
|
191
|
+
stubApi.on.mockClear()
|
|
192
|
+
stubApi.off.mockClear()
|
|
139
193
|
})
|
|
140
194
|
|
|
141
195
|
describe('Carousel', () => {
|
|
142
|
-
it('renders
|
|
196
|
+
it('renders an unlabeled root as a carousel group with no default name', () => {
|
|
143
197
|
const { container } = render(<Basic />)
|
|
144
198
|
const root = container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
145
199
|
expect(root).not.toBeNull()
|
|
146
200
|
expect(root.getAttribute('data-orientation')).toBe('horizontal')
|
|
147
|
-
expect(root.getAttribute('role')).toBe('
|
|
201
|
+
expect(root.getAttribute('role')).toBe('group')
|
|
148
202
|
expect(root.getAttribute('aria-roledescription')).toBe('carousel')
|
|
149
|
-
expect(root.
|
|
203
|
+
expect(root.hasAttribute('aria-label')).toBe(false)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
it('becomes a region when labeled with aria-label', () => {
|
|
207
|
+
render(<Basic aria-label="Gallery" />)
|
|
208
|
+
expect(screen.getByRole('region', { name: 'Gallery' })).toHaveAttribute('data-slot', 'carousel')
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
it('becomes a region when labeled with aria-labelledby', () => {
|
|
212
|
+
render(
|
|
213
|
+
<>
|
|
214
|
+
<h2 id="gallery-heading">New arrivals</h2>
|
|
215
|
+
<Basic aria-labelledby="gallery-heading" />
|
|
216
|
+
</>,
|
|
217
|
+
)
|
|
218
|
+
expect(screen.getByRole('region', { name: 'New arrivals' })).toHaveAttribute('data-slot', 'carousel')
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('lets a consumer role and roledescription win', () => {
|
|
222
|
+
const { container } = render(<Basic role="presentation" aria-roledescription="gallery" />)
|
|
223
|
+
const root = container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
224
|
+
expect(root.getAttribute('role')).toBe('presentation')
|
|
225
|
+
expect(root.getAttribute('aria-roledescription')).toBe('gallery')
|
|
150
226
|
})
|
|
151
227
|
|
|
152
|
-
it('
|
|
228
|
+
it('merges a consumer ref with its own', () => {
|
|
229
|
+
const ref = React.createRef<HTMLDivElement>()
|
|
230
|
+
const { container } = render(<Basic {...({ ref } as Partial<CarouselProps>)} />)
|
|
231
|
+
expect(ref.current).toBe(container.querySelector('[data-slot="carousel"]'))
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('leaves the viewport without a role or name, so each carousel is one landmark at most', () => {
|
|
153
235
|
const { container } = render(<Basic aria-label="Gallery" />)
|
|
154
|
-
|
|
236
|
+
const viewport = container.querySelector('[data-slot="carousel-viewport"]') as HTMLElement
|
|
237
|
+
expect(viewport.hasAttribute('role')).toBe(false)
|
|
238
|
+
expect(viewport.hasAttribute('aria-label')).toBe(false)
|
|
239
|
+
const plugins = (useEmblaCarousel as unknown as ReturnType<typeof vi.fn>).mock.calls.at(-1)?.[1] as {
|
|
240
|
+
name: string
|
|
241
|
+
}[]
|
|
242
|
+
expect(plugins.map((plugin) => plugin.name)).not.toContain('accessibility')
|
|
155
243
|
})
|
|
156
244
|
|
|
157
245
|
it('renders viewport + content + slides with the expected data-slots and roles', () => {
|
|
@@ -208,6 +296,169 @@ describe('Carousel', () => {
|
|
|
208
296
|
})
|
|
209
297
|
})
|
|
210
298
|
|
|
299
|
+
describe('Carousel slides', () => {
|
|
300
|
+
function slides(container: HTMLElement) {
|
|
301
|
+
return Array.from(container.querySelectorAll<HTMLElement>('[data-slot="carousel-slide"]'))
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
it('makes slides outside the viewport inert, and keeps the visible ones reachable', () => {
|
|
305
|
+
mockDomSlides()
|
|
306
|
+
stubApi.slidesInView.mockReturnValue([0, 1])
|
|
307
|
+
const { container } = render(<Basic />)
|
|
308
|
+
const [first, second, third, fourth] = slides(container)
|
|
309
|
+
expect(first).not.toHaveAttribute('inert')
|
|
310
|
+
expect(second).not.toHaveAttribute('inert')
|
|
311
|
+
expect(third).toHaveAttribute('inert')
|
|
312
|
+
expect(fourth).toHaveAttribute('inert')
|
|
313
|
+
slides(container).forEach((slide) => expect(slide).not.toHaveAttribute('aria-hidden'))
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('labels each slide with its position', () => {
|
|
317
|
+
mockDomSlides()
|
|
318
|
+
const { container } = render(<Basic />)
|
|
319
|
+
expect(slides(container).map((slide) => slide.getAttribute('aria-label'))).toEqual([
|
|
320
|
+
'1 of 4',
|
|
321
|
+
'2 of 4',
|
|
322
|
+
'3 of 4',
|
|
323
|
+
'4 of 4',
|
|
324
|
+
])
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
it('keeps a slide label or labelledby of its own', () => {
|
|
328
|
+
mockDomSlides()
|
|
329
|
+
const { container } = render(
|
|
330
|
+
<Carousel>
|
|
331
|
+
<CarouselContent>
|
|
332
|
+
<CarouselSlide aria-label="Intro">A</CarouselSlide>
|
|
333
|
+
<CarouselSlide aria-labelledby="slide-2-title">
|
|
334
|
+
<h3 id="slide-2-title">Spring sale</h3>
|
|
335
|
+
</CarouselSlide>
|
|
336
|
+
<CarouselSlide>C</CarouselSlide>
|
|
337
|
+
</CarouselContent>
|
|
338
|
+
</Carousel>,
|
|
339
|
+
)
|
|
340
|
+
const [first, second, third] = slides(container)
|
|
341
|
+
expect(first).toHaveAttribute('aria-label', 'Intro')
|
|
342
|
+
expect(second).not.toHaveAttribute('aria-label')
|
|
343
|
+
expect(third).toHaveAttribute('aria-label', '3 of 3')
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
it('updates as slides scroll into view', () => {
|
|
347
|
+
mockDomSlides()
|
|
348
|
+
stubApi.slidesInView.mockReturnValue([0])
|
|
349
|
+
const { container } = render(<Basic />)
|
|
350
|
+
expect(slides(container)[1]).toHaveAttribute('inert')
|
|
351
|
+
stubApi.slidesInView.mockReturnValue([1, 2])
|
|
352
|
+
emit('slidesinview')
|
|
353
|
+
const [first, second, third] = slides(container)
|
|
354
|
+
expect(first).toHaveAttribute('inert')
|
|
355
|
+
expect(second).not.toHaveAttribute('inert')
|
|
356
|
+
expect(third).not.toHaveAttribute('inert')
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
it('falls back to the selected snap until the engine reports slides in view', () => {
|
|
360
|
+
mockDomSlides()
|
|
361
|
+
stubApi.slidesInView.mockReturnValue([])
|
|
362
|
+
stubApi.selectedSnap.mockReturnValue(2)
|
|
363
|
+
const { container } = render(<Basic />)
|
|
364
|
+
expect(slides(container).map((slide) => slide.hasAttribute('inert'))).toEqual([true, true, false, true])
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
it('follows the selected snap under fade, where every slide shares one place', () => {
|
|
368
|
+
mockDomSlides()
|
|
369
|
+
stubApi.plugins.mockReturnValue({ fade: {} })
|
|
370
|
+
stubApi.slidesInView.mockReturnValue([0, 1, 2, 3])
|
|
371
|
+
stubApi.selectedSnap.mockReturnValue(1)
|
|
372
|
+
const { container } = render(<Basic fade />)
|
|
373
|
+
expect(slides(container).map((slide) => slide.hasAttribute('inert'))).toEqual([true, false, true, true])
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
it('leaves every slide reachable while the engine is inactive', () => {
|
|
377
|
+
mockDomSlides()
|
|
378
|
+
stubApi.internalEngine.mockReturnValue({
|
|
379
|
+
options: { active: false },
|
|
380
|
+
scrollSnapList: { slidesBySnap: [[0], [1], [2], [3]] },
|
|
381
|
+
})
|
|
382
|
+
const { container } = render(<Basic active={false} />)
|
|
383
|
+
slides(container).forEach((slide) => expect(slide).not.toHaveAttribute('inert'))
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
it('never makes the slide holding focus inert, and re-checks once focus leaves', () => {
|
|
387
|
+
mockDomSlides()
|
|
388
|
+
stubApi.rootNode.mockImplementation(() => document.querySelector('[data-slot="carousel-viewport"]'))
|
|
389
|
+
stubApi.slidesInView.mockReturnValue([0, 1])
|
|
390
|
+
render(
|
|
391
|
+
<>
|
|
392
|
+
<Carousel>
|
|
393
|
+
<CarouselContent>
|
|
394
|
+
<CarouselSlide>A</CarouselSlide>
|
|
395
|
+
<CarouselSlide>
|
|
396
|
+
<button type="button">Shop now</button>
|
|
397
|
+
</CarouselSlide>
|
|
398
|
+
<CarouselSlide>C</CarouselSlide>
|
|
399
|
+
</CarouselContent>
|
|
400
|
+
</Carousel>
|
|
401
|
+
<button type="button">Outside</button>
|
|
402
|
+
</>,
|
|
403
|
+
)
|
|
404
|
+
const cta = screen.getByRole('button', { name: 'Shop now' })
|
|
405
|
+
const slide = cta.closest('[data-slot="carousel-slide"]') as HTMLElement
|
|
406
|
+
act(() => cta.focus())
|
|
407
|
+
|
|
408
|
+
stubApi.slidesInView.mockReturnValue([2])
|
|
409
|
+
emit('select')
|
|
410
|
+
expect(slide).not.toHaveAttribute('inert')
|
|
411
|
+
expect(document.activeElement).toBe(cta)
|
|
412
|
+
|
|
413
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
414
|
+
expect(slide).toHaveAttribute('inert')
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
it('removes what it set on unmount', () => {
|
|
418
|
+
mockDomSlides()
|
|
419
|
+
stubApi.slidesInView.mockReturnValue([0])
|
|
420
|
+
const { container, unmount } = render(<Basic />)
|
|
421
|
+
const nodes = slides(container)
|
|
422
|
+
unmount()
|
|
423
|
+
nodes.forEach((slide) => {
|
|
424
|
+
expect(slide).not.toHaveAttribute('inert')
|
|
425
|
+
expect(slide).not.toHaveAttribute('aria-label')
|
|
426
|
+
})
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
it('leaves slides alone with accessibility={false}', () => {
|
|
430
|
+
mockDomSlides()
|
|
431
|
+
const { container } = render(<Basic accessibility={false} />)
|
|
432
|
+
slides(container).forEach((slide) => {
|
|
433
|
+
expect(slide).not.toHaveAttribute('inert')
|
|
434
|
+
expect(slide).not.toHaveAttribute('aria-label')
|
|
435
|
+
})
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
it('hands slides to the Embla plugin for an options object, still without naming the viewport', () => {
|
|
439
|
+
mockDomSlides()
|
|
440
|
+
const { container } = render(<Basic accessibility={{ slideRole: 'listitem', carouselAriaRoleDescription: 'x' }} />)
|
|
441
|
+
slides(container).forEach((slide) => expect(slide).not.toHaveAttribute('inert'))
|
|
442
|
+
const plugins = (useEmblaCarousel as unknown as ReturnType<typeof vi.fn>).mock.calls.at(-1)?.[1] as {
|
|
443
|
+
name: string
|
|
444
|
+
options: object
|
|
445
|
+
}[]
|
|
446
|
+
expect(plugins.find((plugin) => plugin.name === 'accessibility')?.options).toEqual({
|
|
447
|
+
carouselRole: '',
|
|
448
|
+
carouselAriaLabel: '',
|
|
449
|
+
carouselAriaRoleDescription: 'x',
|
|
450
|
+
slideRole: 'listitem',
|
|
451
|
+
})
|
|
452
|
+
})
|
|
453
|
+
|
|
454
|
+
it('has no accessibility violations with off-screen slides inert', async () => {
|
|
455
|
+
mockDomSlides()
|
|
456
|
+
stubApi.slidesInView.mockReturnValue([0])
|
|
457
|
+
const { container } = render(<Basic aria-label="Featured" />)
|
|
458
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
459
|
+
})
|
|
460
|
+
})
|
|
461
|
+
|
|
211
462
|
describe('CarouselPrev / CarouselNext', () => {
|
|
212
463
|
it('renders as buttons with the right data-slot and aria-label', () => {
|
|
213
464
|
render(<Basic />)
|
|
@@ -271,14 +522,17 @@ describe('CarouselPrev / CarouselNext', () => {
|
|
|
271
522
|
expect(link.getAttribute('href')).toBe('#prev')
|
|
272
523
|
})
|
|
273
524
|
|
|
274
|
-
it('
|
|
525
|
+
it('marks prev aria-disabled, not natively disabled, when canGoToPrev() reports false', () => {
|
|
275
526
|
render(<Basic />)
|
|
276
527
|
const prev = screen.getByRole('button', { name: 'Previous slide' })
|
|
277
|
-
expect(prev).
|
|
528
|
+
expect(prev).toHaveAttribute('aria-disabled', 'true')
|
|
529
|
+
expect(prev).not.toHaveAttribute('disabled')
|
|
278
530
|
expect(prev.getAttribute('data-disabled')).not.toBeNull()
|
|
279
531
|
})
|
|
280
532
|
|
|
281
|
-
it('honors a consumer-supplied disabled prop on next', () => {
|
|
533
|
+
it('honors a consumer-supplied disabled prop on next', async () => {
|
|
534
|
+
const user = userEvent.setup()
|
|
535
|
+
stubApi.goToNext.mockClear()
|
|
282
536
|
render(
|
|
283
537
|
<Carousel>
|
|
284
538
|
<CarouselContent>
|
|
@@ -288,7 +542,34 @@ describe('CarouselPrev / CarouselNext', () => {
|
|
|
288
542
|
</Carousel>,
|
|
289
543
|
)
|
|
290
544
|
const next = screen.getByRole('button', { name: 'Next slide' })
|
|
291
|
-
expect(next).
|
|
545
|
+
expect(next).toHaveAttribute('aria-disabled', 'true')
|
|
546
|
+
await user.click(next)
|
|
547
|
+
expect(stubApi.goToNext).not.toHaveBeenCalled()
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
it('keeps focus on an arrow once it reaches the end', () => {
|
|
551
|
+
render(<Basic />)
|
|
552
|
+
const next = screen.getByRole('button', { name: 'Next slide' })
|
|
553
|
+
act(() => next.focus())
|
|
554
|
+
stubApi.canGoToNext.mockReturnValue(false)
|
|
555
|
+
emit('select')
|
|
556
|
+
expect(next).toHaveAttribute('aria-disabled', 'true')
|
|
557
|
+
expect(document.activeElement).toBe(next)
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
it('passes aria-disabled through a rendered Button without disabling it natively', () => {
|
|
561
|
+
render(
|
|
562
|
+
<Carousel>
|
|
563
|
+
<CarouselContent>
|
|
564
|
+
<CarouselSlide>1</CarouselSlide>
|
|
565
|
+
</CarouselContent>
|
|
566
|
+
<CarouselPrev render={<Button variant="outline" size="icon-md" />} />
|
|
567
|
+
</Carousel>,
|
|
568
|
+
)
|
|
569
|
+
const prev = screen.getByRole('button', { name: 'Previous slide' })
|
|
570
|
+
expect(prev).toHaveAttribute('aria-disabled', 'true')
|
|
571
|
+
expect(prev).toHaveAttribute('data-disabled')
|
|
572
|
+
expect(prev).not.toHaveAttribute('disabled')
|
|
292
573
|
})
|
|
293
574
|
|
|
294
575
|
it('calls api.goToNext when next is clicked', async () => {
|
|
@@ -350,6 +631,39 @@ describe('CarouselPagination', () => {
|
|
|
350
631
|
expect(stubApi.goTo).toHaveBeenCalledWith(2)
|
|
351
632
|
})
|
|
352
633
|
|
|
634
|
+
it('slides the autoplay indicator in rather than resizing it', () => {
|
|
635
|
+
mockAutoplay()
|
|
636
|
+
const { container } = render(<Basic autoplay />)
|
|
637
|
+
const indicator = container.querySelector('[data-slot="carousel-pagination-indicator"]') as HTMLElement
|
|
638
|
+
expect(indicator).not.toBeNull()
|
|
639
|
+
expect(indicator.style.width).toBe('')
|
|
640
|
+
expect(indicator.style.height).toBe('')
|
|
641
|
+
expect(indicator.style.transitionDuration).toBe('4000ms')
|
|
642
|
+
})
|
|
643
|
+
|
|
644
|
+
it('slides the vertical autoplay indicator in rather than resizing it', () => {
|
|
645
|
+
mockAutoplay()
|
|
646
|
+
const { container } = render(
|
|
647
|
+
<Carousel autoplay>
|
|
648
|
+
<CarouselContent>
|
|
649
|
+
<CarouselSlide>1</CarouselSlide>
|
|
650
|
+
<CarouselSlide>2</CarouselSlide>
|
|
651
|
+
<CarouselSlide>3</CarouselSlide>
|
|
652
|
+
</CarouselContent>
|
|
653
|
+
<CarouselPagination orientation="vertical" />
|
|
654
|
+
<CarouselProgress source="autoplay" orientation="vertical" />
|
|
655
|
+
</Carousel>,
|
|
656
|
+
)
|
|
657
|
+
const indicators = container.querySelectorAll<HTMLElement>(
|
|
658
|
+
'[data-slot="carousel-pagination-indicator"][data-orientation="vertical"]',
|
|
659
|
+
)
|
|
660
|
+
expect(indicators).toHaveLength(2)
|
|
661
|
+
indicators.forEach((indicator) => {
|
|
662
|
+
expect(indicator.style.width).toBe('')
|
|
663
|
+
expect(indicator.style.height).toBe('')
|
|
664
|
+
})
|
|
665
|
+
})
|
|
666
|
+
|
|
353
667
|
it('renders nothing when there is only one snap', () => {
|
|
354
668
|
stubApi.snapList.mockReturnValue([0])
|
|
355
669
|
render(
|
|
@@ -491,6 +805,34 @@ describe('CarouselThumbs / CarouselThumb', () => {
|
|
|
491
805
|
expect(options.containScroll).toBe('keepSnaps')
|
|
492
806
|
})
|
|
493
807
|
|
|
808
|
+
it('keeps thumbnails as plain buttons, outside any slide management', () => {
|
|
809
|
+
const mocked = vi.mocked(useEmblaCarousel)
|
|
810
|
+
const thumbsApi = {
|
|
811
|
+
...stubApi,
|
|
812
|
+
slideNodes: vi.fn(() => Array.from(document.querySelectorAll<HTMLElement>('[data-slot="carousel-thumb"]'))),
|
|
813
|
+
}
|
|
814
|
+
mockDomSlides()
|
|
815
|
+
mocked.mockImplementation(((options?: CarouselOptions) => {
|
|
816
|
+
const api = options?.slides ? thumbsApi : stubApi
|
|
817
|
+
return [vi.fn(), api, api]
|
|
818
|
+
}) as unknown as typeof useEmblaCarousel)
|
|
819
|
+
try {
|
|
820
|
+
const { container } = render(<WithThumbs />)
|
|
821
|
+
const thumbs = Array.from(container.querySelectorAll<HTMLElement>('[data-slot="carousel-thumb"]'))
|
|
822
|
+
expect(thumbs).toHaveLength(3)
|
|
823
|
+
thumbs.forEach((thumb, index) => {
|
|
824
|
+
expect(screen.getByRole('button', { name: `Go to slide ${index + 1}` })).toBe(thumb)
|
|
825
|
+
expect(thumb).not.toHaveAttribute('aria-hidden')
|
|
826
|
+
expect(thumb).not.toHaveAttribute('aria-roledescription')
|
|
827
|
+
expect(thumb).not.toHaveAttribute('inert')
|
|
828
|
+
})
|
|
829
|
+
const railPlugins = mocked.mock.calls.findLast(([options]) => options?.slides)?.[1] ?? []
|
|
830
|
+
expect(railPlugins.map((plugin) => plugin.name)).not.toContain('accessibility')
|
|
831
|
+
} finally {
|
|
832
|
+
mocked.mockImplementation((() => [vi.fn(), stubApi, stubApi]) as unknown as typeof useEmblaCarousel)
|
|
833
|
+
}
|
|
834
|
+
})
|
|
835
|
+
|
|
494
836
|
it('throws when a thumbnail is used outside CarouselThumbs', () => {
|
|
495
837
|
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
496
838
|
expect(() => render(<CarouselThumb>Orphan</CarouselThumb>)).toThrow(/CarouselThumb/)
|
|
@@ -572,7 +914,10 @@ describe('useCarousel + lifecycle', () => {
|
|
|
572
914
|
}
|
|
573
915
|
stubApi.plugins.mockReturnValue({ wheelGestures: {}, autoplay })
|
|
574
916
|
stubApi.rootNode.mockReturnValue(viewport)
|
|
575
|
-
stubApi.internalEngine.mockReturnValue({
|
|
917
|
+
stubApi.internalEngine.mockReturnValue({
|
|
918
|
+
options: { active: true, axis: 'x' },
|
|
919
|
+
scrollSnapList: { slidesBySnap: [] },
|
|
920
|
+
})
|
|
576
921
|
try {
|
|
577
922
|
render(<Basic autoplay wheelGestures />)
|
|
578
923
|
viewport.dispatchEvent(new WheelEvent('wheel', { deltaX: 2, deltaY: 40 }))
|
|
@@ -595,6 +940,146 @@ describe('useCarousel + lifecycle', () => {
|
|
|
595
940
|
})
|
|
596
941
|
})
|
|
597
942
|
|
|
943
|
+
describe('Carousel autoplay', () => {
|
|
944
|
+
function root(container: HTMLElement) {
|
|
945
|
+
return container.querySelector('[data-slot="carousel"]') as HTMLElement
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
it('starts rotation on mount', () => {
|
|
949
|
+
const autoplay = mockAutoplay()
|
|
950
|
+
render(<Basic autoplay />)
|
|
951
|
+
expect(autoplay.play).toHaveBeenCalledTimes(1)
|
|
952
|
+
})
|
|
953
|
+
|
|
954
|
+
it('stays off under reduced motion', () => {
|
|
955
|
+
const autoplay = mockAutoplay()
|
|
956
|
+
render(
|
|
957
|
+
<ReducedMotionProvider disableAnimations>
|
|
958
|
+
<Basic autoplay />
|
|
959
|
+
</ReducedMotionProvider>,
|
|
960
|
+
)
|
|
961
|
+
expect(autoplay.play).not.toHaveBeenCalled()
|
|
962
|
+
})
|
|
963
|
+
|
|
964
|
+
it('stops when reduced motion turns on, and starts again when it turns off', () => {
|
|
965
|
+
const autoplay = mockAutoplay()
|
|
966
|
+
const { rerender } = render(
|
|
967
|
+
<ReducedMotionProvider>
|
|
968
|
+
<Basic autoplay />
|
|
969
|
+
</ReducedMotionProvider>,
|
|
970
|
+
)
|
|
971
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
972
|
+
rerender(
|
|
973
|
+
<ReducedMotionProvider disableAnimations>
|
|
974
|
+
<Basic autoplay />
|
|
975
|
+
</ReducedMotionProvider>,
|
|
976
|
+
)
|
|
977
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
978
|
+
rerender(
|
|
979
|
+
<ReducedMotionProvider>
|
|
980
|
+
<Basic autoplay />
|
|
981
|
+
</ReducedMotionProvider>,
|
|
982
|
+
)
|
|
983
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
984
|
+
})
|
|
985
|
+
|
|
986
|
+
it('pauses while hovered and resumes when the pointer leaves', () => {
|
|
987
|
+
const autoplay = mockAutoplay()
|
|
988
|
+
const { container } = render(<Basic autoplay />)
|
|
989
|
+
fireEvent.pointerEnter(root(container))
|
|
990
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
991
|
+
expect(container.querySelector('[data-slot="carousel-pagination-indicator"]')).toBeNull()
|
|
992
|
+
fireEvent.pointerLeave(root(container))
|
|
993
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
994
|
+
expect(autoplay.play).toHaveBeenCalledTimes(2)
|
|
995
|
+
})
|
|
996
|
+
|
|
997
|
+
it('pauses while focus is inside and resumes once it leaves the carousel', () => {
|
|
998
|
+
const autoplay = mockAutoplay()
|
|
999
|
+
render(
|
|
1000
|
+
<>
|
|
1001
|
+
<Basic autoplay />
|
|
1002
|
+
<button type="button">Outside</button>
|
|
1003
|
+
</>,
|
|
1004
|
+
)
|
|
1005
|
+
act(() => screen.getByRole('button', { name: 'Next slide' }).focus())
|
|
1006
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1007
|
+
act(() => screen.getByRole('button', { name: 'Previous slide' }).focus())
|
|
1008
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1009
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1010
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1011
|
+
})
|
|
1012
|
+
|
|
1013
|
+
it('waits for both the pointer and focus to leave', () => {
|
|
1014
|
+
const autoplay = mockAutoplay()
|
|
1015
|
+
const { container } = render(
|
|
1016
|
+
<>
|
|
1017
|
+
<Basic autoplay />
|
|
1018
|
+
<button type="button">Outside</button>
|
|
1019
|
+
</>,
|
|
1020
|
+
)
|
|
1021
|
+
fireEvent.pointerEnter(root(container))
|
|
1022
|
+
act(() => screen.getByRole('button', { name: 'Next slide' }).focus())
|
|
1023
|
+
fireEvent.pointerLeave(root(container))
|
|
1024
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1025
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1026
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1027
|
+
})
|
|
1028
|
+
|
|
1029
|
+
it('keeps a pointerdown stop after the pointer leaves, without resumeAfter', () => {
|
|
1030
|
+
const autoplay = mockAutoplay()
|
|
1031
|
+
const { container } = render(<Basic autoplay />)
|
|
1032
|
+
fireEvent.pointerEnter(root(container))
|
|
1033
|
+
emit('pointerdown')
|
|
1034
|
+
fireEvent.pointerLeave(root(container))
|
|
1035
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1036
|
+
expect(autoplay.play).toHaveBeenCalledTimes(1)
|
|
1037
|
+
})
|
|
1038
|
+
|
|
1039
|
+
it('holds the resumeAfter restart while hovered or focused', () => {
|
|
1040
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1041
|
+
try {
|
|
1042
|
+
const autoplay = mockAutoplay()
|
|
1043
|
+
const { container } = render(
|
|
1044
|
+
<>
|
|
1045
|
+
<Basic autoplay={{ resumeAfter: 1000 }} />
|
|
1046
|
+
<button type="button">Outside</button>
|
|
1047
|
+
</>,
|
|
1048
|
+
)
|
|
1049
|
+
fireEvent.pointerEnter(root(container))
|
|
1050
|
+
emit('pointerdown')
|
|
1051
|
+
emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1052
|
+
act(() => vi.advanceTimersByTime(1000))
|
|
1053
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1054
|
+
|
|
1055
|
+
act(() => screen.getByRole('button', { name: 'Next slide' }).focus())
|
|
1056
|
+
fireEvent.pointerLeave(root(container))
|
|
1057
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1058
|
+
|
|
1059
|
+
act(() => screen.getByRole('button', { name: 'Outside' }).focus())
|
|
1060
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1061
|
+
} finally {
|
|
1062
|
+
vi.useRealTimers()
|
|
1063
|
+
}
|
|
1064
|
+
})
|
|
1065
|
+
|
|
1066
|
+
it('restarts after resumeAfter once nothing is inside', () => {
|
|
1067
|
+
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
|
|
1068
|
+
try {
|
|
1069
|
+
const autoplay = mockAutoplay()
|
|
1070
|
+
render(<Basic autoplay={{ resumeAfter: 1000 }} />)
|
|
1071
|
+
emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
|
|
1072
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1073
|
+
act(() => vi.advanceTimersByTime(999))
|
|
1074
|
+
expect(autoplay.isPlaying()).toBe(false)
|
|
1075
|
+
act(() => vi.advanceTimersByTime(1))
|
|
1076
|
+
expect(autoplay.isPlaying()).toBe(true)
|
|
1077
|
+
} finally {
|
|
1078
|
+
vi.useRealTimers()
|
|
1079
|
+
}
|
|
1080
|
+
})
|
|
1081
|
+
})
|
|
1082
|
+
|
|
598
1083
|
describe('useLinkedCarousels', () => {
|
|
599
1084
|
function makeApi() {
|
|
600
1085
|
const stub = {
|
|
@@ -664,6 +1149,21 @@ describe('a11y', () => {
|
|
|
664
1149
|
expect(await axe(container)).toHaveNoViolations()
|
|
665
1150
|
})
|
|
666
1151
|
|
|
1152
|
+
it('reports no landmark violations with several carousels on one page', async () => {
|
|
1153
|
+
const { container } = render(
|
|
1154
|
+
<main>
|
|
1155
|
+
<Basic />
|
|
1156
|
+
<Basic />
|
|
1157
|
+
<Basic aria-label="New arrivals" />
|
|
1158
|
+
<Basic aria-label="Best sellers" />
|
|
1159
|
+
</main>,
|
|
1160
|
+
)
|
|
1161
|
+
const results = await axe(container, {
|
|
1162
|
+
runOnly: ['landmark-unique', 'aria-allowed-role', 'aria-hidden-focus'],
|
|
1163
|
+
})
|
|
1164
|
+
expect(results).toHaveNoViolations()
|
|
1165
|
+
})
|
|
1166
|
+
|
|
667
1167
|
it('has no accessibility violations with a thumbnail rail', async () => {
|
|
668
1168
|
const { container } = render(
|
|
669
1169
|
<Carousel>
|