代码之家  ›  专栏  ›  技术社区  ›  artooras

在开玩笑的时候有专注的行为

  •  0
  • artooras  · 技术社区  · 7 年前

    我很难理解 toHaveFocus() 工作正常。以下是我的设置:

    MyCalpTun.JS

    import React from 'react'
    import styled from 'styled-components'
    
    import TextArea from './TextArea'
    
    
    const Container = styled.div`
      flex: 1;
      height: 100%;
      padding: ${props => props.theme.size};
    `
    
    const Title = styled(TextArea)`
      font-weight: bold;
      font-size: ${props => props.theme.sizeLarger};
      margin-left: ${props => props.theme.sizeSmall};
    `
    
    class MyComponent extends React.Component {
    
      handleTitleChange = e => {
        this.props.onTitleChange(e.target.value)
      }
    
      handleTitleLostFocus = () => {
        this.props.onChangeComplete()
      }
    
      render () {
        return (
          <Container>
            <Title 
              value={this.props.item.title || ''}
              onChange={this.handleTitleChange}
              onBlur={this.handleTitleLostFocus}
            />
          </Container>
        )
      }
    }
    
    export default MyComponent
    

    mycomponent.test.js测试

    import React from 'react'
    import {render, fireEvent, prettyDOM} from 'react-testing-library'
    
    import MyComponent from '../MyComponent'
    
    
    describe('MyComponent', () => {
      it('handles title changes', () => {
        const title = 'title'
        const handleTitleChangeMock = jest.fn()
        const {getByText} = render(
          <MyComponent
            item={{
              title: title
            }}
            onTitleChange={handleTitleChangeMock}
            onChangeComplete={() => {}}
          />
        )
        const titleInput = getByText(title)
        console.log(prettyDOM(titleInput))
        fireEvent.click(getByText(title))
        expect(getByText(title)).toHaveFocus()
        fireEvent.change(getByText(title), {target: {value: title.slice(0, -1)}})
        expect(handleTitleChangeMock).toHaveBeenCalledTimes(1)
        expect(handleTitleChangeMock).toHaveBeenCalledWith(title.slice(0, -1))
      })
    })
    

    当我这样做的时候:

    const titleInput = getByText(title)
    console.log(prettyDOM(titleInput))
    

    控制台记录以下内容:

    <textarea
        class="sc-htoDjs dLjZCT sc-bxivhb jdLTBU"
        style="height: 0px;"
      >
        title
      </textarea>
    

    textarea 元素是我的目标。但当我这样做的时候:

    fireEvent.click(titleInput)
    expect(titleInput).toHaveFocus()
    

    我得到这个错误:

    Received:
      <body><textarea style="min-height: 0 !important; max-height: none !important; height: 0px !important; visibility: hidden !important; overflow: hidden !important; position: absolute !important; z-index: -1000 !important; top: 0px !important; right: 0px;" /><div><div class="sc-bZQynM ePHCfO"><textarea class="sc-htoDjs dLjZCT sc-bxivhb jdLTBU" style="height: 0px;">title</textarea></div></div></body>
    
      at Object.it (src/__tests__/MyComponent.test.js:84:30)
          at new Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)
    

    我不太明白为什么当我试图断言 特克斯塔利亚 要素 toHeaveCox() ,我收到一个错误,该错误引用了下面的整个DOM树 body

    1 回复  |  直到 7 年前
        1
  •  0
  •   Gio Polvara    7 年前

    如果你用 titleInput.focus() 它起作用了。我不知道为什么 fireEvent.focus 不起作用。

    你可以看到它在这里工作 https://codesandbox.io/s/y020r74rjj