|
| 1 | +import { render, fireEvent, describe, test, expect, beforeEach, afterEach } from '@test/utils'; |
| 2 | +import { vi, type Mock } from 'vitest'; |
| 3 | +import React from 'react'; |
| 4 | +import { AppIcon } from 'tdesign-icons-react'; |
| 5 | +import { useScroll } from 'ahooks'; |
| 6 | +import BackTop from '../Backtop'; |
| 7 | +import { checkWindow } from '../../_util/dom'; |
| 8 | + |
| 9 | +vi.mock('ahooks', async () => { |
| 10 | + const actual = await vi.importActual('ahooks'); |
| 11 | + return { |
| 12 | + ...actual, |
| 13 | + useScroll: vi.fn(), |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock('../../_util/dom', async () => { |
| 18 | + const actual = await vi.importActual('../../_util/dom'); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + checkWindow: vi.fn(), |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +describe('BackTop', () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.useFakeTimers(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + vi.useRealTimers(); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('props', () => { |
| 35 | + test(':target', async () => { |
| 36 | + (checkWindow as Mock).mockReturnValue(true); |
| 37 | + document.documentElement.scrollTo = vi.fn(); |
| 38 | + const { container } = render( |
| 39 | + <div> |
| 40 | + <BackTop target={() => document.querySelector('#custom-container') as HTMLElement} /> |
| 41 | + <div id="custom-container"></div> |
| 42 | + </div>, |
| 43 | + ); |
| 44 | + fireEvent.click(container.querySelector('.t-back-top')); |
| 45 | + expect(document.documentElement.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' }); |
| 46 | + }); |
| 47 | + |
| 48 | + test(':fixed', async () => { |
| 49 | + (window.document.documentElement as any).window = window; |
| 50 | + const { container } = render(<BackTop fixed />); |
| 51 | + expect(container.querySelector('.t-back-top--fixed')).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + test(':icon', async () => { |
| 55 | + const { container, rerender } = render(<BackTop icon={true} />); |
| 56 | + expect(container.querySelector('.t-back-top__icon')).toBeTruthy(); |
| 57 | + |
| 58 | + rerender(<BackTop icon={<AppIcon />} />); |
| 59 | + expect(container.querySelector('.t-icon-app')).toBeTruthy(); |
| 60 | + }); |
| 61 | + |
| 62 | + test(':text', async () => { |
| 63 | + const text = '回到顶部'; |
| 64 | + const { queryByText } = render(<BackTop text={text} />); |
| 65 | + expect(queryByText(text)).toBeTruthy(); |
| 66 | + }); |
| 67 | + |
| 68 | + test(':theme', async () => { |
| 69 | + const themes = ['round', 'half-round', 'round-dark', 'half-round-dark'] as const; |
| 70 | + themes.forEach((theme) => { |
| 71 | + const { container } = render(<BackTop theme={theme} />); |
| 72 | + expect(container.querySelector(`.t-back-top--${theme}`)).toBeTruthy(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + test(':visibilityHeight', async () => { |
| 77 | + (useScroll as Mock).mockReturnValue({ top: 400 }); |
| 78 | + const { container } = render(<BackTop visibilityHeight={200} />); |
| 79 | + |
| 80 | + expect((container.querySelector('.t-back-top') as HTMLElement).style.opacity).toBe('1'); |
| 81 | + }); |
| 82 | + |
| 83 | + test(':container', async () => { |
| 84 | + render( |
| 85 | + <div> |
| 86 | + <BackTop container={() => document.querySelector('#custom-container')} /> |
| 87 | + <div id="custom-container"></div> |
| 88 | + </div>, |
| 89 | + ); |
| 90 | + document.querySelector('#custom-container').scrollTo = vi.fn(); |
| 91 | + fireEvent.click(document.querySelector('.t-back-top')); |
| 92 | + expect(document.querySelector('#custom-container').scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('events', () => { |
| 97 | + test(':onToTop', async () => { |
| 98 | + const onToTop = vi.fn(); |
| 99 | + document.documentElement.scrollTo = vi.fn(); |
| 100 | + const { container } = render(<BackTop onToTop={onToTop} />); |
| 101 | + fireEvent.click(container.querySelector('.t-back-top')); |
| 102 | + expect(onToTop).toHaveBeenCalled(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('interaction', () => { |
| 107 | + test('should show when scrollY > visibilityHeight', async () => { |
| 108 | + (useScroll as Mock).mockReturnValue({ top: 100 }); |
| 109 | + const { container, rerender } = render(<BackTop visibilityHeight={200} />); |
| 110 | + expect((container.querySelector('.t-back-top') as HTMLElement).style.opacity).toBe('0'); |
| 111 | + (useScroll as Mock).mockReturnValue({ top: 400 }); |
| 112 | + rerender(<BackTop visibilityHeight={200} />); |
| 113 | + expect((container.querySelector('.t-back-top') as HTMLElement).style.opacity).toBe('1'); |
| 114 | + }); |
| 115 | + |
| 116 | + test('should scroll to top when clicked', async () => { |
| 117 | + (useScroll as Mock).mockReturnValue({ top: 400 }); |
| 118 | + document.documentElement.scrollTo = vi.fn(); |
| 119 | + const { container } = render(<BackTop />); |
| 120 | + fireEvent.click(container.querySelector('.t-back-top')); |
| 121 | + expect(document.documentElement.scrollTo).toHaveBeenCalledWith({ top: 0, behavior: 'smooth' }); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('edge cases', () => { |
| 126 | + test('should handle empty text', async () => { |
| 127 | + const { container } = render(<BackTop text="" />); |
| 128 | + expect(container.querySelector('.t-back-top__text')).toBeFalsy(); |
| 129 | + }); |
| 130 | + |
| 131 | + test('should handle ssr', async () => { |
| 132 | + (checkWindow as Mock).mockReturnValue(false); |
| 133 | + render(<BackTop />); |
| 134 | + expect(document.querySelector('.t-back-top')).toBeTruthy(); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments