|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | +import { fireEvent, render, waitFor, afterEach } from '@test/utils'; |
| 3 | +import React from 'react'; |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | +import Image from '../Image'; |
| 6 | + |
| 7 | +// 在测试文件中定义局部的 IntersectionObserver mock |
| 8 | +const mockIntersectionObserver = () => { |
| 9 | + const observe = vi.fn(); |
| 10 | + const unobserve = vi.fn(); |
| 11 | + const disconnect = vi.fn(); |
| 12 | + |
| 13 | + // 使用 vi.fn() 模拟 IntersectionObserver |
| 14 | + const MockIntersectionObserver = vi.fn(() => ({ |
| 15 | + observe, |
| 16 | + unobserve, |
| 17 | + disconnect, |
| 18 | + })); |
| 19 | + |
| 20 | + // 仅在当前测试文件中替换 IntersectionObserver |
| 21 | + vi.stubGlobal('IntersectionObserver', MockIntersectionObserver); |
| 22 | + |
| 23 | + return { |
| 24 | + observe, |
| 25 | + unobserve, |
| 26 | + disconnect, |
| 27 | + MockIntersectionObserver, |
| 28 | + }; |
| 29 | +}; |
| 30 | + |
| 31 | +describe('Image', () => { |
| 32 | + describe(': props', () => { |
| 33 | + afterEach(() => { |
| 34 | + vi.unstubAllGlobals(); |
| 35 | + }); |
| 36 | + |
| 37 | + it(': src', () => { |
| 38 | + const { getByRole } = render(<Image src="test.png" />); |
| 39 | + expect(getByRole('img')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it(': alt', () => { |
| 43 | + const { getByAltText } = render(<Image alt="测试图片" src="test.png" />); |
| 44 | + expect(getByAltText('测试图片')).toHaveAttribute('src', 'test.png'); |
| 45 | + }); |
| 46 | + |
| 47 | + it(': shape', () => { |
| 48 | + const shapes = ['circle', 'round', 'square'] as const; |
| 49 | + shapes.forEach((shape) => { |
| 50 | + const { container } = render(<Image shape={shape} src="test.png" />); |
| 51 | + expect(container.firstChild).toHaveClass(`t-image--${shape}`); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it(': fit', () => { |
| 56 | + const { container } = render(<Image fit="cover" src="test.png" />); |
| 57 | + const img = container.querySelector('.t-image__img'); |
| 58 | + expect(img).toHaveClass('t-image--fit-cover'); |
| 59 | + }); |
| 60 | + |
| 61 | + it(': position', () => { |
| 62 | + const { getByRole } = render(<Image position="top center" src="test.png" />); |
| 63 | + expect(getByRole('img')).toHaveClass('t-image--position-top center'); |
| 64 | + }); |
| 65 | + |
| 66 | + it(': referrerpolicy', () => { |
| 67 | + const { getByRole } = render(<Image referrerpolicy="no-referrer" src="test.png" />); |
| 68 | + expect(getByRole('img')).toHaveAttribute('referrerPolicy', 'no-referrer'); |
| 69 | + }); |
| 70 | + |
| 71 | + // 加载状态测试 |
| 72 | + it(': loading(custom)', () => { |
| 73 | + const { container } = render(<Image loading={<span>加载中...</span>} />); |
| 74 | + expect(container.textContent).toContain('加载中'); |
| 75 | + }); |
| 76 | + |
| 77 | + it(': loading(default)', () => { |
| 78 | + const { container } = render(<Image />); |
| 79 | + expect(container.querySelector('.t-loading')).toBeInTheDocument(); |
| 80 | + }); |
| 81 | + |
| 82 | + it(': error', async () => { |
| 83 | + const { getByRole, getByText } = render(<Image src="invalid.png" error={<span>加载失败</span>} />); |
| 84 | + const img = getByRole('img'); |
| 85 | + fireEvent.error(img); |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + expect(getByText('加载失败')).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it(': srcset', () => { |
| 93 | + const srcset = { |
| 94 | + 'image/avif': 'test.avif', |
| 95 | + 'image/webp': 'test.webp', |
| 96 | + }; |
| 97 | + const { getByRole, container } = render(<Image src="test.jpg" srcset={srcset} />); |
| 98 | + |
| 99 | + const avifSource = container.querySelector('source[type="image/avif"]'); |
| 100 | + const webpSource = container.querySelector('source[type="image/webp"]'); |
| 101 | + expect(avifSource).toHaveAttribute('srcset', 'test.avif'); |
| 102 | + expect(webpSource).toHaveAttribute('srcset', 'test.webp'); |
| 103 | + expect(getByRole('img')).toHaveAttribute('src', 'test.jpg'); |
| 104 | + }); |
| 105 | + |
| 106 | + it(': lazy', () => { |
| 107 | + const { observe, MockIntersectionObserver } = mockIntersectionObserver(); |
| 108 | + |
| 109 | + const { container } = render(<Image src="test.png" lazy />); |
| 110 | + |
| 111 | + // 验证 IntersectionObserver 被正确初始化并使用 |
| 112 | + expect(MockIntersectionObserver).toHaveBeenCalled(); |
| 113 | + expect(observe).toHaveBeenCalledWith(container.firstElementChild); |
| 114 | + }); |
| 115 | + |
| 116 | + it(': onLoad', async () => { |
| 117 | + const onLoad = vi.fn(); |
| 118 | + const { getByRole } = render(<Image src="test.png" onLoad={onLoad} />); |
| 119 | + const img = getByRole('img'); |
| 120 | + fireEvent.load(img); |
| 121 | + |
| 122 | + await waitFor(() => { |
| 123 | + expect(onLoad).toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it(': onError', async () => { |
| 128 | + const onError = vi.fn(); |
| 129 | + const { getByRole } = render(<Image src="invalid.png" fallback="fallback.png" onError={onError} />); |
| 130 | + const img = getByRole('img'); |
| 131 | + fireEvent.error(img); |
| 132 | + |
| 133 | + await waitFor(() => { |
| 134 | + expect(onError).toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments