javascript - 点击按钮事件不会改变酶测试中的属性
问题描述
我有一个组件,我有另一个组件按钮,带有点击处理。这是我的主要组件的示例。
<div className={styles.roulette}>
{chosenProjectElement}
<div className={styles.action}>
<PromoButton
caption={chosenProject ? 'Давай еще' : 'Покажи мне'}
onClick={onSpin}
size={isMobile ? 'small' : 'default'}
color={theme === 'simpsons' ? 'yellow' : 'orange'}
/>
{triangleIcon}
</div>
<div className={styles.wheel} ref={wheelRef} data-testid="wheel">
<CircleIcon layout={layout} />
</div>
</div>
我也有点击句柄的功能
const onSpin = useCallback((): void => {
if (!wheelRef.current) return
if (updatingTimerId.current) {
clearTimeout(updatingTimerId.current)
}
wheelRef.current.removeAttribute('style')
const degrees = PROJECT_WHEEL_WIDTH * Math.round(Math.random() * 500)
spinnedDegrees.current += !spinnedDegrees.current
? degrees + FIRST_SPIN
: degrees
const styles = `transform: rotate(${spinnedDegrees.current}deg)`
let chosenProjectIndex =
((spinnedDegrees.current - FIRST_SPIN) / PROJECT_WHEEL_WIDTH) %
projectsInfo.length
if (isMobile) chosenProjectIndex = chosenProjectIndex - 4
updatingTimerId.current = window.setTimeout(() => {
updateChosenProject(chosenProjectIndex)
}, WHEEL_ANIMATION_SPINNING)
wheelRef.current.setAttribute('style', styles)
}, [isMobile])
当我点击按钮时,会发生两件事。首先我的轮子旋转,超时后显示带有测试的隐藏块。我正在尝试测试此事件。
这是我的测试:
describe('<Roulette />', () => {
test('should render Roulette component', () => {
const component = shallow(<Roulette layout="promo" />)
expect(toJson(component)).toMatchSnapshot()
})
test('should show hidden block after first spin', () => {
jest.useFakeTimers()
const component = mount(<Roulette layout="promo" />)
const button = component.find('PromoButton')
expect(button).toHaveLength(1)
button.simulate('click')
act(() => {
jest.runAllTimers()
})
const hiddenTextContainer = component.find({
'data-testid': 'text-container'
})
expect(component).toContain(hiddenTextContainer)
jest.useRealTimers()
})
test('should spin wheel', () => {
const component = mount(<Roulette layout="promo" />)
const button = component.find('PromoButton')
expect(button).toHaveLength(1)
act(() => {
button.simulate('click')
})
const wheel = component.find({'data-testid': 'wheel'})
expect(wheel).toHaveProperty('style')
})
我第一次面对考验。你能帮我看看哪里错了吗?
解决方案
推荐阅读
- android - 当我尝试拍照时,相机不断停止(Android Studio)
- artifactory - 使用 JFrog CLI 删除工件会给出“200 OK”,但不会删除工件
- python - 如何为 wxPython(例如 wxMSW)使用平台特定的“端口”?
- jenkins - Jenkins - 将 withCredentials 翻译成声明式语法
- angular - 如何使用 pdf.js 输出转换文件并将其发送到后端
- jmeter - 带有 Jmeter 的 Graphql
- javascript - 如何访问本地存储中某些元素的键?
- java - 如何在 Jboss 重启时添加请求?
- spring-integration - Spring集成多个队列消费者
- git - 无法将图片加载到个人 github 页面