-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
877dd4e
commit a4f5901
Showing
8 changed files
with
269 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,82 @@ | ||
import { render, fireEvent, cleanup } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
// mask.test.tsx | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
import Mask from './index'; | ||
import { prefixCls } from '../../utils'; | ||
import Mask from './'; | ||
|
||
afterEach(() => { | ||
cleanup(); // 清除所有渲染的组件 | ||
}); | ||
describe('Mask Component', () => { | ||
it('should not render mask when visible is false', () => { | ||
render(<Mask visible={false} data-testid="mask" />); | ||
|
||
// 确保在 visible 为 false 时不渲染遮罩 | ||
expect(screen.queryByTestId('mask')).toBeNull(); | ||
}); | ||
|
||
describe('Mask component visibility', () => { | ||
it('should be hidden initially and visible after updating props', async () => { | ||
render(<Mask visible={false} />); | ||
const element = document.body.querySelector( | ||
`.${prefixCls}-mask`, | ||
) as HTMLElement; | ||
// 初始状态,Mask 不可见 | ||
expect(element).toBeNull(); | ||
render(<Mask visible={true} />); | ||
expect( | ||
document.body.querySelector(`.${prefixCls}-mask`), | ||
).toBeInTheDocument(); | ||
fireEvent.click( | ||
document.body.querySelector(`.${prefixCls}-mask`) as HTMLElement, | ||
it('should render mask when visible is true', async () => { | ||
render(<Mask visible={true} className="test-02" />); | ||
|
||
// 等待组件渲染完成 | ||
await waitFor(() => { | ||
expect(document.querySelector('.test-02')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should call onMaskClick when mask is clicked', async () => { | ||
const mockOnMaskClick = jest.fn(); | ||
|
||
render( | ||
<Mask visible={true} onMaskClick={mockOnMaskClick} className="test-03" />, | ||
); | ||
expect(document.body.querySelector(`.${prefixCls}-mask`)).toBeNull(); | ||
|
||
// 等待组件渲染完成 | ||
await waitFor(() => { | ||
expect(document.querySelector('.test-03')).toBeInTheDocument(); | ||
}); | ||
|
||
// 触发点击事件 | ||
fireEvent.click(document.querySelector('.test-03')!); | ||
|
||
// 确保点击事件处理程序被调用 | ||
expect(mockOnMaskClick).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should apply zIndex and background correctly', () => { | ||
render(<Mask visible={true} zIndex={100} backgroundColor="#ff0000" />); | ||
const element = document.body.querySelector( | ||
`.${prefixCls}-mask`, | ||
) as HTMLElement; | ||
expect(element).toHaveStyle({ | ||
zIndex: 100, | ||
backgroundColor: '#ff0000', | ||
it('should apply correct styles based on props', async () => { | ||
const backgroundColor = 'rgba(0, 0, 0, 0.5)'; | ||
const zIndex = 1000; | ||
|
||
render( | ||
<Mask | ||
visible={true} | ||
backgroundColor={backgroundColor} | ||
zIndex={zIndex} | ||
className="test-04" | ||
/>, | ||
); | ||
|
||
// 等待组件渲染完成 | ||
await waitFor(() => { | ||
const maskElement = document.querySelector('.test-04') as HTMLElement; | ||
expect(maskElement).toBeInTheDocument(); | ||
expect(maskElement).toHaveStyle(`background-color: ${backgroundColor}`); | ||
expect(maskElement).toHaveStyle(`z-index: ${zIndex}`); | ||
}); | ||
fireEvent.click(element); | ||
expect(document.body.querySelector(`.${prefixCls}-mask`)).toBeNull(); | ||
}); | ||
|
||
it('should not call onMaskClick when mask is not visible', async () => { | ||
const mockOnMaskClick = jest.fn(); | ||
|
||
render( | ||
<Mask | ||
visible={false} | ||
onMaskClick={mockOnMaskClick} | ||
className="test-05" | ||
/>, | ||
); | ||
|
||
// 确保在 visible 为 false 时不渲染遮罩 | ||
expect(document.querySelector('.test-05')).toBeNull(); | ||
|
||
// 触发点击事件,不应调用 onMaskClick | ||
fireEvent.click(document.body); | ||
expect(mockOnMaskClick).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.