2022-11-14 15:59:22 +08:00
|
|
|
/**
|
|
|
|
* @vitest-environment jsdom
|
|
|
|
*/
|
|
|
|
|
2022-06-27 12:10:13 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { expect, it, describe } from 'vitest';
|
|
|
|
import { render } from '@testing-library/react';
|
2022-09-07 14:49:54 +08:00
|
|
|
import { Component, PureComponent, memo } from '../src/index';
|
2022-06-27 12:10:13 +08:00
|
|
|
|
|
|
|
describe('component', () => {
|
|
|
|
it('Component should work', () => {
|
|
|
|
class MyComponent extends Component {
|
|
|
|
render() {
|
|
|
|
return <div data-testid="componentId" >my component</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = render(<MyComponent />);
|
|
|
|
const node = wrapper.queryByTestId('componentId');
|
|
|
|
|
|
|
|
expect(node.textContent).toBe('my component');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('PureComponent should work', () => {
|
|
|
|
class MyComponent extends PureComponent {
|
|
|
|
render() {
|
|
|
|
return <div data-testid="pureComponentId" >my component</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = render(<MyComponent />);
|
|
|
|
const node = wrapper.queryByTestId('pureComponentId');
|
|
|
|
|
|
|
|
expect(node.textContent).toBe('my component');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('memo should work', () => {
|
2022-09-07 14:49:54 +08:00
|
|
|
const MyComponent = memo((props: { text: string; id: string }) => {
|
|
|
|
return <div id={props.id}>{props.text}</div>;
|
2022-06-27 12:10:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const wrapper = render(<MyComponent id="" text="memo demo" />);
|
|
|
|
const res = wrapper.queryAllByText('memo demo');
|
|
|
|
|
|
|
|
expect(res.length).toBe(1);
|
|
|
|
});
|
|
|
|
});
|