vitest/examples/react/test/basic.test.tsx
2022-01-04 01:33:51 +08:00

32 lines
825 B
TypeScript

import React from 'react'
import renderer from 'react-test-renderer'
import Link from '../components/Link'
function toJson(component: renderer.ReactTestRenderer) {
const result = component.toJSON()
expect(result).toBeDefined()
expect(result).not.toBeInstanceOf(Array)
return result as renderer.ReactTestRendererJSON
}
test('Link changes the class when hovered', () => {
const component = renderer.create(
<Link page="http://antfu.me">Anthony Fu</Link>,
)
let tree = toJson(component)
expect(tree).toMatchSnapshot()
// manually trigger the callback
tree.props.onMouseEnter()
// re-rendering
tree = toJson(component)
expect(tree).toMatchSnapshot()
// manually trigger the callback
tree.props.onMouseLeave()
// re-rendering
tree = toJson(component)
expect(tree).toMatchSnapshot()
})