amis2/packages/amis/__tests__/renderers/Each.test.tsx

126 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* Each
*
* 1. 使
* 2. index
* 3. 使
* 4. Form
*/
import React from 'react';
import {render} from '@testing-library/react';
2022-06-02 10:00:09 +08:00
import '../../src';
import {render as amisRender} from '../../src';
import {makeEnv} from '../helper';
2019-07-04 16:22:24 +08:00
test('Renderer:Each basic use', async () => {
const {container} = render(
amisRender({
type: 'page',
data: {
arr: ['A', 'B', 'C']
},
body: {
2019-07-04 16:22:24 +08:00
type: 'each',
name: 'arr',
2019-07-04 16:22:24 +08:00
items: {
type: 'tpl',
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']
},
body: [
{
type: 'each',
label: '静态展示each',
name: 'each',
items: {
type: 'tpl',
tpl: "<span class='label label-info m-l-sm'><%= this.item %></span>"
}
}
]
})
);
2019-07-04 16:22:24 +08:00
expect(container.querySelector('.cxd-Form .cxd-Each'));
expect(container).toMatchSnapshot();
});