|
1 | 1 | import * as React from 'react'
|
| 2 | +import { useState } from 'react' |
2 | 3 | import { render, fireEvent, waitFor, act } from '@testing-library/react'
|
3 | 4 | import '@testing-library/jest-dom'
|
4 | 5 | import { Fabulous } from '@nutui/icons-react'
|
@@ -260,3 +261,94 @@ test('vertical container height calculation with children', async () => {
|
260 | 261 | { timeout: 3000 }
|
261 | 262 | )
|
262 | 263 | })
|
| 264 | + |
| 265 | +test('dynamic children update test', async () => { |
| 266 | + let setList: any |
| 267 | + const height = 40 |
| 268 | + |
| 269 | + const TestComponent = () => { |
| 270 | + const [list, updateList] = useState(['原始项目1', '原始项目2', '原始项目3']) |
| 271 | + setList = updateList |
| 272 | + |
| 273 | + return ( |
| 274 | + <NoticeBar direction="vertical" height={height} speed={10} duration={500}> |
| 275 | + {list.map((item, index) => ( |
| 276 | + <div |
| 277 | + className="custom-item" |
| 278 | + style={{ height: `${height}px`, lineHeight: `${height}px` }} |
| 279 | + key={index} |
| 280 | + > |
| 281 | + {item} |
| 282 | + </div> |
| 283 | + ))} |
| 284 | + </NoticeBar> |
| 285 | + ) |
| 286 | + } |
| 287 | + |
| 288 | + const { container } = render(<TestComponent />) |
| 289 | + |
| 290 | + // 等待初始渲染完成 |
| 291 | + await waitFor(() => { |
| 292 | + const wrapElement = container.querySelector('.nut-noticebar-box-wrap') |
| 293 | + expect(wrapElement).toHaveStyle('height: 160px') // (3 + 1) * 40 |
| 294 | + |
| 295 | + // 1. 初始时容器的垂直位移为0(显示第一项) |
| 296 | + expect(wrapElement).toHaveStyle('transform: translate3D(0,0px,0)') |
| 297 | + |
| 298 | + const items = container.querySelectorAll('.custom-item') |
| 299 | + expect(items).toHaveLength(3) |
| 300 | + expect(items[0]).toHaveTextContent('原始项目1') |
| 301 | + }) |
| 302 | + |
| 303 | + // 等待轮播进行一段时间,确保当前不是第一项 |
| 304 | + await waitFor( |
| 305 | + () => { |
| 306 | + const wrapElement = container.querySelector('.nut-noticebar-box-wrap') |
| 307 | + const transform = wrapElement |
| 308 | + ?.getAttribute('style') |
| 309 | + ?.match(/transform:\s*translate3D\(([^,]+),([^,]+),([^)]+)\)/) |
| 310 | + const yOffset = transform ? transform[2].trim() : '0px' |
| 311 | + |
| 312 | + // 验证已经轮播到非第一项(垂直偏移不为0) |
| 313 | + expect(yOffset).not.toBe('0px') |
| 314 | + }, |
| 315 | + { timeout: 2000 } |
| 316 | + ) // 给足够时间让轮播发生 |
| 317 | + |
| 318 | + // 变更列表数据 |
| 319 | + act(() => { |
| 320 | + setList(['新项目A', '新项目B', '新项目C', '新项目D']) |
| 321 | + }) |
| 322 | + |
| 323 | + await waitFor(() => { |
| 324 | + // 验证容器高度更新为新的计算值 |
| 325 | + const wrapElement = container.querySelector('.nut-noticebar-box-wrap') |
| 326 | + expect(wrapElement).toHaveStyle('height: 200px') // (4 + 1) * 40 |
| 327 | + |
| 328 | + // 验证变更后重置回第一项: |
| 329 | + // 1. 容器的垂直位移重置为0 |
| 330 | + expect(wrapElement).toHaveStyle('transform: translate3D(0,0px,0)') |
| 331 | + |
| 332 | + // 2. 第一个子项没有额外的transform |
| 333 | + const firstItem = container.querySelector( |
| 334 | + '.custom-item:first-child' |
| 335 | + ) as HTMLElement |
| 336 | + expect(firstItem.style.transform).toBe('') |
| 337 | + |
| 338 | + // 验证元素结构更新:应该有4个项目 |
| 339 | + const items = container.querySelectorAll('.custom-item') |
| 340 | + expect(items).toHaveLength(4) |
| 341 | + |
| 342 | + // 验证当前显示的是新列表的第一项内容 |
| 343 | + expect(items[0]).toHaveTextContent('新项目A') |
| 344 | + expect(items[1]).toHaveTextContent('新项目B') |
| 345 | + expect(items[2]).toHaveTextContent('新项目C') |
| 346 | + expect(items[3]).toHaveTextContent('新项目D') |
| 347 | + |
| 348 | + // 验证样式更新:每个item的高度样式 |
| 349 | + items.forEach((item) => { |
| 350 | + expect(item).toHaveStyle(`height: ${height}px`) |
| 351 | + expect(item).toHaveStyle(`line-height: ${height}px`) |
| 352 | + }) |
| 353 | + }) |
| 354 | +}) |
0 commit comments