0

索引.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>title</title>
  </head>
  <body>
    <div id="App"></div>
  </body>
</html>

App.spec.ts

const html = fs
  .readFileSync(path.join(__dirname, '..', 'index.html'))
  .toString()

beforeEach(() => {
  document.documentElement.innerHTML = html
})

describe('App test', () => {
  it('App test', () => {
    const appEl = document.querySelector('#App') as HTMLElement
    new App({ el: appEl, floor: 5, elevatorNum: 3 })

    expect(appEl).toMatchSnapshot()
  })

  it('', () => {
    const requestFunc = jest.spyOn(Controller.getInstance(), 'request')
    const appEl = document.querySelector('#App') as HTMLElement
    new App({ el: appEl, floor: 5, elevatorNum: 3 })

    const secondFloor = getByText(
      document.body.querySelector('#App') as HTMLElement,
      '5층'
    )
    fireEvent.click(secondFloor)

    expect(requestFunc).toBeCalled()
    expect(requestFunc).toHaveBeenCalledTimes(1)
  })

问题是当getByText调用 TestingLibraryElementError: Found multiple elements with the text: 5층 <- 发生错误消息。

如何刷新文档??

我在下面试过。但它没有用

afterEach(() => {
document.documentElement.innerHTML = ''
})

//and I also tried cleanup from @marko/testing-library
afterEach(cleanup)

4

0 回答 0