Skip to content

Commit b497d1d

Browse files
committed
fix: test codecr
1 parent 11b9288 commit b497d1d

File tree

3 files changed

+131
-7
lines changed

3 files changed

+131
-7
lines changed

src/packages/menu/__test__/__snapshots__/menu.spec.tsx.snap

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,112 @@ exports[`should match snapshot 1`] = `
9696
</div>
9797
</DocumentFragment>
9898
`;
99+
100+
exports[`should match snapshot of two columns in one line 1`] = `
101+
<DocumentFragment>
102+
<div
103+
class="nut-menu"
104+
>
105+
<div
106+
class="nut-menu-bar"
107+
>
108+
<div
109+
class="nut-menu-title nut-menu-title-0"
110+
>
111+
<div
112+
class="nut-menu-title-text"
113+
>
114+
全部商品
115+
</div>
116+
<svg
117+
aria-labelledby="ArrowDown"
118+
class="nut-icon nut-icon-ArrowDown nut-menu-title-icon"
119+
role="presentation"
120+
style="width: 12px; height: 12px;"
121+
viewBox="0 0 1024 1024"
122+
xmlns="http://www.w3.org/2000/svg"
123+
>
124+
<path
125+
d="M158.17 289.83a42.67 42.67 0 1 0-60.34 60.34l384 384a42.67 42.67 0 0 0 60.36 0l384-384a42.67 42.67 0 1 0-60.36-60.34L512 643.67z"
126+
fill="currentColor"
127+
fill-opacity="0.9"
128+
/>
129+
</svg>
130+
</div>
131+
</div>
132+
<div
133+
class="nut-menu-container"
134+
>
135+
<div
136+
class="nut-menu-container-wrap"
137+
style="display: none;"
138+
>
139+
<div
140+
class="nut-menu-container-content"
141+
>
142+
<div
143+
class="nut-menu-container-item active"
144+
style="flex-basis: 50%;"
145+
>
146+
<i
147+
class="nut-menu-container-item-icon"
148+
>
149+
<svg
150+
aria-labelledby="Check"
151+
class="nut-icon nut-icon-Check "
152+
color=""
153+
role="presentation"
154+
viewBox="0 0 1024 1024"
155+
xmlns="http://www.w3.org/2000/svg"
156+
>
157+
<path
158+
d="M998.4 245.03c-219.43 153.6-398.63 332.8-552.23 552.23-40.23 58.51-128 54.86-164.57-3.66-69.49-106.06-149.94-186.51-256-256-51.2-32.91-18.29-113.37 40.23-98.74 117.03 21.94 208.46 69.49 292.57 146.28 157.26-190.17 358.4-340.11 588.8-435.2 62.17-25.6 106.06 58.51 51.2 95.09"
159+
fill="currentColor"
160+
fill-opacity="0.9"
161+
/>
162+
</svg>
163+
</i>
164+
<div
165+
class="nut-menu-container-item-title "
166+
>
167+
全部商品
168+
</div>
169+
</div>
170+
<div
171+
class="nut-menu-container-item "
172+
style="flex-basis: 50%;"
173+
>
174+
<div
175+
class="nut-menu-container-item-title "
176+
>
177+
新款商品
178+
</div>
179+
</div>
180+
<div
181+
class="nut-menu-container-item "
182+
style="flex-basis: 50%;"
183+
>
184+
<div
185+
class="nut-menu-container-item-title "
186+
>
187+
活动商品
188+
</div>
189+
</div>
190+
</div>
191+
</div>
192+
</div>
193+
</div>
194+
</DocumentFragment>
195+
`;
196+
197+
exports[`should match snapshot of two columns in one line 2`] = `
198+
<DocumentFragment>
199+
<div
200+
class="nut-menu"
201+
>
202+
<div
203+
class="nut-menu-bar"
204+
/>
205+
</div>
206+
</DocumentFragment>
207+
`;

src/packages/menu/__test__/menu.spec.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { act, fireEvent, render } from '@testing-library/react'
33
import '@testing-library/jest-dom'
44
import { Success } from '@nutui/icons-react'
55
import Menu from '@/packages/menu'
6-
import MenuItem from '@/packages/menuitem'
76

87
function mockGetBoundingClientRect(rect: any): () => void {
98
const spy = vi.spyOn(Element.prototype, 'getBoundingClientRect')
@@ -29,7 +28,21 @@ test('should match snapshot', () => {
2928
]
3029
const { asFragment } = render(
3130
<Menu>
32-
<MenuItem options={options} value={0} />
31+
<Menu.Item options={options} value={0} />
32+
</Menu>
33+
)
34+
expect(asFragment()).toMatchSnapshot()
35+
})
36+
37+
test('should match snapshot of two columns in one line', () => {
38+
const options = [
39+
{ text: '全部商品', value: 0 },
40+
{ text: '新款商品', value: 1 },
41+
{ text: '活动商品', value: 2 },
42+
]
43+
const { asFragment } = render(
44+
<Menu scrollFixed>
45+
<Menu.Item options={options} value={0} columns={2} />
3346
</Menu>
3447
)
3548
expect(asFragment()).toMatchSnapshot()
@@ -52,26 +65,25 @@ test('test props', async () => {
5265

5366
const { container, getByText } = render(
5467
<Menu lockScroll={false}>
55-
<MenuItem
68+
<Menu.Item
5669
className="custom-className"
5770
title="custom title"
5871
options={options}
5972
value={0}
6073
disabled
61-
columns={2}
74+
direction="up"
6275
icon={<Success />}
6376
activeTitleClass="activeTitleClass"
6477
inactiveTitleClass="inactiveTitleClass"
6578
onChange={testClick}
6679
/>
80+
<Menu.Item />
6781
</Menu>
6882
)
6983

7084
await act(() => {
7185
fireEvent.click(getByText('custom title'))
72-
7386
fireEvent.click(container.querySelectorAll('.nut-menu-container-item')[1])
74-
7587
expect(testClick).toBeCalledWith({ text: '新款商品', value: 1 })
7688
})
7789

src/packages/menu/menu.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ export const Menu: FunctionComponent<Partial<MenuProps>> & {
6767
})
6868

6969
const getScrollTop = (el: Element | Window) => {
70-
return Math.max(0, 'scrollTop' in el ? el.scrollTop : el.scrollY)
70+
return Math.max(
71+
0,
72+
el === window ? window.scrollY : (el as Element).scrollTop
73+
)
7174
}
7275
const onScroll = useCallback(() => {
7376
const scrollTop = getScrollTop(window)

0 commit comments

Comments
 (0)