|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import Marquee from '../index'; |
| 4 | + |
| 5 | +describe('<Marquee />', () => { |
| 6 | + it('should match the snapshot', () => { |
| 7 | + const { asFragment } = render( |
| 8 | + <Marquee> |
| 9 | + <div>Item 1</div> |
| 10 | + <div>Item 2</div> |
| 11 | + <div>Item 3</div> |
| 12 | + </Marquee> |
| 13 | + ); |
| 14 | + expect(asFragment()).toMatchSnapshot(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should render children duplicated for seamless loop', () => { |
| 18 | + const { getAllByText } = render( |
| 19 | + <Marquee> |
| 20 | + <div>Item</div> |
| 21 | + </Marquee> |
| 22 | + ); |
| 23 | + expect(getAllByText('Item')).toHaveLength(2); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should apply reverse class to track when direction is right', () => { |
| 27 | + const { container } = render( |
| 28 | + <Marquee direction="right"> |
| 29 | + <div>Item</div> |
| 30 | + </Marquee> |
| 31 | + ); |
| 32 | + const track = container.querySelector('.ty-marquee__track'); |
| 33 | + expect(track).toHaveClass('ty-marquee__track_reverse'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should apply pause-on-hover class to track by default', () => { |
| 37 | + const { container } = render( |
| 38 | + <Marquee> |
| 39 | + <div>Item</div> |
| 40 | + </Marquee> |
| 41 | + ); |
| 42 | + const track = container.querySelector('.ty-marquee__track'); |
| 43 | + expect(track).toHaveClass('ty-marquee__track_pause-on-hover'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should not apply pause-on-hover class when disabled', () => { |
| 47 | + const { container } = render( |
| 48 | + <Marquee pauseOnHover={false}> |
| 49 | + <div>Item</div> |
| 50 | + </Marquee> |
| 51 | + ); |
| 52 | + const track = container.querySelector('.ty-marquee__track'); |
| 53 | + expect(track).not.toHaveClass('ty-marquee__track_pause-on-hover'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should apply fade class to wrapper when fade is true', () => { |
| 57 | + const { container } = render( |
| 58 | + <Marquee fade> |
| 59 | + <div>Item</div> |
| 60 | + </Marquee> |
| 61 | + ); |
| 62 | + expect(container.firstChild).toHaveClass('ty-marquee_fade'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should have overflow hidden on wrapper', () => { |
| 66 | + const { container } = render( |
| 67 | + <Marquee> |
| 68 | + <div>Item</div> |
| 69 | + </Marquee> |
| 70 | + ); |
| 71 | + expect(container.firstChild).toHaveClass('ty-marquee'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should set duration and gap as CSS variables on track', () => { |
| 75 | + const { container } = render( |
| 76 | + <Marquee duration={30} gap={24}> |
| 77 | + <div>Item</div> |
| 78 | + </Marquee> |
| 79 | + ); |
| 80 | + const track = container.querySelector('.ty-marquee__track') as HTMLElement; |
| 81 | + expect(track.style.getPropertyValue('--ty-marquee-duration')).toBe('30s'); |
| 82 | + expect(track.style.getPropertyValue('--ty-marquee-gap')).toBe('24px'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should forward ref to wrapper', () => { |
| 86 | + const ref = React.createRef<HTMLDivElement>(); |
| 87 | + render( |
| 88 | + <Marquee ref={ref}> |
| 89 | + <div>Item</div> |
| 90 | + </Marquee> |
| 91 | + ); |
| 92 | + expect(ref.current).toBeInstanceOf(HTMLDivElement); |
| 93 | + expect(ref.current).toHaveClass('ty-marquee'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should pass through custom className', () => { |
| 97 | + const { container } = render( |
| 98 | + <Marquee className="custom"> |
| 99 | + <div>Item</div> |
| 100 | + </Marquee> |
| 101 | + ); |
| 102 | + expect(container.firstChild).toHaveClass('ty-marquee'); |
| 103 | + expect(container.firstChild).toHaveClass('custom'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should not duplicate children when infinite is false', () => { |
| 107 | + const { getAllByText } = render( |
| 108 | + <Marquee infinite={false}> |
| 109 | + <div>Item</div> |
| 110 | + </Marquee> |
| 111 | + ); |
| 112 | + expect(getAllByText('Item')).toHaveLength(1); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should apply once class to track when infinite is false', () => { |
| 116 | + const { container } = render( |
| 117 | + <Marquee infinite={false}> |
| 118 | + <div>Item</div> |
| 119 | + </Marquee> |
| 120 | + ); |
| 121 | + const track = container.querySelector('.ty-marquee__track'); |
| 122 | + expect(track).toHaveClass('ty-marquee__track_once'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should not apply once class by default', () => { |
| 126 | + const { container } = render( |
| 127 | + <Marquee> |
| 128 | + <div>Item</div> |
| 129 | + </Marquee> |
| 130 | + ); |
| 131 | + const track = container.querySelector('.ty-marquee__track'); |
| 132 | + expect(track).not.toHaveClass('ty-marquee__track_once'); |
| 133 | + }); |
| 134 | +}); |
0 commit comments