|
1 | 1 | import React, { useState, useRef } from 'react';
|
2 | 2 | import { fireEvent, render, screen } from '@testing-library/react';
|
3 | 3 | import Tour from '../src/index';
|
| 4 | +import {spyElementPrototypes} from "rc-util/lib/test/domHook"; |
| 5 | +import {act} from "react-dom/test-utils"; |
| 6 | +import {resizeWindow} from "./utils"; |
4 | 7 |
|
5 | 8 | describe('Tour', () => {
|
6 | 9 | beforeEach(() => {
|
@@ -342,4 +345,77 @@ describe('Tour', () => {
|
342 | 345 | fireEvent.click(document.querySelector('.open-tour'));
|
343 | 346 | expect(document.querySelector('.rc-tour-title').innerHTML).toBe('step 2');
|
344 | 347 | });
|
| 348 | + |
| 349 | + const mockBtnRect = (rect: { x: number, y: number, width: number, height: number }) => { |
| 350 | + spyElementPrototypes(HTMLButtonElement, { |
| 351 | + getBoundingClientRect: { |
| 352 | + get(): any { |
| 353 | + return () => ({...rect, left: rect.x, top: rect.y}); |
| 354 | + } |
| 355 | + }, |
| 356 | + scrollIntoView: { |
| 357 | + get(): any { |
| 358 | + return (val) => val |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + }); |
| 363 | + } |
| 364 | + |
| 365 | + it('should update position when window resize', async () => { |
| 366 | + mockBtnRect({ |
| 367 | + x: 800, |
| 368 | + y: 333, |
| 369 | + width: 230, |
| 370 | + height: 180 |
| 371 | + }); |
| 372 | + const Demo = () => { |
| 373 | + const btnRef = useRef<HTMLButtonElement>(null); |
| 374 | + |
| 375 | + return ( |
| 376 | + <div style={{width: '100%'}}> |
| 377 | + <button className="btn2" ref={btnRef} onClick={() => { |
| 378 | + mockBtnRect({ |
| 379 | + x: 15, |
| 380 | + y: 10, |
| 381 | + width: 230, |
| 382 | + height: 180 |
| 383 | + }); |
| 384 | + resizeWindow(300, 200); |
| 385 | + }}> |
| 386 | + 按钮 |
| 387 | + </button> |
| 388 | + <Tour |
| 389 | + open |
| 390 | + placement={'bottom'} |
| 391 | + steps={[ |
| 392 | + { |
| 393 | + title: '创建', |
| 394 | + description: '创建一条数据', |
| 395 | + target: () => btnRef.current, |
| 396 | + }, |
| 397 | + ]} |
| 398 | + /> |
| 399 | + </div> |
| 400 | + ); |
| 401 | + }; |
| 402 | + const {baseElement, unmount} = render(<Demo/>); |
| 403 | + expect(baseElement.querySelector('.rc-tour-target-placeholder')).toHaveStyle('left: 794px; top: 327px; width: 242px; height: 192px;'); |
| 404 | + fireEvent.click(baseElement.querySelector('.btn2')); |
| 405 | + await act(() => { |
| 406 | + jest.runAllTimers(); |
| 407 | + }); |
| 408 | + expect(baseElement.querySelector('.rc-tour-target-placeholder')).toHaveStyle('left: 9px; top: 4px; width: 242px; height: 192px;'); |
| 409 | + |
| 410 | + expect(baseElement).toMatchSnapshot(); |
| 411 | + |
| 412 | + unmount(); |
| 413 | + mockBtnRect({ |
| 414 | + x: 0, |
| 415 | + y: 0, |
| 416 | + width: 0, |
| 417 | + height: 0 |
| 418 | + }); |
| 419 | + |
| 420 | + }); |
345 | 421 | });
|
0 commit comments