forked from nathydre21/nepa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarousel.test.tsx
More file actions
83 lines (62 loc) · 2.73 KB
/
Copy pathCarousel.test.tsx
File metadata and controls
83 lines (62 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React from 'react';
import { render, screen, fireEvent, act } from '@testing-library/react';
import '@testing-library/jest-dom';
import { axe } from 'jest-axe';
import { Carousel } from './Carousel';
describe('Carousel Component', () => {
const items = [
{ id: 1, content: <div data-testid="slide-1">Slide 1</div> },
{ id: 2, content: <div data-testid="slide-2">Slide 2</div> },
{ id: 3, content: <div data-testid="slide-3">Slide 3</div> },
];
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it('renders correctly with given items', () => {
render(<Carousel items={items} />);
expect(screen.getByTestId('slide-1')).toBeInTheDocument();
expect(screen.getByTestId('slide-2')).toBeInTheDocument();
expect(screen.getByTestId('slide-3')).toBeInTheDocument();
});
it('navigates to next and previous slides when arrows are clicked', () => {
render(<Carousel items={items} loop={false} />);
const nextBtn = screen.getByLabelText('Next slide');
const prevBtn = screen.getByLabelText('Previous slide');
expect(screen.getByLabelText('Slide 1 of 3')).toHaveAttribute('aria-hidden', 'false');
fireEvent.click(nextBtn);
expect(screen.getByLabelText('Slide 2 of 3')).toHaveAttribute('aria-hidden', 'false');
fireEvent.click(prevBtn);
expect(screen.getByLabelText('Slide 1 of 3')).toHaveAttribute('aria-hidden', 'false');
});
it('navigates to specific slide when dot is clicked', () => {
render(<Carousel items={items} />);
const dot3 = screen.getByLabelText('Go to slide 3');
fireEvent.click(dot3);
expect(screen.getByLabelText('Slide 3 of 3')).toHaveAttribute('aria-hidden', 'false');
});
it('auto-plays when autoPlay is true', () => {
render(<Carousel items={items} autoPlay={true} interval={3000} />);
act(() => {
jest.advanceTimersByTime(3000);
});
expect(screen.getByLabelText('Slide 2 of 3')).toHaveAttribute('aria-hidden', 'false');
});
it('supports keyboard navigation', () => {
render(<Carousel items={items} />);
const carouselRegion = screen.getByRole('region');
carouselRegion.focus();
fireEvent.keyDown(carouselRegion, { key: 'ArrowRight' });
expect(screen.getByLabelText('Slide 2 of 3')).toHaveAttribute('aria-hidden', 'false');
fireEvent.keyDown(carouselRegion, { key: 'ArrowLeft' });
expect(screen.getByLabelText('Slide 1 of 3')).toHaveAttribute('aria-hidden', 'false');
});
it('passes accessibility (axe) checks', async () => {
jest.useRealTimers();
const { container } = render(<Carousel items={items} />);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
});