2022-11-14 15:59:22 +08:00
|
|
|
/**
|
|
|
|
|
* @vitest-environment jsdom
|
|
|
|
|
*/
|
|
|
|
|
|
2022-07-04 11:24:05 +08:00
|
|
|
import { expect, it, describe, vi } from 'vitest';
|
2022-06-27 17:45:22 +08:00
|
|
|
import { render } from '@testing-library/react';
|
2022-09-07 14:49:54 +08:00
|
|
|
import { createElement } from '../src/index';
|
2022-06-27 17:45:22 +08:00
|
|
|
|
|
|
|
|
describe('createElement', () => {
|
|
|
|
|
it('basic', () => {
|
2022-06-28 14:40:55 +08:00
|
|
|
const str = 'hello world';
|
|
|
|
|
const wrapper = render(createElement(
|
|
|
|
|
'div',
|
|
|
|
|
null,
|
2022-09-07 14:49:54 +08:00
|
|
|
str,
|
2022-06-28 14:40:55 +08:00
|
|
|
));
|
|
|
|
|
expect(wrapper.container.childNodes[0].textContent).toBe(str);
|
2022-06-27 17:45:22 +08:00
|
|
|
});
|
2022-07-04 11:24:05 +08:00
|
|
|
|
2022-07-04 12:05:32 +08:00
|
|
|
it('should work with onAppear', () => {
|
2022-07-04 11:24:05 +08:00
|
|
|
let appearFun = vi.spyOn({
|
|
|
|
|
func: () => {
|
|
|
|
|
expect(appearFun).toHaveBeenCalled();
|
2022-09-07 14:49:54 +08:00
|
|
|
},
|
|
|
|
|
}, 'func');
|
2022-07-04 11:24:05 +08:00
|
|
|
const str = 'hello world';
|
2022-07-04 12:05:32 +08:00
|
|
|
render(createElement(
|
2022-07-04 11:24:05 +08:00
|
|
|
'div',
|
|
|
|
|
{
|
2022-09-07 14:49:54 +08:00
|
|
|
onAppear: appearFun,
|
2022-07-04 11:24:05 +08:00
|
|
|
},
|
2022-09-07 14:49:54 +08:00
|
|
|
str,
|
2022-07-04 11:24:05 +08:00
|
|
|
));
|
|
|
|
|
});
|
2022-07-04 12:05:32 +08:00
|
|
|
|
|
|
|
|
it('should work with onDisappear', () => {
|
|
|
|
|
const func = () => {
|
|
|
|
|
expect(disappearFun).toHaveBeenCalled();
|
2022-09-07 14:49:54 +08:00
|
|
|
};
|
2022-07-04 12:05:32 +08:00
|
|
|
let disappearFun = vi.spyOn({
|
2022-09-07 14:49:54 +08:00
|
|
|
func: func,
|
|
|
|
|
}, 'func');
|
2022-07-04 12:05:32 +08:00
|
|
|
const str = 'hello world';
|
|
|
|
|
render(createElement(
|
|
|
|
|
'div',
|
|
|
|
|
{
|
2022-07-14 09:56:56 +08:00
|
|
|
onDisappear: func,
|
2022-07-04 12:05:32 +08:00
|
|
|
},
|
2022-09-07 14:49:54 +08:00
|
|
|
str,
|
2022-07-04 12:05:32 +08:00
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-20 14:08:06 +08:00
|
|
|
it('component should not work with onAppear', () => {
|
|
|
|
|
let appearFun = vi.spyOn({
|
|
|
|
|
func: () => {
|
|
|
|
|
expect(appearFun).toBeCalledTimes(0);
|
|
|
|
|
},
|
|
|
|
|
}, 'func');
|
|
|
|
|
const str = 'hello world';
|
|
|
|
|
|
|
|
|
|
const FunctionComponent = ({ onDisappear }) => {
|
|
|
|
|
expect(typeof onDisappear).toBe('function');
|
|
|
|
|
return createElement('div');
|
|
|
|
|
};
|
|
|
|
|
render(createElement(
|
|
|
|
|
FunctionComponent,
|
|
|
|
|
{
|
|
|
|
|
onDisappear: appearFun,
|
|
|
|
|
},
|
|
|
|
|
str,
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-04 12:05:32 +08:00
|
|
|
it('rpx should transform to vw', () => {
|
|
|
|
|
const str = 'hello world';
|
|
|
|
|
const wrapper = render(createElement(
|
|
|
|
|
'div',
|
|
|
|
|
{
|
2022-08-10 17:59:40 +08:00
|
|
|
'data-testid': 'rpxTest',
|
2022-07-04 12:05:32 +08:00
|
|
|
style: {
|
2022-09-07 14:49:54 +08:00
|
|
|
width: '300rpx',
|
|
|
|
|
},
|
2022-07-04 12:05:32 +08:00
|
|
|
},
|
2022-09-07 14:49:54 +08:00
|
|
|
str,
|
2022-07-04 12:05:32 +08:00
|
|
|
));
|
|
|
|
|
|
2022-08-10 17:59:40 +08:00
|
|
|
const node = wrapper.queryByTestId('rpxTest');
|
2022-07-04 12:05:32 +08:00
|
|
|
expect(node.style.width).toBe('40vw');
|
|
|
|
|
});
|
2022-08-10 16:41:02 +08:00
|
|
|
|
|
|
|
|
it('should work with value', () => {
|
|
|
|
|
const str = 'hello world';
|
|
|
|
|
const wrapper = render(createElement(
|
|
|
|
|
'input',
|
|
|
|
|
{
|
2022-08-10 17:59:40 +08:00
|
|
|
'data-testid': 'valueTest',
|
2022-08-10 16:41:02 +08:00
|
|
|
value: str,
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
2023-02-02 15:12:21 +08:00
|
|
|
const node = wrapper.queryByTestId('valueTest') as HTMLInputElement;
|
2022-08-10 16:41:02 +08:00
|
|
|
expect(node.value).toBe(str);
|
|
|
|
|
});
|
2022-08-17 15:13:46 +08:00
|
|
|
|
|
|
|
|
it('input set empty string to maxlength not be 0', () => {
|
|
|
|
|
const str = 'hello world';
|
|
|
|
|
const wrapper = render(createElement(
|
|
|
|
|
'input',
|
|
|
|
|
{
|
|
|
|
|
'data-testid': 'maxlengthTest',
|
|
|
|
|
value: str,
|
2022-09-07 14:49:54 +08:00
|
|
|
maxlength: '',
|
2022-08-17 15:13:46 +08:00
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
const node = wrapper.queryByTestId('valueTest');
|
|
|
|
|
expect(node?.getAttribute('maxlength')).toBe(null);
|
|
|
|
|
});
|
2024-01-02 17:06:20 +08:00
|
|
|
|
|
|
|
|
it('should normalize array type style', () => {
|
|
|
|
|
const str = 'hello world';
|
|
|
|
|
const wrapper = render(
|
|
|
|
|
createElement(
|
|
|
|
|
'div',
|
|
|
|
|
{
|
|
|
|
|
'data-testid': 'styleTest',
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
style: [
|
|
|
|
|
{
|
|
|
|
|
width: '300rpx',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
height: '300rpx',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
padding: '300rpx',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
str,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const node = wrapper.queryByTestId('styleTest');
|
|
|
|
|
expect(node?.style.width).toBe('40vw');
|
|
|
|
|
expect(node?.style.height).toBe('40vw');
|
|
|
|
|
expect(node?.style.padding).toBe('40vw');
|
|
|
|
|
});
|
2022-06-27 17:45:22 +08:00
|
|
|
});
|