Files
gw-svelte/src/lib/components/TestimonialsSection.test.ts
T

131 lines
4.0 KiB
TypeScript
Raw Normal View History

2026-05-02 19:44:45 +12:00
import { fireEvent, render, screen } from '@testing-library/svelte';
2026-05-02 08:26:18 +12:00
import { afterEach, describe, expect, it, vi } from 'vitest';
import TestimonialsSection from './TestimonialsSection.svelte';
import { homepageContent } from '$lib/content/homepage';
2026-05-02 19:44:45 +12:00
import type { TestimonialContent } from '$lib/types';
const expectedMappedSlides = [
{ reviewer: 'Kate' },
{ reviewer: 'Estelle' },
{ reviewer: 'Ross' },
{ reviewer: 'Nina' }
2026-05-02 19:44:45 +12:00
];
function getActiveSlide(container: HTMLElement) {
return container.querySelector('.testimonial-slide-active') as HTMLElement;
}
function getActiveReviewer(container: HTMLElement) {
return getActiveSlide(container).querySelector('.testimonial-author-name')?.textContent;
}
function getActiveImage(container: HTMLElement) {
return getActiveSlide(container).querySelector('img') as HTMLImageElement;
}
2026-05-02 08:26:18 +12:00
function getNextButton(container: HTMLElement) {
return container.querySelector('.testimonial-arrow-right') as HTMLButtonElement;
}
function getPreviousButton(container: HTMLElement) {
return container.querySelector('.testimonial-arrow-left') as HTMLButtonElement;
}
2026-05-02 08:26:18 +12:00
describe('TestimonialsSection', () => {
afterEach(() => {
vi.useRealTimers();
});
2026-05-02 19:44:45 +12:00
it('maps all known testimonial images to the local PNG assets', async () => {
2026-05-02 08:26:18 +12:00
const { container } = render(TestimonialsSection, {
testimonials: homepageContent.testimonials
});
const nextButton = getNextButton(container);
2026-05-02 19:44:45 +12:00
for (const [index, slide] of expectedMappedSlides.entries()) {
expect(getActiveReviewer(container)).toBe(slide.reviewer);
expect(getActiveImage(container)).toBeTruthy();
2026-05-02 08:26:18 +12:00
2026-05-02 19:44:45 +12:00
if (index < expectedMappedSlides.length - 1) {
await fireEvent.click(nextButton);
}
}
2026-05-02 08:26:18 +12:00
});
it('moves to the next testimonial on arrow click and auto-rotation', async () => {
vi.useFakeTimers();
const { container } = render(TestimonialsSection, {
testimonials: homepageContent.testimonials
});
const nextButton = getNextButton(container);
2026-05-02 08:26:18 +12:00
2026-05-02 19:44:45 +12:00
expect(getActiveReviewer(container)).toBe('Kate');
2026-05-02 08:26:18 +12:00
await fireEvent.click(nextButton);
2026-05-02 19:44:45 +12:00
expect(getActiveReviewer(container)).toBe('Estelle');
2026-05-02 08:26:18 +12:00
2026-05-03 11:49:59 +12:00
await vi.advanceTimersByTimeAsync(9000);
2026-05-02 19:44:45 +12:00
expect(getActiveReviewer(container)).toBe('Ross');
});
it('wraps to the last testimonial when navigating backwards from the first slide', async () => {
const { container } = render(TestimonialsSection, {
testimonials: homepageContent.testimonials
});
const previousButton = getPreviousButton(container);
2026-05-02 19:44:45 +12:00
expect(getActiveReviewer(container)).toBe('Kate');
await fireEvent.click(previousButton);
expect(getActiveReviewer(container)).toBe('Nina');
expect(getActiveImage(container)).toBeTruthy();
2026-05-02 19:44:45 +12:00
});
it('keeps custom testimonial images and filters out testimonials with no image', async () => {
const customTestimonials: TestimonialContent[] = [
...homepageContent.testimonials,
{
reviewer: 'Casey',
detail: "Poppy's mum",
quote: 'Thoughtful updates and a very happy dog after every walk.',
imageUrl: '/images/custom-casey-review.png'
},
{
reviewer: 'Jordan',
detail: "Scout's dad",
quote: 'Should be hidden because there is no image.'
}
];
const { container } = render(TestimonialsSection, {
testimonials: customTestimonials
});
const nextButton = getNextButton(container);
2026-05-02 19:44:45 +12:00
expect(container.querySelectorAll('.testimonial-slide')).toHaveLength(5);
for (let step = 0; step < 4; step += 1) {
await fireEvent.click(nextButton);
}
expect(getActiveReviewer(container)).toBe('Casey');
expect(getActiveImage(container)).toBeTruthy();
2026-05-02 19:44:45 +12:00
expect(screen.queryByText('Jordan')).not.toBeInTheDocument();
2026-05-02 08:26:18 +12:00
});
it('can start on a different testimonial for a different page seed', () => {
const { container } = render(TestimonialsSection, {
testimonials: homepageContent.testimonials,
seedKey: '/dog-walking'
});
expect(getActiveReviewer(container)).not.toBe('Kate');
});
2026-05-02 08:26:18 +12:00
});