ice/packages/rax-compat/tests/createElement.test.tsx

150 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

Feat/define data loader (#643) * feat: modify dataLoader * feat: add type * feat: add DataLoaderConfig * feat: modify getData to defineDataLoader * chore: remove defineDataLoader * fix: fetcher shoule return promise * fix: loader may be undefined * fix: err loader config * chore: modify example of pha * feat: modify getData * chore: modify type and mofiy options of init * feat: modify fetcher to dataLoaderFetcher and add dataLoaderImport to entry * chore: modify defaultDataLoaderFetcher * chore: load data by route id * feat: modify serverDataLoader and staticDataLoader * feat: add fetcher when route change * fix: deal with window undefined * chore: modify type * feat: try get data from cache * feat: support useData * feat: add defineStaticDataLoader and defineServerDataLoader * chore: modify getData of example * fix: should load data * fix: fix ssg err * fix: fix dataloader for ssg * test: modify env of test * fix: shoule clear cache when route changed * fix: fix renderMode and add defult remder mode * fix: add window * test: add jsdom * test: modify getData to dataLoader * test: modify test of clientApp * test: clear window after each test * test: remove only * test: remove only * chore: modify appear * fix: Try get data from cache when CSR * chore: update lock * chore: remove unused * refactor: data loader (#685) * refactor: set global fetcher * refactor: set global fetcher * fix: should not build react in data loader * fix: test * test: modify test Co-authored-by: 水澜 <shuilan.cj@taobao.com> Co-authored-by: ZeroLing <i@zeroling.com>
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';
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,
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();
},
}, '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',
{
onAppear: appearFun,
2022-07-04 11:24:05 +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-07-04 12:05:32 +08:00
let disappearFun = vi.spyOn({
func: func,
}, 'func');
2022-07-04 12:05:32 +08:00
const str = 'hello world';
render(createElement(
'div',
{
onDisappear: func,
2022-07-04 12:05:32 +08:00
},
str,
2022-07-04 12:05:32 +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: {
width: '300rpx',
},
2022-07-04 12:05:32 +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,
},
));
const node = wrapper.queryByTestId('valueTest') as HTMLInputElement;
2022-08-10 16:41:02 +08:00
expect(node.value).toBe(str);
});
it('input set empty string to maxlength not be 0', () => {
const str = 'hello world';
const wrapper = render(createElement(
'input',
{
'data-testid': 'maxlengthTest',
value: str,
maxlength: '',
},
));
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
});