Skip to content

Commit 75449c2

Browse files
committed
fix: 调整命名,单测修改
1 parent a7c611c commit 75449c2

File tree

4 files changed

+140
-142
lines changed

4 files changed

+140
-142
lines changed

src/packages/popover/__tests__/popover.spec.tsx

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import '@testing-library/jest-dom'
44
import { Tips, Close } from '@nutui/icons-react'
55
import Popover from '../index'
66
import Button from '@/packages/button'
7+
import { FullPosition } from '@/types'
78

89
const itemList = [
910
{
@@ -27,6 +28,10 @@ const itemListOne = [
2728
icon: <Tips />,
2829
action: {
2930
icon: <Close />,
31+
onClick: (e: any) => {
32+
console.log('onclick 1')
33+
e.stopPropagation()
34+
},
3035
},
3136
},
3237
]
@@ -51,9 +56,7 @@ const itemListDisabled = [
5156
test('render popover content', async () => {
5257
const { container } = render(
5358
<Popover visible list={itemList}>
54-
<Button type="primary" shape="square">
55-
基础用法
56-
</Button>
59+
<Button type="primary">基础用法</Button>
5760
</Popover>
5861
)
5962
const content = document.querySelectorAll('.nut-popover-content')[0]
@@ -76,9 +79,7 @@ test('render popover content dark', async () => {
7679
test('render popover position', async () => {
7780
render(
7881
<Popover visible list={itemList} location="bottom-left">
79-
<Button type="primary" shape="square">
80-
基础用法
81-
</Button>
82+
<Button type="primary">基础用法</Button>
8283
</Popover>
8384
)
8485
const content = document.querySelectorAll('.nut-popover-content')[0]
@@ -87,66 +88,43 @@ test('render popover position', async () => {
8788
)
8889
})
8990

90-
test('render popover position2', async () => {
91-
const { container } = render(
91+
test('render popover position with arrowOffset', async () => {
92+
const { rerender } = render(
9293
<Popover visible list={itemList} location="bottom-left" arrowOffset={20}>
93-
<Button type="primary" shape="square">
94-
基础用法
95-
</Button>
94+
<Button type="primary">基础用法</Button>
9695
</Popover>
9796
)
98-
const content = document.querySelectorAll(
99-
'.nut-popover-arrow'
100-
)[0] as HTMLElement
101-
expect(content).toHaveAttribute('style', 'left: 36px;')
102-
})
10397

104-
test('render popover position22', async () => {
105-
const { container } = render(
106-
<Popover visible list={itemList} arrowOffset={20}>
107-
<Button type="primary" shape="square">
108-
基础用法
109-
</Button>
110-
</Popover>
111-
)
112-
const content = document.querySelectorAll(
113-
'.nut-popover-arrow'
114-
)[0] as HTMLElement
115-
expect(content).toHaveAttribute('style', 'left: calc(50% + 20px);')
116-
})
98+
const checkArrowStyles = (location: FullPosition, expectedStyles: string) => {
99+
rerender(
100+
<Popover visible list={itemList} location={location} arrowOffset={20}>
101+
<Button type="primary">基础用法</Button>
102+
</Popover>
103+
)
104+
content = document.querySelectorAll('.nut-popover-arrow')[0]
105+
expect(content).toHaveAttribute('style', expectedStyles)
106+
}
117107

118-
test('render popover position3', async () => {
119-
const { container } = render(
120-
<Popover visible list={itemList} location="left-top" arrowOffset={20}>
121-
<Button type="primary" shape="square">
122-
基础用法
123-
</Button>
124-
</Popover>
125-
)
126-
const content = document.querySelectorAll(
127-
'.nut-popover-arrow'
128-
)[0] as HTMLElement
129-
expect(content).toHaveAttribute('style', 'top: -4px;')
130-
})
108+
let content = document.querySelectorAll('.nut-popover-arrow')[0]
109+
expect(content).toHaveAttribute('style', 'left: 36px;')
131110

132-
test('render popover position33', async () => {
133-
const { container } = render(
134-
<Popover visible list={itemList} location="left" arrowOffset={20}>
135-
<Button type="primary" shape="square">
136-
基础用法
137-
</Button>
138-
</Popover>
139-
)
140-
const content = document.querySelectorAll(
141-
'.nut-popover-arrow'
142-
)[0] as HTMLElement
143-
expect(content).toHaveAttribute('style', 'top: calc(50% - 20px);')
111+
checkArrowStyles('bottom', 'left: calc(50% + 20px);')
112+
checkArrowStyles('bottom-right', 'right: -4px;')
113+
checkArrowStyles('left', 'top: calc(50% - 20px);')
114+
checkArrowStyles('left-bottom', 'bottom: 36px;')
115+
checkArrowStyles('left-top', 'top: -4px;')
116+
checkArrowStyles('right', 'top: calc(50% - 20px);')
117+
checkArrowStyles('right-bottom', 'bottom: 36px;')
118+
checkArrowStyles('right-top', 'top: -4px;')
119+
checkArrowStyles('top-right', 'right: -4px;')
120+
checkArrowStyles('top-left', 'left: 36px;')
121+
checkArrowStyles('top', 'left: calc(50% + 20px);')
144122
})
145123

146124
test('render position fixed ', async () => {
147125
const close = vi.fn()
148126
const click = vi.fn()
149-
const { container, getByTestId } = render(
127+
const { getByTestId } = render(
150128
<div
151129
style={{
152130
height: '200px',
@@ -166,7 +144,7 @@ test('render position fixed ', async () => {
166144
onClick={click}
167145
onClose={close}
168146
>
169-
<Button data-testid="a" type="primary" shape="square">
147+
<Button data-testid="a" type="primary">
170148
position: fixed
171149
</Button>
172150
</Popover>
@@ -186,11 +164,9 @@ test('render position fixed ', async () => {
186164

187165
test('should emit onchoose event when clicking the action', async () => {
188166
const choose = vi.fn()
189-
const { container } = render(
167+
render(
190168
<Popover visible list={itemList} onSelect={choose}>
191-
<Button type="primary" shape="square">
192-
明朗风格
193-
</Button>
169+
<Button type="primary">明朗风格</Button>
194170
</Popover>
195171
)
196172
const contentItem = document.querySelectorAll('.nut-popover-item')[0]
@@ -201,22 +177,21 @@ test('should emit onchoose event when clicking the action', async () => {
201177

202178
test('should not emit select event when the action is disabled', async () => {
203179
const choose = vi.fn()
204-
const { container } = render(
180+
render(
205181
<Popover visible list={itemListDisabled} onSelect={choose}>
206-
<Button type="primary" shape="square">
207-
明朗风格
208-
</Button>
182+
<Button type="primary">明朗风格</Button>
209183
</Popover>
210184
)
211185
const contentItem = document.querySelectorAll('.nut-popover-item')[0]
212186
fireEvent.click(contentItem)
213187
await waitFor(() => expect(choose).not.toBeCalled())
214188
})
215189

216-
test('target id', async () => {
217-
const choose = vi.fn()
190+
test('click event', async () => {
218191
const close = vi.fn()
219-
const { container, getByTestId } = render(
192+
const close1 = vi.fn()
193+
const open = vi.fn()
194+
const { getByTestId, rerender } = render(
220195
<div>
221196
<Popover visible targetId="popid" list={itemListOne} />
222197
<Button type="primary" id="popid">
@@ -228,4 +203,32 @@ test('target id', async () => {
228203
</div>
229204
)
230205
fireEvent.click(getByTestId('closeid'))
206+
await waitFor(() =>
207+
expect(document.querySelectorAll('.nut-popover')[0]).toHaveStyle({
208+
visibility: 'hidden',
209+
})
210+
)
211+
212+
rerender(
213+
<>
214+
<Popover
215+
visible
216+
targetId="popid"
217+
list={itemListOne}
218+
location="left-bottom"
219+
onOpen={open}
220+
onClose={close1}
221+
data-testid="popoverid"
222+
/>
223+
<Button type="primary" id="popid" data-testid="popid">
224+
自定义目标元素
225+
</Button>
226+
</>
227+
)
228+
fireEvent.click(getByTestId('popid'))
229+
await waitFor(() => {
230+
expect(document.querySelectorAll('.nut-popover')[0]).toHaveStyle({
231+
visibility: 'hidden',
232+
})
233+
})
231234
})

src/packages/popover/popover.taro.tsx

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,10 @@ import Popup from '@/packages/popup/index.taro'
1212
import { getRectByTaro } from '@/utils/get-rect-by-taro'
1313
import { ComponentDefaults } from '@/utils/typings'
1414
import { useRtl } from '@/packages/configprovider/index.taro'
15-
import { TaroPopoverProps, PopoverList } from '@/types'
15+
import { TaroPopoverProps, PopoverList, WrapperPosition } from '@/types'
1616
import pxTransform from '@/utils/px-transform'
1717
import useUuid from '@/hooks/use-uuid'
1818

19-
export interface WrapperPosition {
20-
width: number
21-
height: number
22-
left: number
23-
top: number
24-
right: number
25-
}
26-
2719
const defaultProps = {
2820
...ComponentDefaults,
2921
list: [],
@@ -95,19 +87,16 @@ export const Popover: FunctionComponent<
9587
const popoverId = `popover-${uid}`
9688

9789
const getWrapperPosition = async () => {
98-
let rect
99-
if (targetId) {
100-
const elem = document.querySelector(`#${targetId}`)
101-
rect = await getRectByTaro(elem, targetId)
102-
} else {
103-
rect = await getRectByTaro(popoverRef.current, popoverId)
104-
}
90+
const rect = targetId
91+
? await getRectByTaro(document.querySelector(`#${targetId}`), targetId)
92+
: await getRectByTaro(popoverRef.current, popoverId)
93+
const { width, height, right, left, top } = rect
10594
setWrapperPosition({
106-
width: rect.width,
107-
height: rect.height,
108-
left: rtl ? rect.right : rect.left,
109-
top: rect.top,
110-
right: rtl ? rect.left : rect.right,
95+
width,
96+
height,
97+
left: rtl ? right : left,
98+
top,
99+
right: rtl ? left : right,
111100
})
112101
}
113102

@@ -145,7 +134,6 @@ export const Popover: FunctionComponent<
145134
return styles
146135
}
147136
const { width, height, left, top, right } = wrapperPosition
148-
149137
const direction = location.split('-')[0]
150138
const skew = location.split('-')[1]
151139
let cross = 0
@@ -155,7 +143,6 @@ export const Popover: FunctionComponent<
155143
cross += +offset[1]
156144
parallel += +rtloffset
157145
}
158-
159146
if (width) {
160147
const dir = rtl ? 'right' : 'left'
161148
if (['bottom', 'top'].includes(direction)) {
@@ -275,7 +262,7 @@ export const Popover: FunctionComponent<
275262
<ArrowRadius width={8} height={4} />
276263
</View>
277264
)}
278-
{Array.isArray(children) ? children[1] : ''}
265+
{Array.isArray(children) ? children[1] : null}
279266
{list.map((item, index) => {
280267
return (
281268
<View

0 commit comments

Comments
 (0)