Carousel that keeps only one or two images in the DOM
A hiking app shows trail photos in a carousel on low-end phones. Rendering every photo and sliding a long strip around costs memory and decoding time, so the team wants a lean version: at rest only the current photo is in the DOM, and during a slide only the outgoing and incoming photos are. Build it, with Previous/Next buttons, one dot per photo, and arrow-key support.
The starter code includes the photos and the slide duration:
const TRANSITION_MS = 400;
const PHOTOS = [
{ src: 'https://picsum.photos/id/1018/640/400', alt: 'Misty pine ridge at dawn' },
{ src: 'https://picsum.photos/id/1015/640/400', alt: 'River bend below granite cliffs' },
{ src: 'https://picsum.photos/id/1016/640/400', alt: 'Red canyon walls at noon' },
{ src: 'https://picsum.photos/id/1039/640/400', alt: 'Waterfall in a fern gully' },
{ src: 'https://picsum.photos/id/1043/640/400', alt: 'Autumn forest trail' },
{ src: 'https://picsum.photos/id/1036/640/400', alt: 'Snowfield under a pale sky' },
];
<img>: the current photo, with its src and alt. The first photo is shown first.aria-label). They wrap: Previous on the first photo goes to the last, Next on the last goes to the first.aria-label Show photo N (N starts at 1). The dot of the shown photo has aria-current="true"; the others don’t. Clicking a dot goes straight to that photo. Clicking the dot of the photo already shown does nothing.Photo N of 6.TRANSITION_MS milliseconds.
<img>s: the outgoing photo and the incoming one. Never mount the photos in between, even when a dot jumps from photo 1 to photo 5.setTimeout(..., TRANSITION_MS), then remove the outgoing <img>. Don’t rely only on transitionend/animationend: they never fire when the animation doesn’t run (a background tab, prefers-reduced-motion, or display: none), which would leave two images mounted forever.Start 1 <img> (Misty pine ridge at dawn), Photo 1 of 6
Click Next 2 <img>s: Misty pine ridge + River bend, Photo 2 of 6
...400ms later 1 <img> (River bend below granite cliffs)
On photo 1, click "Show photo 5"
2 <img>s: photo 1 and photo 5 (never photos 2, 3 or 4)
On photo 1, click Previous slides from the left to photo 6, Photo 6 of 6
Click Next, then Next again 100ms later
the second click is ignored: the carousel ends on photo 2
<img> a stable identity (React key = src) so the incoming image is reused, not re-created and re-decoded, when the transition ends.@keyframes) avoids the “mount, wait a frame, then add a class” dance that CSS transitions need.<img> elements: the dots are plain buttons.The tests click your buttons, dispatch keydown events, and count and read the <img> elements in your carousel. You don’t need test ids, specific class names, or an exact HTML structure. The test frame is hidden, so real timers and animations don’t run there: each test replaces setTimeout, clearTimeout, requestAnimationFrame and cancelAnimationFrame with a fake clock and moves it forward by hand. Call those globals when the transition starts (not at load time). The tests check that:
<img> in your component is counted, hidden or not. There is 1 at rest and 2 during a transition, and their alt texts are the photos described above.aria-label; dots by aria-label="Show photo N", with aria-current="true" on the shown photo.Photo N of 6 is shown. Arrow keys are dispatched as keydown events (key: 'ArrowRight' / 'ArrowLeft') on a button inside the carousel.Capitalization and extra spacing don’t matter.