From b497d1d020249794ad1f706dbb1333276d496444 Mon Sep 17 00:00:00 2001 From: hanyuxinting Date: Thu, 10 Oct 2024 20:01:27 +0800 Subject: [PATCH] fix: test codecr --- .../__test__/__snapshots__/menu.spec.tsx.snap | 109 ++++++++++++++++++ src/packages/menu/__test__/menu.spec.tsx | 24 +++- src/packages/menu/menu.tsx | 5 +- 3 files changed, 131 insertions(+), 7 deletions(-) diff --git a/src/packages/menu/__test__/__snapshots__/menu.spec.tsx.snap b/src/packages/menu/__test__/__snapshots__/menu.spec.tsx.snap index 1636a11ffc..cc86886c31 100644 --- a/src/packages/menu/__test__/__snapshots__/menu.spec.tsx.snap +++ b/src/packages/menu/__test__/__snapshots__/menu.spec.tsx.snap @@ -96,3 +96,112 @@ exports[`should match snapshot 1`] = ` `; + +exports[`should match snapshot of two columns in one line 1`] = ` + +
+
+
+
+ 全部商品 +
+ + + +
+
+
+ +
+
+
+`; + +exports[`should match snapshot of two columns in one line 2`] = ` + +
+
+
+ +`; diff --git a/src/packages/menu/__test__/menu.spec.tsx b/src/packages/menu/__test__/menu.spec.tsx index 8223c40ced..3fe2dc5986 100644 --- a/src/packages/menu/__test__/menu.spec.tsx +++ b/src/packages/menu/__test__/menu.spec.tsx @@ -3,7 +3,6 @@ import { act, fireEvent, render } from '@testing-library/react' import '@testing-library/jest-dom' import { Success } from '@nutui/icons-react' import Menu from '@/packages/menu' -import MenuItem from '@/packages/menuitem' function mockGetBoundingClientRect(rect: any): () => void { const spy = vi.spyOn(Element.prototype, 'getBoundingClientRect') @@ -29,7 +28,21 @@ test('should match snapshot', () => { ] const { asFragment } = render( - + + + ) + expect(asFragment()).toMatchSnapshot() +}) + +test('should match snapshot of two columns in one line', () => { + const options = [ + { text: '全部商品', value: 0 }, + { text: '新款商品', value: 1 }, + { text: '活动商品', value: 2 }, + ] + const { asFragment } = render( + + ) expect(asFragment()).toMatchSnapshot() @@ -52,26 +65,25 @@ test('test props', async () => { const { container, getByText } = render( - } activeTitleClass="activeTitleClass" inactiveTitleClass="inactiveTitleClass" onChange={testClick} /> + ) await act(() => { fireEvent.click(getByText('custom title')) - fireEvent.click(container.querySelectorAll('.nut-menu-container-item')[1]) - expect(testClick).toBeCalledWith({ text: '新款商品', value: 1 }) }) diff --git a/src/packages/menu/menu.tsx b/src/packages/menu/menu.tsx index b3eeeaeaea..5e0d8c1783 100644 --- a/src/packages/menu/menu.tsx +++ b/src/packages/menu/menu.tsx @@ -67,7 +67,10 @@ export const Menu: FunctionComponent> & { }) const getScrollTop = (el: Element | Window) => { - return Math.max(0, 'scrollTop' in el ? el.scrollTop : el.scrollY) + return Math.max( + 0, + el === window ? window.scrollY : (el as Element).scrollTop + ) } const onScroll = useCallback(() => { const scrollTop = getScrollTop(window)