• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kentcdodds/dom-testing-library-with-anything: Use DOM Testing Library to test an ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

kentcdodds/dom-testing-library-with-anything

开源软件地址(OpenSource Url):

https://github.com/kentcdodds/dom-testing-library-with-anything

开源编程语言(OpenSource Language):

JavaScript 82.5%

开源软件介绍(OpenSource Introduction):

Use DOM Testing Library to test any JS framework


Anything you can render to the DOM, you can test with DOM Testing Library.

This repo is a bunch of simple examples of using DOM Testing Library to test a Counter component in various frameworks. If your framework of choice is not listed here, please add it!

Contributing

NOTE: In the interest of full disclosure, this repository will be used by me to create a course on testing for which I will be paid.

The prime example is this react version:

// adds handy assertions we'll use
import '@testing-library/jest-dom/extend-expect'

// framework imports
import * as React from 'react'
import ReactDOM from 'react-dom'

// DOM Testing Library utilities
// note: if your framework does not apply updates to the DOM synchronously
// then you can use the userEventAsync export in ./user-event-async.js
// see hyperapp.test.js for an example of this.
import {getQueriesForElement} from '@testing-library/dom'
import userEvent from '@testing-library/user-event'

// the component in your framework
function Counter() {
  const [count, setCount] = React.useState(0)
  const increment = () => setCount(c => c + 1)
  return (
    <div>
      <button onClick={increment}>{count}</button>
    </div>
  )
}

// a generic "render" method that you could use for any component for
// your framework
function render(ui) {
  const container = document.createElement('div')
  document.body.appendChild(container)
  ReactDOM.render(ui, container)
  return {
    container,
    ...getQueriesForElement(container),
  }
}

// the test.
// This test _should_ look almost identical between each framework
// that's the idea that I'm trying to get across in this repo!
test('renders a counter', () => {
  const {getByText} = render(<Counter />)
  const counter = getByText('0')
  userEvent.click(counter)
  expect(counter).toHaveTextContent('1')

  userEvent.click(counter)
  expect(counter).toHaveTextContent('2')
})

If you can make your example resemble that, I would be thrilled :)

IMPORTANT Notes

I want to keep things as simple as possible, but I also want to be true to what's typical for a given framework. If your framework strongly encourages the use of TypeScript for example, then please feel free to use TypeScript (Jest should already be configured to pick it up properly).

If Jest is not the testing framework of choice for your web framework, I'd still prefer to stick with Jest. Hopefully it shouldn't make much of a difference for the test itself.

Try really hard to keep everything in a single file, even if that means authoring your component in a slightly non-typical way.

This project is setup with prettier, husky, and lint-staged. That means that when you commit, a git commit hook will automatically format the files you're changing and run the tests relevant to those files. Neat right?

LICENSE

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
kevin-wayne/algs4: Algorithms, 4th edition textbook code and libraries发布时间:2022-08-15
下一篇:
adafruit/Adafruit-RGB-LCD-Shield-Library发布时间:2022-08-15
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap