import { mount } from '@vue/test-utils' import { expect, it, describe, beforeAll, vi, afterEach, beforeEach } from 'vitest' import { Component, createSSRApp, defineComponent, h, nextTick, ref } from 'vue' import { renderToString } from 'vue/server-renderer' import { Carousel, Navigation, Pagination, Slide } from '@/index' import App from '../components/BasicApp.vue' import SlottedApp from '../components/SlottedApp.vue' import type { ComponentProps } from 'vue-component-type-helpers' describe('Carousel.ts', () => { let wrapper: ReturnType> beforeEach(async () => { wrapper = await mount(App, { props: { slideNum: 5, modelValue: 0, 'onUpdate:modelValue': (e: number) => wrapper.setProps({ modelValue: e }), }, }) }) // A carousel left mounted keeps its transition timer, which can fire after the // test environment is torn down and fail the run with an unhandled error. afterEach(() => { wrapper.unmount() }) it('It renders *five* slides correctly', () => { const slides = wrapper.findAll('.carousel__slide') expect(slides.length).toBe(5) }) it('Should display *one* visible item', () => { const slides = wrapper.findAll('.carousel__slide--visible') expect(slides.length).toBe(1) }) it('Should display *one* next item', () => { const slides = wrapper.findAll('.carousel__slide--next') expect(slides.length).toBe(1) }) it('Should navigate to the focused slide', async () => { const slide = wrapper.find('.carousel__slide:nth-child(4)') await slide.trigger('focusin') expect(wrapper.props('modelValue')).toBe(3) }) it('Should reset both viewport scroll offsets when a slide receives focus', async () => { const focusWrapper = mount(Carousel, { slots: { default: () => [0, 1].map((i) => h(Slide, { key: i }, () => `slide ${i}`)), }, }) await nextTick() const viewport = focusWrapper.find('.carousel__viewport').element viewport.scrollLeft = 20 viewport.scrollTop = 30 await focusWrapper.findAll('.carousel__slide')[1].trigger('focusin') expect(viewport.scrollLeft).toBe(0) expect(viewport.scrollTop).toBe(0) focusWrapper.unmount() }) it('Should not navigate when focus is caused by a pointer (mousedown)', async () => { const viewport = wrapper.find('.carousel__viewport').element viewport.scrollLeft = 20 const slide = wrapper.find('.carousel__slide:nth-child(4)') await slide.trigger('mousedown') await slide.trigger('focusin') expect(wrapper.props('modelValue')).toBe(0) // The focus still scrolls the viewport, so the reset must run regardless expect(viewport.scrollLeft).toBe(0) }) it('Should not navigate when a pointer focuses an input inside a slide with mouseDrag enabled', async () => { const input = wrapper.find('.carousel__slide:nth-child(4) input') await input.trigger('mousedown') await input.trigger('focusin') expect(wrapper.props('modelValue')).toBe(0) }) it('Should navigate on keyboard focus after a pointer interaction has ended', async () => { vi.useFakeTimers() const slide = wrapper.find('.carousel__slide:nth-child(4)') await slide.trigger('mousedown') // No focusin: the mousedown target was not focusable. Fire the flag's 0 ms timer. vi.advanceTimersByTime(1) vi.useRealTimers() await slide.trigger('focusin') expect(wrapper.props('modelValue')).toBe(3) }) it('Should not navigate when a pointer focuses content inside a non-active slide with mouseDrag disabled', async () => { const onClick = vi.fn() const clickWrapper = mount(Carousel, { props: { itemsToShow: 3, mouseDrag: false, modelValue: 0 }, slots: { default: () => [0, 1, 2, 3, 4].map((i) => h(Slide, { key: i }, () => h('button', { onClick }, `slide ${i}`)) ), }, }) await nextTick() const button = clickWrapper.findAll('.carousel__slide')[2].find('button') await button.trigger('mousedown') await button.trigger('focusin') await button.trigger('mouseup') await button.trigger('click') expect(onClick).toHaveBeenCalledTimes(1) expect(clickWrapper.emitted('update:modelValue')).toBeUndefined() clickWrapper.unmount() }) it('Should not navigate when slide content stops mousedown propagation', async () => { const stopWrapper = mount(Carousel, { props: { itemsToShow: 3, mouseDrag: false, modelValue: 0 }, slots: { default: () => [0, 1, 2, 3, 4].map((i) => h(Slide, { key: i }, () => h( 'button', { onMousedown: (e: Event) => e.stopPropagation() }, `slide ${i}` ) ) ), }, }) await nextTick() const button = stopWrapper.findAll('.carousel__slide')[2].find('button') await button.trigger('mousedown') await button.trigger('focusin') expect(stopWrapper.emitted('update:modelValue')).toBeUndefined() stopWrapper.unmount() }) it('Should not navigate on focus when autoScrollOnFocus is false', async () => { const focusWrapper = mount(Carousel, { props: { itemsToShow: 1, autoScrollOnFocus: false, modelValue: 0 }, slots: { default: () => [0, 1, 2, 3, 4].map((i) => h(Slide, { key: i }, () => `slide ${i}`)), }, }) await nextTick() const viewport = focusWrapper.find('.carousel__viewport').element viewport.scrollLeft = 20 await focusWrapper.findAll('.carousel__slide')[3].trigger('focusin') expect(focusWrapper.emitted('update:modelValue')).toBeUndefined() // The focus still scrolls the viewport, so the reset must run regardless expect(viewport.scrollLeft).toBe(0) focusWrapper.unmount() }) it('Should honour autoScrollOnFocus from a breakpoint', async () => { const focusWrapper = mount(Carousel, { props: { itemsToShow: 1, modelValue: 0, breakpoints: { 0: { autoScrollOnFocus: false } }, }, slots: { default: () => [0, 1, 2, 3, 4].map((i) => h(Slide, { key: i }, () => `slide ${i}`)), }, }) await nextTick() await focusWrapper.findAll('.carousel__slide')[3].trigger('focusin') expect(focusWrapper.emitted('update:modelValue')).toBeUndefined() focusWrapper.unmount() }) it('Should navigate the carousel with arrow keys', async () => { vi.useFakeTimers() const track = wrapper.find('[tabindex="0"]') const triggerKeyEvent = async (key = 'ArrowRight', ctrl = false) => { document.dispatchEvent(new KeyboardEvent('keydown', { key: key, ctrlKey: ctrl })) // Fire the 200 ms throttle, then let the 300 ms slide finish so the // next key press is not ignored by the sliding guard vi.advanceTimersByTime(500) await nextTick() } await triggerKeyEvent() expect(wrapper.props('modelValue')).toBe(0) await track.trigger('focus') await triggerKeyEvent() expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent() expect(wrapper.props('modelValue')).toBe(2) await triggerKeyEvent('ArrowLeft') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowRight', true) expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowUp') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowDown') expect(wrapper.props('modelValue')).toBe(1) await wrapper.setProps({ dir: 'ttb', height: 200 }) await triggerKeyEvent('ArrowDown') expect(wrapper.props('modelValue')).toBe(2) await triggerKeyEvent('ArrowUp') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowRight') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowLeft') expect(wrapper.props('modelValue')).toBe(1) await wrapper.setProps({ dir: 'btt', height: 200 }) await triggerKeyEvent('ArrowDown') expect(wrapper.props('modelValue')).toBe(0) await triggerKeyEvent('ArrowUp') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowRight') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowLeft') expect(wrapper.props('modelValue')).toBe(1) await wrapper.setProps({ dir: 'rtl' }) await triggerKeyEvent('ArrowDown') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowUp') expect(wrapper.props('modelValue')).toBe(1) await triggerKeyEvent('ArrowRight') expect(wrapper.props('modelValue')).toBe(0) await triggerKeyEvent('ArrowLeft') expect(wrapper.props('modelValue')).toBe(1) await track.trigger('blur') await triggerKeyEvent('ArrowLeft') expect(wrapper.props('modelValue')).toBe(1) vi.useRealTimers() }) it('Should ignore an arrow key pressed while the carousel is still sliding', async () => { vi.useFakeTimers() await wrapper.setProps({ wrapAround: true }) const carousel = wrapper.findComponent(Carousel) await wrapper.find('[tabindex="0"]').trigger('focus') const pressArrowLeft = async () => { document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft' })) // Fire the 200 ms throttle only: the 300 ms transition is still running vi.advanceTimersByTime(200) await nextTick() } await pressArrowLeft() expect(carousel.emitted('slide-start')).toHaveLength(1) await pressArrowLeft() expect(carousel.emitted('slide-start')).toHaveLength(1) vi.runAllTimers() await nextTick() expect(wrapper.props('modelValue')).toBe(4) expect(carousel.emitted('slide-end')).toHaveLength(1) vi.useRealTimers() }) it('Should not navigate with arrow keys when keyboardNavigation is false', async () => { vi.useFakeTimers() const kbWrapper = mount(Carousel, { props: { keyboardNavigation: false, modelValue: 0, 'onUpdate:modelValue': (e: number) => kbWrapper.setProps({ modelValue: e }), }, slots: { default: () => [0, 1, 2].map((i) => h(Slide, { key: i }, () => `slide ${i}`)), }, }) await nextTick() const track = kbWrapper.find('[tabindex="0"]') const triggerKeyEvent = async (key = 'ArrowRight') => { document.dispatchEvent(new KeyboardEvent('keydown', { key })) vi.advanceTimersByTime(200) await nextTick() } await track.trigger('focus') await triggerKeyEvent() expect(kbWrapper.props('modelValue')).toBe(0) await kbWrapper.setProps({ keyboardNavigation: true }) await triggerKeyEvent() expect(kbWrapper.props('modelValue')).toBe(1) await kbWrapper.setProps({ keyboardNavigation: false }) await triggerKeyEvent() expect(kbWrapper.props('modelValue')).toBe(1) await track.trigger('blur') kbWrapper.unmount() vi.useRealTimers() }) it('Should honour keyboardNavigation from a matching breakpoint', async () => { vi.useFakeTimers() const bpWrapper = mount(Carousel, { props: { breakpoints: { 0: { keyboardNavigation: false } }, modelValue: 0, 'onUpdate:modelValue': (e: number) => bpWrapper.setProps({ modelValue: e }), }, slots: { default: () => [0, 1, 2].map((i) => h(Slide, { key: i }, () => `slide ${i}`)), }, }) await nextTick() const track = bpWrapper.find('[tabindex="0"]') await track.trigger('focus') document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight' })) vi.advanceTimersByTime(200) await nextTick() expect(bpWrapper.props('modelValue')).toBe(0) await track.trigger('blur') bpWrapper.unmount() vi.useRealTimers() }) it('Should default itemsToShow to 1 if less than 1', async () => { await wrapper.setProps({ itemsToShow: 0 }) const slides = wrapper.findAll('.carousel__slide--visible') expect(slides.length).toBe(1) }) it('Should default itemsToShow to slidesCount if greater than slidesCount', async () => { await wrapper.setProps({ itemsToShow: 10 }) const slides = wrapper.findAll('.carousel__slide') expect(slides.length).toBe(5) }) it('Should exclude invisible slides from tab navigation', async () => { const wrapper = await mount(App, { props: { slideNum: 5, itemsToShow: 1, modelValue: 0, }, }) const allSlides = wrapper.findAll('.carousel__slide') const visibleSlides = wrapper.findAll('.carousel__slide--visible') // With itemsToShow: 1, only 1 slide should be visible expect(visibleSlides.length).toBe(1) expect(allSlides.length).toBe(5) // Check that visible slide has no tabindex or has tabindex="0" const visibleSlide = visibleSlides[0].element as HTMLElement const visibleTabindex = visibleSlide.getAttribute('tabindex') expect(visibleTabindex === null || visibleTabindex === '0').toBe(true) // Check that non-visible slides have tabindex="-1" for (let i = 0; i < allSlides.length; i++) { const slide = allSlides[i].element as HTMLElement const hasVisibleClass = slide.classList.contains('carousel__slide--visible') if (!hasVisibleClass) { expect(slide.getAttribute('tabindex')).toBe('-1') } } }) it('Should exclude cloned slides from tab navigation', async () => { const wrapper = await mount(App, { props: { slideNum: 5, itemsToShow: 3, wrapAround: true, modelValue: 0, }, }) const clonedSlides = wrapper.findAll('.carousel__slide--clone') // With wrapAround, there should be cloned slides expect(clonedSlides.length).toBeGreaterThan(0) // Check that cloned slides have tabindex="-1" for (const clonedSlide of clonedSlides) { const slide = clonedSlide.element as HTMLElement expect(slide.getAttribute('tabindex')).toBe('-1') } }) }) describe('Slotted Carousel.ts', () => { let wrapper: ReturnType> beforeAll(async () => { wrapper = await mount(SlottedApp, { props: { slideNum: 3 } }) }) it('It renders *three* slides correctly', () => { const slides = wrapper.findAll('.carousel__slide') expect(slides.length).toBe(3) }) it('Should display *one* visible item', () => { const slides = wrapper.findAll('.carousel__slide--visible') expect(slides.length).toBe(1) }) it('Should display *one* next item', () => { const slides = wrapper.findAll('.carousel__slide--next') expect(slides.length).toBe(1) }) }) describe('Wrap around Carousel.ts', () => { let wrapper: ReturnType> beforeAll(async () => { wrapper = await mount(App, { props: { wrapAround: true, itemsToShow: 3, slideNum: 9, modelValue: 8 }, }) }) it('renders wrapAround correctly', () => { expect(wrapper.html()).toMatchSnapshot() }) }) describe('SSR Carousel', () => { const consoleMock = vi.spyOn(console, 'warn').mockImplementation(() => undefined) afterEach(() => { consoleMock.mockReset() }) const renderSSR = async >( component: T, props: P = {} as P ) => { const comp = { render() { return h('div', { id: 'app' }, h(component, props)) }, } // Simulate a SSR env where there is no window or document const windowBackup = window const documentBackup = document Object.defineProperty(global, 'window', { value: undefined, enumerable: true, writable: false, }) const app = createSSRApp(comp) const html = await renderToString(app) Object.defineProperty(global, 'window', { value: windowBackup, enumerable: true, writable: false, }) Object.defineProperty(global, 'document', { value: documentBackup, enumerable: true, writable: false, }) Object.defineProperty(global.window, 'innerWidth', { value: 700 }) document.body.innerHTML = html const wrapper = await mount(comp, { attachTo: '#app' }) return [html, wrapper] } it('renders server side properly', async () => { const [html, wrapper] = await renderSSR(App, { height: 200, wrapAround: true, modelValue: 1, itemsToShow: 2, }) expect(consoleMock).not.toHaveBeenCalled() expect(wrapper.html()).toMatchSnapshot() expect(html).toMatchSnapshot() }) it('renders slotted server side properly', async () => { const [html, wrapper] = await renderSSR(SlottedApp, { wrapAround: true, slideNum: 5, }) expect(consoleMock).not.toHaveBeenCalled() expect(wrapper.html()).toMatchSnapshot() expect(html).toMatchSnapshot() }) it("doesn't get hydration mismatch with breakpoints", async () => { const [html, wrapper] = await renderSSR(App, { wrapAround: true, breakpoints: { 600: { itemsToShow: 3, }, }, modelValue: 1, itemsToShow: 1, }) expect(consoleMock).not.toHaveBeenCalled() const slides = wrapper.findAll('.carousel__slide--visible') expect(slides.length).toBe(3) const el = document.createElement('div') el.innerHTML = html const slidesSSR = el.querySelectorAll('.carousel__slide--visible') expect(slidesSSR.length).toBe(1) }) }) describe('Carousel Clone Count Logic', () => { let wrapper: ReturnType> it('should not clone slides when wrapAround is disabled', async () => { wrapper = await mount(App, { props: { slideNum: 5, wrapAround: false, itemsToShow: 3, itemsToScroll: 1, modelValue: 0, }, }) const slides = wrapper.findAll('.carousel__slide') expect(slides.length).toBe(5) // Only original slides }) it('should calculate correct clone counts at start position', async () => { wrapper = await mount(App, { props: { slideNum: 5, wrapAround: true, itemsToShow: 3, itemsToScroll: 2, modelValue: 0, }, }) const slides = wrapper.findAll('.carousel__slide') // Original slides (5) + cloned slides before (4) + cloned slides after (0) // slidesToClone = Math.ceil(3 + (2 - 1)) = 4 // -> before: Math.max(0, 4-0) = 4 // -> after: Math.max(0, 4-(5-(0+1))) = 0 expect(slides.length).toBe(9) }) it('should adjust clone counts when scrolling to middle', async () => { wrapper = await mount(App, { props: { slideNum: 5, wrapAround: true, itemsToShow: 2, itemsToScroll: 1, modelValue: 2, }, }) const slides = wrapper.findAll('.carousel__slide') // Original slides (5) only expect(slides.length).toBe(5) }) it('should handle edge case at last slide', async () => { wrapper = await mount(App, { props: { slideNum: 5, wrapAround: true, itemsToShow: 2, itemsToScroll: 1, modelValue: 4, }, }) const slides = wrapper.findAll('.carousel__slide') // Original slides (5) + cloned slides after (2) expect(slides.length).toBe(7) }) it('should handle decimal itemsToShow with itemsToScroll', async () => { wrapper = await mount(App, { props: { slideNum: 5, wrapAround: true, itemsToShow: 2.5, itemsToScroll: 2, modelValue: 0, }, }) const slides = wrapper.findAll('.carousel__slide') // Original slides (5) + ceil(2.5 + (2 - 1)) = 4 expect(slides.length).toBe(9) }) it('should handle minimal setup with a single item', async () => { wrapper = await mount(App, { props: { slideNum: 1, wrapAround: true, itemsToShow: 1, itemsToScroll: 1, modelValue: 0, }, }) const slides = wrapper.findAll('.carousel__slide') // Original slides (1) + cloned slides before (1) + cloned slides after (1) expect(slides.length).toBe(3) }) }) describe('Carousel inside a scaled ancestor', () => { const VIEWPORT_SCREEN_WIDTH = 500 let scale = 0.5 // layout width is 1000 at this scale let scaleY = scale // kept separate so a test can pin a non-uniform scale const ScaledApp = defineComponent({ props: { dir: { type: String, default: 'ltr' }, height: { type: [String, Number], default: 'auto' }, // Extra props merged over the defaults below, for per-test configuration carouselProps: { type: Object, default: () => ({}) }, }, setup() { // The carousel's exposed API is only reachable through a template ref return { carousel: ref() } }, render() { return h('div', { class: 'scaled-wrapper' }, [ h( Carousel, { ref: 'carousel', itemsToShow: 1, snapAlign: 'start', modelValue: 0, dir: this.dir, height: this.height, ...this.carouselProps, }, { default: () => [1, 2, 3, 4, 5].map((n) => h(Slide, { key: n }, () => `${n}`)), } ), ]) }, }) let wrapper: ReturnType> const mountScaled = async ( props: { dir?: string height?: string carouselProps?: Record } = {}, // Only needed by tests that rely on events bubbling up to the document options: { attachTo?: Element } = {} ) => { wrapper = mount(ScaledApp, { props, ...options }) await nextTick() return wrapper } beforeEach(() => { scale = 0.5 scaleY = scale vi.useFakeTimers() vi.spyOn(window, 'getComputedStyle').mockImplementation( (el) => ({ transform: (el as Element).classList?.contains('scaled-wrapper') ? `matrix(${scale}, 0, 0, ${scaleY}, 0, 0)` : 'none', }) as CSSStyleDeclaration ) vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation( () => ({ width: VIEWPORT_SCREEN_WIDTH, height: VIEWPORT_SCREEN_WIDTH, top: 0, left: 0, right: 0, bottom: 0, x: 0, y: 0, toJSON: () => ({}), }) as DOMRect ) }) afterEach(() => { wrapper?.unmount() vi.restoreAllMocks() vi.useRealTimers() }) const dragMouse = async (from: [number, number], to: [number, number]) => { const track = wrapper.find('.carousel__track') await track.trigger('mousedown', { clientX: from[0], clientY: from[1], button: 0 }) document.dispatchEvent( new MouseEvent('mousemove', { clientX: to[0], clientY: to[1] }) ) vi.runAllTimers() // flush the throttled drag handler await nextTick() return track } const releaseMouse = async () => { document.dispatchEvent(new MouseEvent('mouseup')) await nextTick() } it('measures the slide size in layout pixels', async () => { await mountScaled() expect(wrapper.vm.carousel.data.slideSize).toBe(VIEWPORT_SCREEN_WIDTH / scale) }) it('moves the track by one layout-pixel slide per slide', async () => { await mountScaled() wrapper.vm.carousel.slideTo(1) await nextTick() expect(wrapper.find('.carousel__track').attributes('style')).toContain( 'translateX(-1000px)' ) }) it('converts mouse drag distance from screen pixels to layout pixels', async () => { await mountScaled() // 60 screen px at scale 0.5 is 120 layout px const track = await dragMouse([400, 0], [340, 0]) expect(track.attributes('style')).toContain('translateX(-120px)') await releaseMouse() // 120 / 1000 = 0.12 of a slide, above the 0.08 default threshold expect(wrapper.findComponent(Carousel).emitted('update:modelValue')?.[0]).toEqual([1]) }) it('resamples the scale on each drag', async () => { await mountScaled() await dragMouse([400, 0], [340, 0]) await releaseMouse() vi.runAllTimers() // finish the slide transition scale = 0.25 // the wrapper rescaled (e.g. window resize) between drags const track = await dragMouse([400, 0], [340, 0]) // 60 screen px at scale 0.25 is 240 layout px, on top of the 1000px scroll // The 1000px offset was measured at scale 0.5 and is not re-measured here: // vitest.setup.ts stubs ResizeObserver as a no-op, so no resize callback // fires (a real browser would re-measure and the offset would become 2000). expect(track.attributes('style')).toContain('translateX(-1240px)') }) it("matches 'carousel' breakpoints on the layout width", async () => { await mountScaled({ carouselProps: { breakpointMode: 'carousel', breakpoints: { 800: { itemsToShow: 2 } }, }, }) // root is 500 screen px at scale 0.5 = 1000 layout px, which is >= 800 expect(wrapper.findAll('.carousel__slide--visible').length).toBe(2) }) it('stops the ancestor animation loop when the animation is cancelled', async () => { await mountScaled({}, { attachTo: document.body }) const cancelSpy = vi.spyOn(window, 'cancelAnimationFrame') const ancestor = wrapper.element as HTMLElement // .scaled-wrapper contains the carousel root ancestor.dispatchEvent(new Event('animationstart', { bubbles: true })) ancestor.dispatchEvent(new Event('animationcancel', { bubbles: true })) expect(cancelSpy).toHaveBeenCalledTimes(1) }) it('uses the height multiplier for vertical drags', async () => { // Non-uniform on purpose: a uniform scale cannot catch an axis swap scaleY = 0.25 await mountScaled({ dir: 'ttb', height: '500px' }) // 60 screen px at scaleY 0.25 is 240 layout px (widthMultiplier is only 2) const track = await dragMouse([0, 400], [0, 340]) expect(track.attributes('style')).toContain('translateY(-240px)') }) }) describe('Drag release without a slide change', () => { const SLIDE_WIDTH = 1000 let wrapper: ReturnType> beforeEach(() => { vi.useFakeTimers() vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation( () => ({ width: SLIDE_WIDTH, height: SLIDE_WIDTH, top: 0, left: 0, right: 0, bottom: 0, x: 0, y: 0, toJSON: () => ({}), }) as DOMRect ) }) afterEach(() => { // Release any drag an assertion failure left open, or its document // listeners leak into the next test document.dispatchEvent(new MouseEvent('mouseup')) wrapper?.unmount() vi.restoreAllMocks() vi.useRealTimers() }) const mountCarousel = async (props: ComponentProps = {}) => { wrapper = mount(Carousel, { props: { itemsToShow: 1, modelValue: 0, ...props }, slots: { default: () => [1, 2, 3, 4, 5].map((n) => h(Slide, { key: n }, () => `${n}`)), }, }) await nextTick() } const pressMouse = async (clientX: number, clientY = 0) => { await wrapper .find('.carousel__track') .trigger('mousedown', { clientX, clientY, button: 0 }) } const moveMouse = async (clientX: number, clientY = 0) => { document.dispatchEvent(new MouseEvent('mousemove', { clientX, clientY })) vi.advanceTimersToNextFrame() // flush the rAF-throttled drag handler only await nextTick() } const releaseMouse = async () => { document.dispatchEvent(new MouseEvent('mouseup')) await nextTick() } it('animates the snap-back of a drag that stays under the threshold', async () => { await mountCarousel() await pressMouse(500) // 40px is 4% of the slide, under the 8% default threshold await moveMouse(460) expect(wrapper.find('.carousel__track').attributes('style')).toContain( 'translateX(-40px)' ) await releaseMouse() expect(wrapper.find('.carousel__track').attributes('style')).toContain( 'translateX(0px)' ) expect(wrapper.attributes('style')).toContain('--vc-transition-duration: 300ms') expect(wrapper.classes()).not.toContain('is-sliding') expect(wrapper.emitted('update:modelValue')).toBeUndefined() expect(wrapper.emitted('slide-start')).toBeUndefined() vi.advanceTimersByTime(300) await nextTick() expect(wrapper.attributes('style')).not.toContain('--vc-transition-duration') }) it('animates the snap-back of a drag clamped at the first slide', async () => { await mountCarousel() await pressMouse(500) // Dragging toward the previous slide at index 0 is clamped to index 0 await moveMouse(800) await releaseMouse() expect(wrapper.attributes('style')).toContain('--vc-transition-duration: 300ms') expect(wrapper.classes()).not.toContain('is-sliding') expect(wrapper.emitted('update:modelValue')).toBeUndefined() }) it('does not enter the sliding state on a click without movement', async () => { await mountCarousel() await pressMouse(500) await releaseMouse() expect(wrapper.attributes('style')).not.toContain('--vc-transition-duration') }) it('does not transition after movement off the carousel axis', async () => { await mountCarousel() // Vertical movement (e.g. a page scroll) over a horizontal carousel await pressMouse(500, 500) await moveMouse(500, 300) await releaseMouse() expect(wrapper.attributes('style')).not.toContain('--vc-transition-duration') }) it('animates the snap-back of a vertical drag on a vertical carousel', async () => { await mountCarousel({ dir: 'ttb', height: '500px' }) await pressMouse(0, 500) // 40px is 4% of the slide, under the 8% default threshold await moveMouse(0, 460) await releaseMouse() expect(wrapper.attributes('style')).toContain('--vc-transition-duration: 300ms') expect(wrapper.emitted('update:modelValue')).toBeUndefined() }) it('does not transition after a drag with the fade effect', async () => { await mountCarousel({ slideEffect: 'fade' }) await pressMouse(500) await moveMouse(460) await releaseMouse() expect(wrapper.attributes('style')).not.toContain('--vc-transition-duration') }) it('lets a new drag start during the snap-back without easing', async () => { await mountCarousel() await pressMouse(500) await moveMouse(460) await releaseMouse() expect(wrapper.attributes('style')).toContain('--vc-transition-duration: 300ms') await pressMouse(500) await nextTick() expect(wrapper.attributes('style')).not.toContain('--vc-transition-duration') }) it('keeps the transition when a slide starts during the snap-back', async () => { await mountCarousel() await pressMouse(500) await moveMouse(460) await releaseMouse() vi.advanceTimersByTime(200) wrapper.vm.slideTo(1) await nextTick() vi.advanceTimersByTime(150) // snap-back timer has expired, the slide has not await nextTick() expect(wrapper.attributes('style')).toContain('--vc-transition-duration: 300ms') expect(wrapper.classes()).toContain('is-sliding') }) }) describe('Slide content tab order', () => { const mountLinks = (props: Record = {}) => mount(Carousel, { props: { itemsToShow: 1, modelValue: 0, ...props }, slots: { default: () => [0, 1, 2, 3, 4].map((i) => h(Slide, { key: i }, () => h('a', { href: '#' }, `slide ${i}`)) ), }, }) it('Should exclude links inside non-visible slides from the tab order', async () => { const wrapper = mountLinks() await nextTick() const links = wrapper.findAll('.carousel__slide a') expect(links[0].attributes('tabindex')).toBeUndefined() for (const link of links.slice(1)) { expect(link.attributes('tabindex')).toBe('-1') } wrapper.unmount() }) it('Should restore the tab order of slide content when it becomes visible', async () => { const wrapper = mountLinks() await nextTick() await wrapper.setProps({ modelValue: 3 }) await nextTick() const links = wrapper.findAll('.carousel__slide a') expect(links[3].attributes('tabindex')).toBeUndefined() expect(links[0].attributes('tabindex')).toBe('-1') wrapper.unmount() }) it('Should keep an author-set tabindex when a slide becomes visible', async () => { const wrapper = mount(Carousel, { props: { itemsToShow: 1, modelValue: 0 }, slots: { default: () => [0, 1, 2].map((i) => h(Slide, { key: i }, () => h('div', { tabindex: '0' }, `slide ${i}`)) ), }, }) await nextTick() const divs = wrapper.findAll('.carousel__slide div') expect(divs[1].attributes('tabindex')).toBe('-1') await wrapper.setProps({ modelValue: 1 }) await nextTick() expect(divs[1].attributes('tabindex')).toBe('0') expect(divs[0].attributes('tabindex')).toBe('-1') wrapper.unmount() }) it('Should keep clone content out of the tab order even inside the visible range', async () => { const wrapper = mountLinks({ itemsToShow: 3, wrapAround: true, snapAlign: 'center' }) await nextTick() const cloneLinks = wrapper.findAll('.carousel__slide--clone a') expect(cloneLinks.length).toBeGreaterThan(0) for (const link of cloneLinks) { expect(link.attributes('tabindex')).toBe('-1') } expect( wrapper.find('.carousel__slide--active a').attributes('tabindex') ).toBeUndefined() wrapper.unmount() }) it('Should leave slot content alone when the carousel is disabled', async () => { const wrapper = mountLinks({ enabled: false }) await nextTick() for (const link of wrapper.findAll('a')) { expect(link.attributes('tabindex')).toBeUndefined() } wrapper.unmount() }) it.each(['center', 'end'] as const)( 'Should keep every on-screen slide tabbable at the clamped start (snapAlign: %s)', async (snapAlign) => { const wrapper = mount(Carousel, { props: { itemsToShow: 3, modelValue: 0, snapAlign }, slots: { default: () => [0, 1, 2, 3, 4, 5].map((i) => h(Slide, { key: i }, () => h('a', { href: '#' }, `slide ${i}`)) ), }, }) await nextTick() const links = wrapper.findAll('.carousel__slide a') expect(links).toHaveLength(6) for (const link of links.slice(0, 3)) { expect(link.attributes('tabindex')).toBeUndefined() } for (const link of links.slice(3)) { expect(link.attributes('tabindex')).toBe('-1') } wrapper.unmount() } ) it('Should keep every on-screen slide tabbable with a fractional itemsToShow', async () => { const wrapper = mount(Carousel, { props: { itemsToShow: 2.5, modelValue: 1, snapAlign: 'end' }, slots: { default: () => [0, 1, 2, 3, 4, 5].map((i) => h(Slide, { key: i }, () => h('a', { href: '#' }, `slide ${i}`)) ), }, }) await nextTick() const links = wrapper.findAll('.carousel__slide a') expect(links).toHaveLength(6) for (const link of links.slice(0, 3)) { expect(link.attributes('tabindex')).toBeUndefined() } for (const link of links.slice(3)) { expect(link.attributes('tabindex')).toBe('-1') } wrapper.unmount() }) it("Should keep an inner carousel's hidden and clone content out of the tab order", async () => { const wrapper = mount(Carousel, { props: { itemsToShow: 1, modelValue: 0 }, slots: { default: () => [ h(Slide, { key: 'outer-0' }, () => h(Carousel, { itemsToShow: 1, wrapAround: true, class: 'inner' }, () => [0, 1, 2].map((i) => h(Slide, { key: i }, () => h('a', { href: '#' }, `inner ${i}`)) ) ) ), h(Slide, { key: 'outer-1' }, () => 'outer 1'), ], }, }) await nextTick() // Classify the inner slides themselves: a descendant selector such as // '.carousel__slide--active a' would also match through the outer active slide const innerSlides = wrapper.find('.inner').findAll('.carousel__slide') const linkOf = (slide: (typeof innerSlides)[number]) => slide.find('a') const clones = innerSlides.filter((s) => s.classes('carousel__slide--clone')) const real = innerSlides.filter((s) => !s.classes('carousel__slide--clone')) const hidden = real.filter((s) => !s.classes('carousel__slide--visible')) const active = real.filter((s) => s.classes('carousel__slide--active')) expect(clones.length).toBeGreaterThan(0) for (const slide of clones) { expect(linkOf(slide).attributes('tabindex')).toBe('-1') } expect(hidden).toHaveLength(2) for (const slide of hidden) { expect(linkOf(slide).attributes('tabindex')).toBe('-1') } expect(active).toHaveLength(1) expect(linkOf(active[0]).attributes('tabindex')).toBeUndefined() wrapper.unmount() }) it('Should reset the viewport scroll on focus in a vertical carousel', async () => { const wrapper = mountLinks({ dir: 'ttb', height: 200 }) await nextTick() const viewport = wrapper.find('.carousel__viewport').element viewport.scrollTop = 20 await wrapper.findAll('.carousel__slide')[3].trigger('focusin') expect(viewport.scrollTop).toBe(0) wrapper.unmount() }) }) describe('itemsToScroll paging (#522)', () => { const mountCarousel = ( props: Record = {}, { slides = 9, paginateByItemsToShow = false } = {} ) => mount(Carousel, { props: { itemsToShow: 3, itemsToScroll: 3, snapAlign: 'center', wrapAround: false, ...props, }, slots: { default: () => Array.from({ length: slides }, (_, i) => h(Slide, { key: i }, () => `${i}`)), addons: () => [ h(Navigation), ...(paginateByItemsToShow ? [h(Pagination, { paginateByItemsToShow })] : []), ], }, }) const visible = (wrapper: ReturnType) => wrapper.findAll('.carousel__slide--visible').map((slide) => slide.text()) const step = async ( wrapper: ReturnType, direction: 'next' | 'prev' ) => { wrapper.vm[direction]() vi.advanceTimersByTime(300) await nextTick() } beforeEach(() => vi.useFakeTimers()) afterEach(() => vi.useRealTimers()) it('moves the visible slides by itemsToScroll from the clamped edges', async () => { const wrapper = mountCarousel() await nextTick() expect(visible(wrapper)).toEqual(['0', '1', '2']) await step(wrapper, 'next') expect(visible(wrapper)).toEqual(['3', '4', '5']) expect(wrapper.vm.currentSlide).toBe(4) await step(wrapper, 'next') expect(visible(wrapper)).toEqual(['6', '7', '8']) expect(wrapper.vm.currentSlide).toBe(8) expect(wrapper.find('.carousel__next').attributes('disabled')).toBeDefined() await step(wrapper, 'prev') expect(visible(wrapper)).toEqual(['3', '4', '5']) await step(wrapper, 'prev') expect(visible(wrapper)).toEqual(['0', '1', '2']) expect(wrapper.vm.currentSlide).toBe(0) expect(wrapper.find('.carousel__prev').attributes('disabled')).toBeDefined() }) it('keeps wrapAround stepping by itemsToScroll', async () => { const wrapper = mountCarousel({ wrapAround: true }) await nextTick() expect(visible(wrapper)).toEqual(['8', '0', '1']) await step(wrapper, 'next') expect(visible(wrapper)).toEqual(['2', '3', '4']) expect(wrapper.vm.currentSlide).toBe(3) await step(wrapper, 'next') expect(visible(wrapper)).toEqual(['5', '6', '7']) }) it('steps one slide at a time with itemsToScroll 1 at the clamped edges', async () => { const wrapper = mountCarousel({ itemsToShow: 5, itemsToScroll: 1 }) await nextTick() await step(wrapper, 'next') expect(wrapper.vm.currentSlide).toBe(1) expect(visible(wrapper)).toEqual(['0', '1', '2', '3', '4']) await step(wrapper, 'next') expect(wrapper.vm.currentSlide).toBe(2) expect(visible(wrapper)).toEqual(['0', '1', '2', '3', '4']) await step(wrapper, 'next') expect(wrapper.vm.currentSlide).toBe(3) expect(visible(wrapper)).toEqual(['1', '2', '3', '4', '5']) }) it('steps by index when itemsToShow exceeds the slide count', async () => { const wrapper = mountCarousel( { itemsToShow: 4, itemsToScroll: 2, snapAlign: 'start' }, { slides: 3 } ) await nextTick() await step(wrapper, 'next') expect(wrapper.vm.currentSlide).toBe(2) await step(wrapper, 'prev') expect(wrapper.vm.currentSlide).toBe(0) }) it('does not jump to the last slide when itemsToShow exceeds the slide count', async () => { const wrapper = mountCarousel( { itemsToShow: 6, itemsToScroll: 2, snapAlign: 'start' }, { slides: 5 } ) await nextTick() await step(wrapper, 'next') expect(wrapper.vm.currentSlide).toBe(2) await step(wrapper, 'next') expect(wrapper.vm.currentSlide).toBe(4) await step(wrapper, 'prev') expect(wrapper.vm.currentSlide).toBe(2) }) it.each(['start', 'center', 'end'])( 'highlights the last page after paging to the end (snapAlign %s)', async (snapAlign) => { const wrapper = mountCarousel({ snapAlign }, { paginateByItemsToShow: true }) await nextTick() await step(wrapper, 'next') await step(wrapper, 'next') const buttons = wrapper.findAll('.carousel__pagination-button') const active = buttons.filter((button) => button.classes('carousel__pagination-button--active') ) expect(buttons).toHaveLength(3) expect(active).toHaveLength(1) expect(buttons[2].classes()).toContain('carousel__pagination-button--active') } ) it.each(['start', 'center'])( 'lands the last page dot where next() lands (snapAlign %s)', async (snapAlign) => { const wrapper = mountCarousel({ snapAlign }, { paginateByItemsToShow: true }) await nextTick() const buttons = wrapper.findAll('.carousel__pagination-button') await buttons[2].trigger('click') vi.advanceTimersByTime(300) await nextTick() expect(visible(wrapper)).toEqual(['6', '7', '8']) expect(wrapper.vm.currentSlide).toBe(8) expect(wrapper.find('.carousel__next').attributes('disabled')).toBeDefined() expect(buttons[2].classes()).toContain('carousel__pagination-button--active') } ) }) describe('v-model with wrapAround (#519)', () => { const mountCarousel = (props: Record = {}, slides = 3) => mount(Carousel, { props: { itemsToShow: 1, snapAlign: 'start', wrapAround: true, ...props }, slots: { default: () => Array.from({ length: slides }, (_, i) => h(Slide, { key: i }, () => `${i}`)), }, }) const setModel = async (wrapper: ReturnType, value: number) => { await wrapper.setProps({ modelValue: value }) await nextTick() } const finish = async () => { vi.advanceTimersByTime(300) await nextTick() } beforeEach(() => vi.useFakeTimers()) afterEach(() => vi.useRealTimers()) it('loops forward through the clone from the last slide to the first', async () => { const wrapper = mountCarousel({ modelValue: 2 }) await nextTick() await setModel(wrapper, 0) expect(wrapper.vm.currentSlide).toBe(3) expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual([0]) await finish() expect(wrapper.vm.currentSlide).toBe(0) expect(wrapper.emitted('loop')?.at(-1)).toEqual([ { currentSlideIndex: 0, slidingToIndex: 3 }, ]) }) it('loops backward through the clone from the first slide to the last', async () => { const wrapper = mountCarousel({ modelValue: 0 }) await nextTick() await setModel(wrapper, 2) expect(wrapper.vm.currentSlide).toBe(-1) await finish() expect(wrapper.vm.currentSlide).toBe(2) expect(wrapper.emitted('loop')).toHaveLength(1) }) it('round-trips the canonical index through the parent while looping', async () => { const wrapper = mountCarousel({ modelValue: 0, 'onUpdate:modelValue': (e: number) => wrapper.setProps({ modelValue: e }), }) await nextTick() await setModel(wrapper, -1) expect(wrapper.vm.currentSlide).toBe(-1) expect(wrapper.props('modelValue')).toBe(2) await finish() expect(wrapper.vm.currentSlide).toBe(2) expect(wrapper.emitted('loop')).toHaveLength(1) expect(wrapper.emitted('slide-start')).toHaveLength(1) }) it('does not wrap on a tie', async () => { const wrapper = mountCarousel({ modelValue: 0 }, 4) await nextTick() await setModel(wrapper, 2) expect(wrapper.vm.currentSlide).toBe(2) await finish() expect(wrapper.emitted('loop')).toBeUndefined() }) it('normalizes an out-of-range value, then takes the shortest path', async () => { const wrapper = mountCarousel({ modelValue: 2 }) await nextTick() await setModel(wrapper, 3) expect(wrapper.vm.currentSlide).toBe(3) await finish() expect(wrapper.vm.currentSlide).toBe(0) }) it('takes the shortest path to an out-of-range model value', async () => { const wrapper = mountCarousel({ modelValue: 0 }) await nextTick() await setModel(wrapper, 7) expect(wrapper.vm.currentSlide).toBe(1) await finish() expect(wrapper.vm.currentSlide).toBe(1) expect(wrapper.emitted('loop')).toBeUndefined() }) it('loops through the clone with several items shown', async () => { const wrapper = mountCarousel( { modelValue: 4, itemsToShow: 3, snapAlign: 'center' }, 5 ) await nextTick() await setModel(wrapper, 0) expect(wrapper.vm.currentSlide).toBe(5) await finish() expect(wrapper.vm.currentSlide).toBe(0) expect(wrapper.emitted('loop')).toHaveLength(1) }) it('slides directly without wrapAround', async () => { const wrapper = mountCarousel({ modelValue: 2, wrapAround: false }) await nextTick() await setModel(wrapper, 0) expect(wrapper.vm.currentSlide).toBe(0) await finish() expect(wrapper.emitted('loop')).toBeUndefined() }) it('slides directly in auto width mode', async () => { const wrapper = mountCarousel({ modelValue: 2, itemsToShow: 'auto' }) await nextTick() await setModel(wrapper, 0) expect(wrapper.vm.currentSlide).toBe(0) await finish() expect(wrapper.emitted('loop')).toBeUndefined() }) }) describe('Drag on a carousel with no measurable size (#518)', () => { let wrapper: ReturnType> beforeEach(() => { vi.useFakeTimers() }) afterEach(() => { wrapper?.unmount() vi.useRealTimers() }) it('ignores a vertical wrapAround drag instead of creating infinite clones', async () => { // jsdom lays nothing out: every rect is 0, so slideSize and gap are both 0 wrapper = mount(App, { props: { dir: 'ttb', height: 200, // jsdom still measures 0; only satisfies the ttb/height validator wrapAround: true, slideNum: 5, modelValue: 0, 'onUpdate:modelValue': (e: number) => wrapper.setProps({ modelValue: e }), }, }) await nextTick() const clonesBefore = wrapper.findAll('.carousel__slide--clone').length expect(clonesBefore).toBeGreaterThan(0) const track = wrapper.find('.carousel__track') await track.trigger('mousedown', { clientX: 0, clientY: 200, button: 0 }) document.dispatchEvent(new MouseEvent('mousemove', { clientX: 0, clientY: 100 })) vi.runAllTimers() // flush the throttled drag handler await nextTick() expect(wrapper.findComponent(Carousel).emitted('drag')).toHaveLength(1) expect(wrapper.findAll('.carousel__slide--clone').length).toBe(clonesBefore) document.dispatchEvent(new MouseEvent('mouseup')) await nextTick() vi.runAllTimers() // any slide transition await nextTick() expect(wrapper.props('modelValue')).toBe(0) expect(wrapper.findAll('.carousel__slide--clone').length).toBe(clonesBefore) }) })