-
Notifications
You must be signed in to change notification settings - Fork 23
fix: reset stack when unmount #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { type EscCallback } from './Portal'; | |
| import useId from '@rc-component/util/lib/hooks/useId'; | ||
| import { useEvent } from '@rc-component/util'; | ||
|
|
||
| let stack: string[] = []; | ||
| export let stack: string[] = []; // export for testing | ||
|
|
||
| export default function useEscKeyDown(open: boolean, onEsc?: EscCallback) { | ||
| const id = useId(); | ||
|
|
@@ -31,6 +31,7 @@ export default function useEscKeyDown(open: boolean, onEsc?: EscCallback) { | |
| window.addEventListener('keydown', handleEscKeyDown); | ||
|
|
||
| return () => { | ||
| stack = stack.filter(item => item !== id); | ||
|
||
| window.removeEventListener('keydown', handleEscKeyDown); | ||
| }; | ||
| }, [open, id]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import Portal from '../src'; | ||
| import { stack } from '../src/useEscKeyDown'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 考虑测试隔离问题。 直接导入内部实现的 建议在 beforeEach(() => {
global.isOverflow = true;
stack.length = 0; // 清空 stack 以确保测试隔离
});🤖 Prompt for AI Agents |
||
|
|
||
| global.isOverflow = true; | ||
|
|
||
|
|
@@ -355,5 +356,17 @@ describe('Portal', () => { | |
| ); | ||
| expect(onEscB).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should clear stack when unmount', () => { | ||
| const { unmount } = render( | ||
| <Portal open> | ||
| <div /> | ||
| </Portal>, | ||
| ); | ||
|
|
||
| expect(stack).toHaveLength(1); | ||
| unmount(); | ||
| expect(stack).toHaveLength(0); | ||
| }); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.