|
1 |
| -import { fireEvent, render } from '@testing-library/react'; |
| 1 | +import { act, fireEvent, render } from '@testing-library/react'; |
2 | 2 | import React from 'react';
|
3 |
| -import Image from '../src'; |
| 3 | +import Image, { CoverConfig } from '../src'; |
4 | 4 |
|
5 | 5 | describe('Basic', () => {
|
| 6 | + beforeEach(() => { |
| 7 | + jest.useFakeTimers(); |
| 8 | + }); |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + jest.useRealTimers(); |
| 12 | + }); |
| 13 | + |
6 | 14 | it('snapshot', () => {
|
7 | 15 | const { asFragment } = render(
|
8 | 16 | <Image
|
@@ -101,4 +109,52 @@ describe('Basic', () => {
|
101 | 109 | const operationsElement = baseElement.querySelector('.rc-image-preview');
|
102 | 110 | expect(operationsElement).toHaveStyle({ zIndex: 9999 });
|
103 | 111 | });
|
| 112 | + it('cover placement should work', () => { |
| 113 | + const App = () => { |
| 114 | + const [placement, setPlacement] = React.useState<'top' | 'bottom' | 'center'>('center'); |
| 115 | + return ( |
| 116 | + <> |
| 117 | + <select |
| 118 | + id="placement" |
| 119 | + onChange={e => setPlacement(e.target.value as CoverConfig['placement'])} |
| 120 | + value={placement} |
| 121 | + > |
| 122 | + <option value="top">top</option> |
| 123 | + <option value="bottom">bottom</option> |
| 124 | + <option value="center">center</option> |
| 125 | + </select> |
| 126 | + <Image |
| 127 | + src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" |
| 128 | + preview={{ |
| 129 | + cover: { |
| 130 | + coverNode: 'Click to Preview', |
| 131 | + placement: placement as CoverConfig['placement'], |
| 132 | + }, |
| 133 | + }} |
| 134 | + /> |
| 135 | + </> |
| 136 | + ); |
| 137 | + }; |
| 138 | + const { container } = render(<App />); |
| 139 | + const coverElement = container.querySelector('.rc-image-cover'); |
| 140 | + expect(coverElement).toHaveClass('rc-image-cover-center'); |
| 141 | + |
| 142 | + fireEvent.change(container.querySelector('#placement'), { |
| 143 | + target: { value: 'top' }, |
| 144 | + }); |
| 145 | + // Wait for the state update to take effect |
| 146 | + act(() => { |
| 147 | + jest.runAllTimers(); |
| 148 | + }); |
| 149 | + expect(coverElement).toHaveClass('rc-image-cover-top'); |
| 150 | + |
| 151 | + fireEvent.change(container.querySelector('#placement'), { |
| 152 | + target: { value: 'bottom' }, |
| 153 | + }); |
| 154 | + // Wait for the state update to take effect |
| 155 | + act(() => { |
| 156 | + jest.runAllTimers(); |
| 157 | + }); |
| 158 | + expect(coverElement).toHaveClass('rc-image-cover-bottom'); |
| 159 | + }); |
104 | 160 | });
|
0 commit comments