2023-01-30 20:49:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 组件名称:Each 循环渲染器
|
|
|
|
|
* 单测内容:
|
|
|
|
|
* 1. 基础使用
|
|
|
|
|
* 2. 对象数组的值;获取其index
|
|
|
|
|
* 3. 使用数据映射
|
|
|
|
|
* 4. Form 中静态展示
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-06-03 13:05:35 +08:00
|
|
|
|
import {render} from '@testing-library/react';
|
2022-06-02 10:00:09 +08:00
|
|
|
|
import '../../src';
|
|
|
|
|
import {render as amisRender} from '../../src';
|
2021-06-03 13:05:35 +08:00
|
|
|
|
import {makeEnv} from '../helper';
|
2019-07-04 16:22:24 +08:00
|
|
|
|
|
2023-01-30 20:49:19 +08:00
|
|
|
|
test('Renderer:Each basic use', async () => {
|
2021-06-03 13:05:35 +08:00
|
|
|
|
const {container} = render(
|
2023-01-30 20:49:19 +08:00
|
|
|
|
amisRender({
|
|
|
|
|
type: 'page',
|
|
|
|
|
data: {
|
|
|
|
|
arr: ['A', 'B', 'C']
|
|
|
|
|
},
|
|
|
|
|
body: {
|
2019-07-04 16:22:24 +08:00
|
|
|
|
type: 'each',
|
2023-01-30 20:49:19 +08:00
|
|
|
|
name: 'arr',
|
2019-07-04 16:22:24 +08:00
|
|
|
|
items: {
|
2021-06-03 13:05:35 +08:00
|
|
|
|
type: 'tpl',
|
2023-01-30 20:49:19 +08:00
|
|
|
|
tpl: "<span class='label label-default m-l-sm'><%= data.item %></span> "
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const items = container.querySelectorAll('.cxd-Each .cxd-TplField');
|
|
|
|
|
|
|
|
|
|
expect(items!.length).toBe(3);
|
|
|
|
|
expect(items[0]).toHaveTextContent('A');
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Renderer:Each with array of objects value', async () => {
|
|
|
|
|
const {container} = render(
|
|
|
|
|
amisRender({
|
|
|
|
|
type: 'each',
|
|
|
|
|
items: {
|
|
|
|
|
type: 'tpl',
|
|
|
|
|
tpl: "<span class='label label-default m-l-sm'><%= data.name %>:<%= data.index %></span> "
|
|
|
|
|
},
|
|
|
|
|
value: [{name: 'jack'}, {name: 'martin'}],
|
|
|
|
|
className: 'show'
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
const items = container.querySelectorAll('.cxd-Each .cxd-TplField');
|
|
|
|
|
|
|
|
|
|
expect(items!.length).toBe(2);
|
|
|
|
|
expect(items[1]).toHaveTextContent('martin:1');
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Renderer:Each with source', async () => {
|
|
|
|
|
const {container} = render(
|
|
|
|
|
amisRender({
|
|
|
|
|
type: 'page',
|
|
|
|
|
data: {
|
|
|
|
|
arr: [
|
|
|
|
|
{
|
|
|
|
|
name: 'jerry',
|
|
|
|
|
age: 21
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'tom',
|
|
|
|
|
age: 29
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
body: {
|
|
|
|
|
type: 'each',
|
|
|
|
|
source: '${arr}',
|
|
|
|
|
items: {
|
|
|
|
|
type: 'wrapper',
|
|
|
|
|
body: [
|
|
|
|
|
{
|
|
|
|
|
type: 'tpl',
|
|
|
|
|
tpl: "<span class='label label-default m-l-sm'>name: <%= data.name %></span> "
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: 'tag',
|
|
|
|
|
label: 'age: ${age}'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
const items = container.querySelectorAll('.cxd-Each .cxd-Wrapper');
|
|
|
|
|
|
|
|
|
|
expect(items!.length).toBe(2);
|
|
|
|
|
expect(items[1]).toHaveTextContent('age: 29');
|
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Renderer:Each in form', async () => {
|
|
|
|
|
const {container} = render(
|
|
|
|
|
amisRender({
|
|
|
|
|
type: 'form',
|
|
|
|
|
data: {
|
|
|
|
|
each: ['A', 'B', 'C']
|
2021-06-03 13:05:35 +08:00
|
|
|
|
},
|
2023-01-30 20:49:19 +08:00
|
|
|
|
body: [
|
|
|
|
|
{
|
|
|
|
|
type: 'each',
|
|
|
|
|
label: '静态展示each',
|
|
|
|
|
name: 'each',
|
|
|
|
|
items: {
|
|
|
|
|
type: 'tpl',
|
|
|
|
|
tpl: "<span class='label label-info m-l-sm'><%= this.item %></span>"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
2021-06-03 13:05:35 +08:00
|
|
|
|
);
|
2019-07-04 16:22:24 +08:00
|
|
|
|
|
2023-01-30 20:49:19 +08:00
|
|
|
|
expect(container.querySelector('.cxd-Form .cxd-Each'));
|
2021-06-03 13:05:35 +08:00
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
});
|