amis/packages/amis-core/__tests__/compat.test.ts

643 lines
10 KiB
TypeScript
Raw Normal View History

import moment from 'moment';
2022-06-01 15:06:00 +08:00
import {
resolveVariable,
resolveVariableAndFilter
} from '../src/utils/tpl-builtin';
2019-04-30 11:11:25 +08:00
2019-06-05 15:49:29 +08:00
const filters = [
2020-05-11 16:55:18 +08:00
{
type: 'raw1',
data: {
value: 1
},
path: '${value}',
filter: '| raw',
expectValue: 1
},
{
type: 'raw2',
data: {
value: '2'
},
path: '${value}',
filter: '| raw',
expectValue: '2'
},
{
type: 'raw3',
data: {
a: 1,
b: '2',
c: {
'1': 'first',
'2': 'second'
}
},
path: '${c.${a}}',
filter: '| raw',
expectValue: 'first'
},
{
type: 'raw4',
data: {
a: 1,
b: '2',
c: {
'1': 'first',
'2': 'second'
}
},
path: '${c.${b}}',
filter: '| raw',
expectValue: 'second'
},
{
type: 'raw5',
data: {
a: 1
},
path: '',
filter: '| raw',
expectValue: undefined
},
{
type: 'raw6',
data: {
a: 1
},
path: '$$',
2022-06-01 15:06:00 +08:00
filter: 'raw',
expectValue: {a: 1}
2020-05-11 16:55:18 +08:00
},
{
type: 'raw7',
data: {
a: 1
},
path: '$a',
filter: '| raw',
expectValue: 1
},
{
type: 'json',
data: {
value: {
a: 'a',
b: 'b'
}
},
2022-06-01 15:06:00 +08:00
path: '${value | json:0}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '{"a":"a","b":"b"}'
},
{
type: 'toJson',
data: {
value: '{"a":"a","b":"b"}'
},
2022-06-01 15:06:00 +08:00
path: '${value | toJson}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: {
a: 'a',
b: 'b'
}
},
{
type: 'date',
data: {
value: 1559649981
},
2022-06-01 15:06:00 +08:00
path: '${value | date}',
filter: '',
expectValue: moment(1559649981, 'X').format('LLL')
2020-05-11 16:55:18 +08:00
},
{
type: 'number',
data: {
value: 9999
},
path: '${value| number}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '9,999'
},
{
type: 'trim',
data: {
value: ' abc '
},
path: '${value| trim}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'abc'
},
{
type: 'percent',
data: {
value: 0.8232343
},
path: '${value| percent}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '82%'
},
// duration
{
type: 'duration1',
data: {
value: 1
},
path: '${value| duration}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1秒'
},
{
type: 'duration2',
data: {
value: 61
},
path: '${value| duration}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1分1秒'
},
{
type: 'duration3',
data: {
value: 233233
},
path: '${value| duration}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '2天16时47分13秒'
},
// bytes
{
type: 'bytes1',
data: {
value: 1024
},
path: '${value| bytes}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1.02 KB'
},
{
type: 'bytes2',
data: {
value: 1024000
},
path: '${value| bytes}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1.02 MB'
},
{
type: 'bytes3',
data: {
value: -1024
},
path: '${value| bytes}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '-1.02 KB'
},
{
type: 'bytes4',
data: {
value: 0.5
},
path: '${value| bytes}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '0.5 B'
},
// round
{
type: 'round1',
data: {
value: '啥啊'
},
path: '${value| round}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 0
},
{
type: 'round2',
data: {
value: 1.22
},
path: '${value| round:1}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1.2'
},
{
type: 'round3',
data: {
value: 1.26
},
path: '${value| round:1}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1.3'
},
{
type: 'truncate1',
data: {
value: 'this is a very loooooong sentence.'
},
path: '${value| truncate:10}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'this is a ...'
},
{
type: 'truncate2',
data: {
value: 'this is a very loooooong sentence.'
},
path: '${value| truncate:null}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'this is a very loooooong sentence.'
},
{
type: 'url_encode',
data: {
value: 'http://www.baidu.com?query=123'
},
path: '${value| url_encode}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'http%3A%2F%2Fwww.baidu.com%3Fquery%3D123'
},
{
type: 'url_decode',
data: {
value: 'http%3A%2F%2Fwww.baidu.com%3Fquery%3D123'
},
path: '${value| url_decode:10}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'http://www.baidu.com?query=123'
},
{
type: 'default1',
data: {
value: ''
},
path: '${value| default}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: undefined
},
{
type: 'default2',
data: {
value: ''
},
path: '${value| default:-}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '-'
},
{
type: 'join',
data: {
value: ['a', 'b', 'c']
},
path: '${value| join:,}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'a,b,c'
},
{
type: 'split',
data: {
value: 'a,b,c'
},
path: '${value| split}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: ['a', 'b', 'c']
},
{
type: 'first',
data: {
value: ['a', 'b', 'c']
},
path: '${value| first}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'a'
},
{
type: 'nth',
data: {
value: ['a', 'b', 'c']
},
path: '${value| nth:1}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'b'
},
{
type: 'last',
data: {
value: ['a', 'b', 'c']
},
path: '${value| last}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'c'
},
{
type: 'minus',
data: {
value: 5
},
path: '${value| minus:1}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 4
},
{
type: 'plus',
data: {
value: 5
},
path: '${value| plus:1}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 6
},
{
type: 'pick1',
data: {
value: {
a: '1',
b: '2'
}
},
path: '${value| pick:a}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: '1'
},
{
type: 'pick2',
data: {
value: [
{
label: 'A',
value: 'a'
},
{
label: 'B',
value: 'b'
},
{
label: 'C',
value: 'c'
2019-06-05 15:49:29 +08:00
}
2020-05-11 16:55:18 +08:00
]
},
path: '${value| pick:value}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: ['a', 'b', 'c']
},
{
type: 'pick_if_exist',
data: {
value: [
{
label: 'A',
value: 'a'
},
{
label: 'B',
value: 'b'
},
{
label: 'C',
value: 'c'
}
]
},
path: '${value| pick_if_exist:value}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: ['a', 'b', 'c']
},
{
type: 'str2date',
data: {
value: '1559649981'
},
path: '${value| str2date:X:YYYY-MM-DD HH-mm-ss}',
filter: '',
expectValue: moment('1559649981', 'X').format('YYYY-MM-DD HH-mm-ss')
2020-05-11 16:55:18 +08:00
},
{
type: 'asArray',
data: {
value: 'a'
},
path: '${value| asArray}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: ['a']
},
{
type: 'base64Encode',
data: {
value: 'I love amis'
},
path: '${value| base64Encode}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'SSBsb3ZlIGFtaXM='
},
{
type: 'base64Decode',
data: {
value: 'SSBsb3ZlIGFtaXM='
},
path: '${value| base64Decode}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'I love amis'
},
{
type: 'lowerCase',
data: {
value: 'AbC'
},
path: '${value| lowerCase}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'abc'
},
{
type: 'upperCase',
data: {
value: 'aBc'
},
path: '${value| upperCase}',
filter: '',
2020-05-11 16:55:18 +08:00
expectValue: 'ABC'
}
];
2019-04-30 11:11:25 +08:00
2019-06-05 15:49:29 +08:00
filters.forEach(f => {
2022-06-01 15:06:00 +08:00
test(`compat:${f.type}`, () => {
const result = resolveVariableAndFilter(f.path, f.data, f.filter);
expect(result).toEqual(f.expectValue);
2020-05-11 16:55:18 +08:00
});
2019-04-30 11:11:25 +08:00
});
2022-06-01 15:06:00 +08:00
test(`compat:filter`, () => {
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${rows | filter:engine:match:keywords}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
rows: [
{
engine: 'a'
},
{
engine: 'b'
},
{
engine: 'c'
}
]
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toMatchObject([
{
engine: 'a'
},
{
engine: 'b'
},
{
engine: 'c'
2020-05-11 16:55:18 +08:00
}
2022-06-01 15:06:00 +08:00
]);
2019-06-05 15:49:29 +08:00
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${rows | filter:engine:match:keywords}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
keywords: 'a',
rows: [
{
engine: 'a'
},
{
engine: 'b'
},
{
engine: 'c'
}
]
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toMatchObject([
{
engine: 'a'
}
]);
});
2019-06-05 15:49:29 +08:00
2022-06-01 15:06:00 +08:00
test(`compat:&`, () => {
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${& | json:0}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: 1,
b: 2
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toBe('{"a":1,"b":2}');
});
2019-06-05 15:49:29 +08:00
2022-06-01 15:06:00 +08:00
test(`compat:filter-default`, () => {
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${a | default:undefined}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: 1
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toBe(1);
2019-06-05 15:49:29 +08:00
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${a | default:undefined}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: [1, 2, 3]
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toMatchObject([1, 2, 3]);
2019-06-05 15:49:29 +08:00
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${b | default:undefined}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: 1
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toBe(undefined);
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${b | default:-}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: 1
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toBe('-');
2019-06-05 15:49:29 +08:00
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${b | default:undefined}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: 1
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw',
() => ''
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toBe(undefined);
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'${b}',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
a: 1
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw',
() => ''
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toBe('');
});
2019-06-05 15:49:29 +08:00
2022-06-01 15:06:00 +08:00
test(`compat:numberVariable`, () => {
expect(
resolveVariableAndFilter(
'a $1 ',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
'1': 233
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
)
).toEqual('a 233 ');
2019-06-05 15:49:29 +08:00
2020-05-11 16:55:18 +08:00
expect(
2022-06-01 15:06:00 +08:00
resolveVariableAndFilter(
'a $1',
2020-05-11 16:55:18 +08:00
{
2022-06-01 15:06:00 +08:00
'1': 233
2020-05-11 16:55:18 +08:00
},
2022-06-01 15:06:00 +08:00
'| raw'
2020-05-11 16:55:18 +08:00
)
2022-06-01 15:06:00 +08:00
).toEqual('a 233');
});
test(`compat:test`, () => {
const result = resolveVariableAndFilter('', {}, '| raw');
expect(result).toEqual(undefined);
});
test(`compat:test2`, () => {
const data = {
'123': 123,
'123.123': 123,
'中文': 123,
'obj': {
x: 123
}
};
expect(resolveVariable('123', data)).toEqual(123);
expect(resolveVariable('123.123', data)).toEqual(123);
expect(resolveVariable('中文', data)).toEqual(123);
expect(resolveVariable('obj.x', data)).toEqual(123);
2020-05-11 16:55:18 +08:00
});