vendor: amis 6.13.0 base (fork root)
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`factory custom not found 2! 1`] = `
|
||||
<div>
|
||||
<div>
|
||||
Not Found
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`factory custom not found 3! 1`] = `
|
||||
<div>
|
||||
<div>
|
||||
Not Found
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`factory custom not found! 1`] = `
|
||||
<div>
|
||||
<div>
|
||||
Not Found
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`factory load Renderer on need 1`] = `
|
||||
<div>
|
||||
<div>
|
||||
This is Custom Renderer2, a is
|
||||
23
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`factory unregistered Renderer 1`] = `
|
||||
<div>
|
||||
<div
|
||||
class="RuntimeError"
|
||||
>
|
||||
<p>
|
||||
Error: 找不到对应的渲染器
|
||||
</p>
|
||||
<p>
|
||||
Path:
|
||||
my-renderer
|
||||
</p>
|
||||
<pre>
|
||||
<code>
|
||||
{
|
||||
"type": "my-renderer",
|
||||
"a": 23
|
||||
}
|
||||
</code>
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`factory:registerRenderer 1`] = `
|
||||
<div>
|
||||
<div>
|
||||
This is Custom Renderer, a is
|
||||
23
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
403
packages/amis-core/__tests__/async-filter.test.ts
Normal file
403
packages/amis-core/__tests__/async-filter.test.ts
Normal file
@@ -0,0 +1,403 @@
|
||||
import {resolveVariableAndFilterForAsync} from '../src/utils/tpl-builtin';
|
||||
import {evaluateForAsync} from 'amis-formula';
|
||||
|
||||
test(`filter:map`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a | map: toInt}', {
|
||||
a: ['123', '3434']
|
||||
})
|
||||
).toMatchObject([123, 3434]);
|
||||
});
|
||||
test(`filter:html`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a}', {
|
||||
a: '<html>'
|
||||
})
|
||||
).toEqual('<html>');
|
||||
});
|
||||
|
||||
test(`filter:complex`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${`${a}`}', {
|
||||
a: '<html>'
|
||||
})
|
||||
).toEqual('<html>');
|
||||
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a ? a : a}', {
|
||||
a: '<html>'
|
||||
})
|
||||
).toEqual('<html>');
|
||||
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${b.a}', {
|
||||
a: '<html>',
|
||||
b: {
|
||||
a: '<br />'
|
||||
}
|
||||
})
|
||||
).toEqual('<br />');
|
||||
});
|
||||
|
||||
test(`filter:json`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a | json : 0}', {
|
||||
a: {a: 1}
|
||||
})
|
||||
).toEqual('{"a":1}');
|
||||
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a | json : 2}', {
|
||||
a: {a: 1}
|
||||
})
|
||||
).toEqual('{\n "a": 1\n}');
|
||||
});
|
||||
|
||||
test(`filter:toJson`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|toJson}', {
|
||||
a: '{"a":1}'
|
||||
})
|
||||
).toMatchObject({a: 1});
|
||||
});
|
||||
|
||||
test(`filter:toInt`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|toInt}', {
|
||||
a: '233'
|
||||
})
|
||||
).toBe(233);
|
||||
});
|
||||
|
||||
test(`filter:toFloat`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|toFloat}', {
|
||||
a: '233.233'
|
||||
})
|
||||
).toBe(233.233);
|
||||
});
|
||||
|
||||
test(`filter:toDate`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|toDate:x|date: YYYY-MM-DD}', {
|
||||
a: 1638028267226
|
||||
})
|
||||
).toBe('2021-11-27');
|
||||
});
|
||||
|
||||
test(`filter:fromNow`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|toDate:x|fromNow}', {
|
||||
a: Date.now() - 2 * 60 * 1000
|
||||
})
|
||||
).toBe('2 minutes ago');
|
||||
});
|
||||
|
||||
test(`filter:dateModify`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync(
|
||||
'${a|toDate:x|dateModify:subtract:2:m|fromNow}',
|
||||
{
|
||||
a: Date.now()
|
||||
}
|
||||
)
|
||||
).toBe('2 minutes ago');
|
||||
});
|
||||
|
||||
test(`filter:number`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|number}', {
|
||||
a: 1234
|
||||
})
|
||||
).toBe('1,234');
|
||||
});
|
||||
|
||||
test(`filter:trim`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|trim}', {
|
||||
a: ' ab '
|
||||
})
|
||||
).toBe('ab');
|
||||
});
|
||||
|
||||
test(`filter:duration`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|duration}', {
|
||||
a: 234343
|
||||
})
|
||||
).toBe('2天17时5分43秒');
|
||||
});
|
||||
|
||||
test(`filter:bytes`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|bytes}', {
|
||||
a: 234343
|
||||
})
|
||||
).toBe('234 KB');
|
||||
});
|
||||
|
||||
test(`filter:bytes:step`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|bytes:1024}', {
|
||||
a: 234343
|
||||
})
|
||||
).toBe('229 KB');
|
||||
});
|
||||
|
||||
test(`filter:round`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|round}', {
|
||||
a: 23.234
|
||||
})
|
||||
).toBe('23.23');
|
||||
});
|
||||
|
||||
test(`filter:truncate`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|truncate:5}', {
|
||||
a: 'abcdefghijklmnopqrst'
|
||||
})
|
||||
).toBe('abcde...');
|
||||
});
|
||||
|
||||
test(`filter:url_encode`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|url_encode}', {
|
||||
a: '='
|
||||
})
|
||||
).toBe('%3D');
|
||||
});
|
||||
|
||||
describe('filter:url_decode', () => {
|
||||
test(`filter:url_decode:normal`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|url_decode}', {
|
||||
a: '%3D'
|
||||
})
|
||||
).toBe('=');
|
||||
});
|
||||
|
||||
test(`filter:url_decode:error`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|url_decode}', {
|
||||
a: '%'
|
||||
})
|
||||
).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
test(`filter:default`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|default:-}', {
|
||||
a: ''
|
||||
})
|
||||
).toBe('-');
|
||||
});
|
||||
|
||||
test(`filter:join`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|join:-}', {
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toBe('1-2-3');
|
||||
});
|
||||
|
||||
test(`filter:split`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|split:-}', {
|
||||
a: '1-2-3'
|
||||
})
|
||||
).toMatchObject(['1', '2', '3']);
|
||||
});
|
||||
|
||||
test(`filter:sortBy`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|sortBy:&|join}', {
|
||||
a: ['b', 'c', 'a']
|
||||
})
|
||||
).toBe('a,b,c');
|
||||
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|sortBy:&:numerical|join}', {
|
||||
a: ['023', '20', '44']
|
||||
})
|
||||
).toBe('20,023,44');
|
||||
});
|
||||
|
||||
test(`filter:objectToArray`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|objectToArray}', {
|
||||
a: {
|
||||
a: 1,
|
||||
b: 2,
|
||||
done: 'Done'
|
||||
}
|
||||
})
|
||||
).toMatchObject([
|
||||
{
|
||||
value: 'a',
|
||||
label: 1
|
||||
},
|
||||
{
|
||||
value: 'b',
|
||||
label: 2
|
||||
},
|
||||
{
|
||||
value: 'done',
|
||||
label: 'Done'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test(`filter:substring`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|substring:0:2}', {
|
||||
a: 'abcdefg'
|
||||
})
|
||||
).toBe('ab');
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a|substring:1:3}', {
|
||||
a: 'abcdefg'
|
||||
})
|
||||
).toBe('bc');
|
||||
});
|
||||
|
||||
test(`filter:variableInVariable`, async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${a}', {
|
||||
a: 'abc$0defg'
|
||||
})
|
||||
).toBe('abc$0defg');
|
||||
});
|
||||
|
||||
test('filter:isMatch', async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync(
|
||||
'${status | isMatch:2:1|isMatch:5:1:4}',
|
||||
{
|
||||
status: 2
|
||||
}
|
||||
)
|
||||
).toBe(1);
|
||||
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync(
|
||||
'${status | isMatch:2:1|isMatch:5:1:4}',
|
||||
{
|
||||
status: 1
|
||||
}
|
||||
)
|
||||
).toBe(4);
|
||||
});
|
||||
|
||||
test('filter:filter:isMatch', async () => {
|
||||
expect(
|
||||
await resolveVariableAndFilterForAsync('${items|filter:text:match:"ab"}', {
|
||||
items: [
|
||||
{
|
||||
text: 'abc'
|
||||
},
|
||||
{
|
||||
text: 'bcd'
|
||||
},
|
||||
{
|
||||
text: 'cde'
|
||||
}
|
||||
]
|
||||
})
|
||||
).toMatchObject([
|
||||
{
|
||||
text: 'abc'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test('evalute:conditional', async () => {
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isTrue: true : false}',
|
||||
{
|
||||
a: 4
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(true);
|
||||
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isTrue: b : false}',
|
||||
{
|
||||
a: 4,
|
||||
b: 5
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(5);
|
||||
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isTrue: b : false}',
|
||||
{
|
||||
a: null,
|
||||
b: 5
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(false);
|
||||
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isTrue : trueValue : falseValue}',
|
||||
{
|
||||
a: true,
|
||||
trueValue: 5,
|
||||
falseValue: 10
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(5);
|
||||
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isEquals: 1 : "1" |isEquals: 2 : "2" | isEquals: 3 : "3" }',
|
||||
{
|
||||
a: 3
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe('3');
|
||||
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isEquals: 1 : "1" |isEquals: 1 : "2" | isEquals: 1 : "3" }',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe('1');
|
||||
|
||||
expect(
|
||||
await evaluateForAsync(
|
||||
'${a | isEquals: 1 : "1" : "12" |isEquals: 2 : "2" | isEquals: 3 : "3" }',
|
||||
{
|
||||
a: 2
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe('12');
|
||||
});
|
||||
1
packages/amis-core/__tests__/compat.test.d.ts
vendored
Normal file
1
packages/amis-core/__tests__/compat.test.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
660
packages/amis-core/__tests__/compat.test.ts
Normal file
660
packages/amis-core/__tests__/compat.test.ts
Normal file
@@ -0,0 +1,660 @@
|
||||
import moment from 'moment';
|
||||
import {
|
||||
resolveVariable,
|
||||
resolveVariableAndFilter,
|
||||
resolveVariableAndFilterForAsync
|
||||
} from '../src/utils/tpl-builtin';
|
||||
import {setFormulaEvalErrorHandler} from '../src/utils';
|
||||
|
||||
const filters = [
|
||||
{
|
||||
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: '$$',
|
||||
filter: 'raw',
|
||||
expectValue: {a: 1}
|
||||
},
|
||||
{
|
||||
type: 'raw7',
|
||||
data: {
|
||||
a: 1
|
||||
},
|
||||
path: '$a',
|
||||
filter: '| raw',
|
||||
expectValue: 1
|
||||
},
|
||||
{
|
||||
type: 'json',
|
||||
data: {
|
||||
value: {
|
||||
a: 'a',
|
||||
b: 'b'
|
||||
}
|
||||
},
|
||||
path: '${value | json:0}',
|
||||
filter: '',
|
||||
expectValue: '{"a":"a","b":"b"}'
|
||||
},
|
||||
{
|
||||
type: 'toJson',
|
||||
data: {
|
||||
value: '{"a":"a","b":"b"}'
|
||||
},
|
||||
path: '${value | toJson}',
|
||||
filter: '',
|
||||
expectValue: {
|
||||
a: 'a',
|
||||
b: 'b'
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'date',
|
||||
data: {
|
||||
value: 1559649981
|
||||
},
|
||||
path: '${value | date}',
|
||||
filter: '',
|
||||
expectValue: moment(1559649981, 'X').format('LLL')
|
||||
},
|
||||
{
|
||||
type: 'number',
|
||||
data: {
|
||||
value: 9999
|
||||
},
|
||||
path: '${value| number}',
|
||||
filter: '',
|
||||
expectValue: '9,999'
|
||||
},
|
||||
{
|
||||
type: 'trim',
|
||||
data: {
|
||||
value: ' abc '
|
||||
},
|
||||
path: '${value| trim}',
|
||||
filter: '',
|
||||
expectValue: 'abc'
|
||||
},
|
||||
{
|
||||
type: 'percent',
|
||||
data: {
|
||||
value: 0.8232343
|
||||
},
|
||||
path: '${value| percent}',
|
||||
filter: '',
|
||||
expectValue: '82%'
|
||||
},
|
||||
// duration
|
||||
{
|
||||
type: 'duration1',
|
||||
data: {
|
||||
value: 1
|
||||
},
|
||||
path: '${value| duration}',
|
||||
filter: '',
|
||||
expectValue: '1秒'
|
||||
},
|
||||
{
|
||||
type: 'duration2',
|
||||
data: {
|
||||
value: 61
|
||||
},
|
||||
path: '${value| duration}',
|
||||
filter: '',
|
||||
expectValue: '1分1秒'
|
||||
},
|
||||
{
|
||||
type: 'duration3',
|
||||
data: {
|
||||
value: 233233
|
||||
},
|
||||
path: '${value| duration}',
|
||||
filter: '',
|
||||
expectValue: '2天16时47分13秒'
|
||||
},
|
||||
// bytes
|
||||
{
|
||||
type: 'bytes1',
|
||||
data: {
|
||||
value: 1024
|
||||
},
|
||||
path: '${value| bytes}',
|
||||
filter: '',
|
||||
expectValue: '1.02 KB'
|
||||
},
|
||||
{
|
||||
type: 'bytes2',
|
||||
data: {
|
||||
value: 1024000
|
||||
},
|
||||
path: '${value| bytes}',
|
||||
filter: '',
|
||||
expectValue: '1.02 MB'
|
||||
},
|
||||
{
|
||||
type: 'bytes3',
|
||||
data: {
|
||||
value: -1024
|
||||
},
|
||||
path: '${value| bytes}',
|
||||
filter: '',
|
||||
expectValue: '-1.02 KB'
|
||||
},
|
||||
{
|
||||
type: 'bytes4',
|
||||
data: {
|
||||
value: 0.5
|
||||
},
|
||||
path: '${value| bytes}',
|
||||
filter: '',
|
||||
expectValue: '0.5 B'
|
||||
},
|
||||
// round
|
||||
{
|
||||
type: 'round1',
|
||||
data: {
|
||||
value: '啥啊'
|
||||
},
|
||||
path: '${value| round}',
|
||||
filter: '',
|
||||
expectValue: 0
|
||||
},
|
||||
{
|
||||
type: 'round2',
|
||||
data: {
|
||||
value: 1.22
|
||||
},
|
||||
path: '${value| round:1}',
|
||||
filter: '',
|
||||
expectValue: '1.2'
|
||||
},
|
||||
{
|
||||
type: 'round3',
|
||||
data: {
|
||||
value: 1.26
|
||||
},
|
||||
path: '${value| round:1}',
|
||||
filter: '',
|
||||
expectValue: '1.3'
|
||||
},
|
||||
{
|
||||
type: 'truncate1',
|
||||
data: {
|
||||
value: 'this is a very loooooong sentence.'
|
||||
},
|
||||
path: '${value| truncate:10}',
|
||||
filter: '',
|
||||
expectValue: 'this is a ...'
|
||||
},
|
||||
{
|
||||
type: 'truncate2',
|
||||
data: {
|
||||
value: 'this is a very loooooong sentence.'
|
||||
},
|
||||
path: '${value| truncate:null}',
|
||||
filter: '',
|
||||
expectValue: 'this is a very loooooong sentence.'
|
||||
},
|
||||
{
|
||||
type: 'url_encode',
|
||||
data: {
|
||||
value: 'http://www.baidu.com?query=123'
|
||||
},
|
||||
path: '${value| url_encode}',
|
||||
filter: '',
|
||||
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: '',
|
||||
expectValue: 'http://www.baidu.com?query=123'
|
||||
},
|
||||
{
|
||||
type: 'default1',
|
||||
data: {
|
||||
value: ''
|
||||
},
|
||||
path: '${value| default}',
|
||||
filter: '',
|
||||
expectValue: undefined
|
||||
},
|
||||
{
|
||||
type: 'default2',
|
||||
data: {
|
||||
value: ''
|
||||
},
|
||||
path: '${value| default:-}',
|
||||
filter: '',
|
||||
expectValue: '-'
|
||||
},
|
||||
{
|
||||
type: 'join',
|
||||
data: {
|
||||
value: ['a', 'b', 'c']
|
||||
},
|
||||
path: '${value| join:,}',
|
||||
filter: '',
|
||||
expectValue: 'a,b,c'
|
||||
},
|
||||
{
|
||||
type: 'split',
|
||||
data: {
|
||||
value: 'a,b,c'
|
||||
},
|
||||
path: '${value| split}',
|
||||
filter: '',
|
||||
expectValue: ['a', 'b', 'c']
|
||||
},
|
||||
{
|
||||
type: 'first',
|
||||
data: {
|
||||
value: ['a', 'b', 'c']
|
||||
},
|
||||
path: '${value| first}',
|
||||
filter: '',
|
||||
expectValue: 'a'
|
||||
},
|
||||
{
|
||||
type: 'nth',
|
||||
data: {
|
||||
value: ['a', 'b', 'c']
|
||||
},
|
||||
path: '${value| nth:1}',
|
||||
filter: '',
|
||||
expectValue: 'b'
|
||||
},
|
||||
{
|
||||
type: 'last',
|
||||
data: {
|
||||
value: ['a', 'b', 'c']
|
||||
},
|
||||
path: '${value| last}',
|
||||
filter: '',
|
||||
expectValue: 'c'
|
||||
},
|
||||
{
|
||||
type: 'minus',
|
||||
data: {
|
||||
value: 5
|
||||
},
|
||||
path: '${value| minus:1}',
|
||||
filter: '',
|
||||
expectValue: 4
|
||||
},
|
||||
{
|
||||
type: 'plus',
|
||||
data: {
|
||||
value: 5
|
||||
},
|
||||
path: '${value| plus:1}',
|
||||
filter: '',
|
||||
expectValue: 6
|
||||
},
|
||||
{
|
||||
type: 'pick1',
|
||||
data: {
|
||||
value: {
|
||||
a: '1',
|
||||
b: '2'
|
||||
}
|
||||
},
|
||||
path: '${value| pick:a}',
|
||||
filter: '',
|
||||
expectValue: '1'
|
||||
},
|
||||
{
|
||||
type: 'pick2',
|
||||
data: {
|
||||
value: [
|
||||
{
|
||||
label: 'A',
|
||||
value: 'a'
|
||||
},
|
||||
{
|
||||
label: 'B',
|
||||
value: 'b'
|
||||
},
|
||||
{
|
||||
label: 'C',
|
||||
value: 'c'
|
||||
}
|
||||
]
|
||||
},
|
||||
path: '${value| pick:value}',
|
||||
filter: '',
|
||||
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: '',
|
||||
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')
|
||||
},
|
||||
{
|
||||
type: 'asArray',
|
||||
data: {
|
||||
value: 'a'
|
||||
},
|
||||
path: '${value| asArray}',
|
||||
filter: '',
|
||||
expectValue: ['a']
|
||||
},
|
||||
{
|
||||
type: 'base64Encode',
|
||||
data: {
|
||||
value: 'I love amis'
|
||||
},
|
||||
path: '${value| base64Encode}',
|
||||
filter: '',
|
||||
expectValue: 'SSBsb3ZlIGFtaXM='
|
||||
},
|
||||
{
|
||||
type: 'base64Decode',
|
||||
data: {
|
||||
value: 'SSBsb3ZlIGFtaXM='
|
||||
},
|
||||
path: '${value| base64Decode}',
|
||||
filter: '',
|
||||
expectValue: 'I love amis'
|
||||
},
|
||||
{
|
||||
type: 'lowerCase',
|
||||
data: {
|
||||
value: 'AbC'
|
||||
},
|
||||
path: '${value| lowerCase}',
|
||||
filter: '',
|
||||
expectValue: 'abc'
|
||||
},
|
||||
{
|
||||
type: 'upperCase',
|
||||
data: {
|
||||
value: 'aBc'
|
||||
},
|
||||
path: '${value| upperCase}',
|
||||
filter: '',
|
||||
expectValue: 'ABC'
|
||||
}
|
||||
];
|
||||
|
||||
filters.forEach(f => {
|
||||
test(`compat:${f.type}`, () => {
|
||||
const result = resolveVariableAndFilter(f.path, f.data, f.filter);
|
||||
expect(result).toEqual(f.expectValue);
|
||||
});
|
||||
});
|
||||
|
||||
test(`compat:filter`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${rows | filter:engine:match:keywords}',
|
||||
{
|
||||
rows: [
|
||||
{
|
||||
engine: 'a'
|
||||
},
|
||||
{
|
||||
engine: 'b'
|
||||
},
|
||||
{
|
||||
engine: 'c'
|
||||
}
|
||||
]
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toMatchObject([
|
||||
{
|
||||
engine: 'a'
|
||||
},
|
||||
{
|
||||
engine: 'b'
|
||||
},
|
||||
{
|
||||
engine: 'c'
|
||||
}
|
||||
]);
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${rows | filter:engine:match:keywords}',
|
||||
{
|
||||
keywords: 'a',
|
||||
rows: [
|
||||
{
|
||||
engine: 'a'
|
||||
},
|
||||
{
|
||||
engine: 'b'
|
||||
},
|
||||
{
|
||||
engine: 'c'
|
||||
}
|
||||
]
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toMatchObject([
|
||||
{
|
||||
engine: 'a'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test(`compat:&`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${& | json:0}',
|
||||
{
|
||||
a: 1,
|
||||
b: 2
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toBe('{"a":1,"b":2}');
|
||||
});
|
||||
|
||||
test(`compat:filter-default`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${a | default:undefined}',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toBe(1);
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${a | default:undefined}',
|
||||
{
|
||||
a: [1, 2, 3]
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toMatchObject([1, 2, 3]);
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${b | default:undefined}',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toBe(undefined);
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${b | default:-}',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toBe('-');
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${b | default:undefined}',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
'| raw',
|
||||
() => ''
|
||||
)
|
||||
).toBe(undefined);
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'${b}',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
'| raw',
|
||||
() => ''
|
||||
)
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
test(`compat:numberVariable`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'a $1 ',
|
||||
{
|
||||
'1': 233
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).toEqual('a 233 ');
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter(
|
||||
'a $1',
|
||||
{
|
||||
'1': 233
|
||||
},
|
||||
'| raw'
|
||||
)
|
||||
).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);
|
||||
});
|
||||
|
||||
test('compat:formulaEvalErrorHandler', async () => {
|
||||
setFormulaEvalErrorHandler(
|
||||
(
|
||||
path?: string,
|
||||
data: object = {},
|
||||
defaultFilter: string = '| html',
|
||||
fallbackValue = (value: any) => value,
|
||||
skipFormulaEvalErrorHandler: boolean = false
|
||||
) => {
|
||||
return Promise.resolve(1 * 2 * 2);
|
||||
}
|
||||
);
|
||||
|
||||
expect(await resolveVariableAndFilterForAsync('${AAA(1,2)}')).toBe(4);
|
||||
});
|
||||
547
packages/amis-core/__tests__/condition.test.ts
Normal file
547
packages/amis-core/__tests__/condition.test.ts
Normal file
@@ -0,0 +1,547 @@
|
||||
import moment from 'moment';
|
||||
import {
|
||||
resolveConditionAsync,
|
||||
guid,
|
||||
registerConditionComputer,
|
||||
setConditionComputeErrorHandler
|
||||
} from '../src/utils/';
|
||||
|
||||
const data = {
|
||||
name: 'amis',
|
||||
feature: 'flexible',
|
||||
features: ['flexible', 'powerful'],
|
||||
tool: 'amis-editor',
|
||||
platform: 'aisuda',
|
||||
version: '2.9.0',
|
||||
num: 1,
|
||||
num2: 0,
|
||||
num3: undefined,
|
||||
str: '',
|
||||
obj: {},
|
||||
arr: [],
|
||||
bool: true,
|
||||
detail: {
|
||||
version: '2.8.0',
|
||||
github: 'https://github.com/baidu/amis'
|
||||
},
|
||||
range: 1678723200,
|
||||
date: '2023-03-19',
|
||||
datetime: '2023-03-20T04:55:00+08:00',
|
||||
time: '00:05'
|
||||
};
|
||||
|
||||
const equal1 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'name'
|
||||
},
|
||||
op: 'equal',
|
||||
right: '${name}'
|
||||
};
|
||||
const equal2 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'detail'
|
||||
},
|
||||
op: 'equal',
|
||||
right: '${detail}'
|
||||
};
|
||||
const equal3 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'bool'
|
||||
},
|
||||
op: 'equal',
|
||||
right: '${!num2}'
|
||||
};
|
||||
const not_equal = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'version'
|
||||
},
|
||||
op: 'not_equal',
|
||||
right: '${version}'
|
||||
};
|
||||
const greater = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'greater',
|
||||
right: '${num + 0.5}'
|
||||
};
|
||||
const less = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'less',
|
||||
right: '${num - 0.5}'
|
||||
};
|
||||
const greater_or_equal = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'greater_or_equal',
|
||||
right: '${num}'
|
||||
};
|
||||
const less_or_equal = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'less_or_equal',
|
||||
right: '${num}'
|
||||
};
|
||||
const starts_with = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'tool'
|
||||
},
|
||||
op: 'starts_with',
|
||||
right: 'amis-'
|
||||
};
|
||||
const ends_with = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'tool'
|
||||
},
|
||||
op: 'ends_with',
|
||||
right: '-editor'
|
||||
};
|
||||
const like = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'tool'
|
||||
},
|
||||
op: 'like',
|
||||
right: '${tool}-core'
|
||||
};
|
||||
const not_like = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'tool'
|
||||
},
|
||||
op: 'not_like',
|
||||
right: '${tool}r'
|
||||
};
|
||||
const is_empty1 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num2'
|
||||
},
|
||||
op: 'is_empty'
|
||||
};
|
||||
const is_empty2 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'str'
|
||||
},
|
||||
op: 'is_empty'
|
||||
};
|
||||
const is_empty3 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'obj'
|
||||
},
|
||||
op: 'is_empty'
|
||||
};
|
||||
const is_not_empty1 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num3'
|
||||
},
|
||||
op: 'is_not_empty'
|
||||
};
|
||||
const is_not_empty2 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'name'
|
||||
},
|
||||
op: 'is_not_empty'
|
||||
};
|
||||
const is_not_empty3 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'detail'
|
||||
},
|
||||
op: 'is_not_empty'
|
||||
};
|
||||
const is_not_empty4 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'arr'
|
||||
},
|
||||
op: 'is_not_empty'
|
||||
};
|
||||
const between = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'between',
|
||||
right: '${[0.5,1]}'
|
||||
};
|
||||
const not_between = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'range'
|
||||
},
|
||||
op: 'not_between',
|
||||
right: '${[1678636800,1678895999]}'
|
||||
};
|
||||
const select_any_in1 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'features'
|
||||
},
|
||||
op: 'select_any_in',
|
||||
right: '${[LAST(features)]}'
|
||||
};
|
||||
const select_any_in2 = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'feature'
|
||||
},
|
||||
op: 'select_any_in',
|
||||
right: '${features}'
|
||||
};
|
||||
const select_not_any_in = {
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'features'
|
||||
},
|
||||
op: 'select_not_any_in',
|
||||
right: "${['powerful']}"
|
||||
};
|
||||
|
||||
const conditions1 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [equal1, equal2, equal3, not_equal]
|
||||
};
|
||||
|
||||
const conditions2 = {
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [greater, less, greater_or_equal, less_or_equal]
|
||||
};
|
||||
|
||||
const conditions3 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [starts_with, ends_with, like, not_like]
|
||||
};
|
||||
|
||||
const conditions4 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [is_empty1, is_empty2, is_empty3]
|
||||
};
|
||||
|
||||
const conditions5 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [is_not_empty1, is_not_empty2, is_not_empty3, is_not_empty4]
|
||||
};
|
||||
|
||||
const conditions6 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [between, not_between]
|
||||
};
|
||||
|
||||
const conditions7 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [select_any_in1, select_any_in2, select_not_any_in]
|
||||
};
|
||||
|
||||
test(`condition`, async () => {
|
||||
expect(await resolveConditionAsync(conditions1, data)).toBe(false);
|
||||
expect(await resolveConditionAsync(conditions2, data)).toBe(true);
|
||||
expect(await resolveConditionAsync(conditions3, data)).toBe(false);
|
||||
expect(await resolveConditionAsync(conditions4, data)).toBe(false);
|
||||
expect(await resolveConditionAsync(conditions5, data)).toBe(false);
|
||||
expect(await resolveConditionAsync(conditions6, data)).toBe(false);
|
||||
expect(await resolveConditionAsync(conditions7, data)).toBe(false);
|
||||
});
|
||||
|
||||
test(`condition date`, async () => {
|
||||
const conditions = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'date'
|
||||
},
|
||||
op: 'less',
|
||||
right: '2023-03-20'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'datetime'
|
||||
},
|
||||
op: 'less',
|
||||
right: '2023-03-21T04:55:00+08:00'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'time'
|
||||
},
|
||||
op: 'less',
|
||||
right: '00:08'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'date'
|
||||
},
|
||||
op: 'less_or_equal',
|
||||
right: '2023-03-19'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'datetime'
|
||||
},
|
||||
op: 'less_or_equal',
|
||||
right: '2023-03-20T04:55:00+08:00'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'time'
|
||||
},
|
||||
op: 'less_or_equal',
|
||||
right: '00:05'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'date'
|
||||
},
|
||||
op: 'between',
|
||||
right: '${["2023-03-19","2023-03-20"]}'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'datetime'
|
||||
},
|
||||
op: 'between',
|
||||
right: '${["2023-03-20T04:55:00+08:00","2023-03-21T04:55:00+08:00"]}'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'time'
|
||||
},
|
||||
op: 'between',
|
||||
right: '${["00:05","00:06"]}'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(await resolveConditionAsync(conditions, data)).toBe(true);
|
||||
});
|
||||
|
||||
test(`condition tree`, async () => {
|
||||
const conditions8 = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [
|
||||
select_any_in1,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [
|
||||
equal1,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [between, not_between]
|
||||
},
|
||||
between,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [between, select_any_in2, conditions5]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const conditions9 = {
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [
|
||||
is_empty1,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [
|
||||
is_empty1,
|
||||
conditions6,
|
||||
between,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [is_empty1, select_any_in2, conditions5]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
const conditions10 = {
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [
|
||||
conditions6,
|
||||
is_empty1,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [
|
||||
is_empty1,
|
||||
{
|
||||
id: guid(),
|
||||
conjunction: 'or',
|
||||
children: [
|
||||
is_not_empty1,
|
||||
is_not_empty2,
|
||||
is_not_empty3,
|
||||
is_not_empty4
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
is_empty1
|
||||
]
|
||||
};
|
||||
|
||||
expect(await resolveConditionAsync(conditions8, data)).toBe(false);
|
||||
expect(await resolveConditionAsync(conditions9, data)).toBe(true);
|
||||
expect(await resolveConditionAsync(conditions10, data)).toBe(true);
|
||||
});
|
||||
|
||||
test(`condition register`, async () => {
|
||||
registerConditionComputer(
|
||||
'customless',
|
||||
(left: any, right?: any, fieldType?: string) => {
|
||||
if (fieldType === 'date') {
|
||||
return moment(left).isBefore(moment(right), 'day');
|
||||
}
|
||||
return left < right;
|
||||
}
|
||||
);
|
||||
|
||||
const conditions = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'date'
|
||||
},
|
||||
op: 'customless',
|
||||
right: '2023-03-20'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'customless',
|
||||
right: '5'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(await resolveConditionAsync(conditions, data)).toBe(true);
|
||||
});
|
||||
|
||||
test(`condition conditionComputeHander`, async () => {
|
||||
// 无法解析时,自定义解析逻辑
|
||||
setConditionComputeErrorHandler(
|
||||
(conditions: any, data: any, defaultResult: boolean) => {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
);
|
||||
|
||||
const conditions = {
|
||||
id: guid(),
|
||||
conjunction: 'and',
|
||||
children: [
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'date',
|
||||
field: 'date'
|
||||
},
|
||||
op: 'equal',
|
||||
right: '2023-03-19'
|
||||
},
|
||||
{
|
||||
id: guid(),
|
||||
left: {
|
||||
type: 'field',
|
||||
field: 'num'
|
||||
},
|
||||
op: 'equal',
|
||||
right: '${AAA(5)}'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(await resolveConditionAsync(conditions, data)).toBe(true);
|
||||
});
|
||||
33
packages/amis-core/__tests__/datamapping.test.ts
Normal file
33
packages/amis-core/__tests__/datamapping.test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import {dataMapping} from '../src';
|
||||
|
||||
test('datamapping', async () => {
|
||||
const context = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: [
|
||||
{a: 1, b: 1},
|
||||
{a: 2, b: 2}
|
||||
]
|
||||
};
|
||||
|
||||
const result = dataMapping(
|
||||
{
|
||||
a: '${a}',
|
||||
b: '${c}',
|
||||
c: '${ARRAYMAP(c, item => {a: item.a, c: item.b})}'
|
||||
},
|
||||
context
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
a: 1,
|
||||
b: [
|
||||
{a: 1, b: 1},
|
||||
{a: 2, b: 2}
|
||||
],
|
||||
c: [
|
||||
{a: 1, c: 1},
|
||||
{a: 2, c: 2}
|
||||
]
|
||||
});
|
||||
});
|
||||
1
packages/amis-core/__tests__/factory.test.d.ts
vendored
Normal file
1
packages/amis-core/__tests__/factory.test.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
292
packages/amis-core/__tests__/factory.test.tsx
Normal file
292
packages/amis-core/__tests__/factory.test.tsx
Normal file
@@ -0,0 +1,292 @@
|
||||
import {
|
||||
registerRenderer,
|
||||
unRegisterRenderer,
|
||||
RendererProps
|
||||
} from '../src/factory';
|
||||
import {render as amisRender} from '../src';
|
||||
import React from 'react';
|
||||
import {render, fireEvent, waitFor} from '@testing-library/react';
|
||||
import {makeEnv} from './helper';
|
||||
|
||||
test('factory unregistered Renderer', async () => {
|
||||
const {container, getByText} = render(
|
||||
amisRender({
|
||||
type: 'my-renderer',
|
||||
a: 23
|
||||
})
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(getByText('Error: 找不到对应的渲染器')).toBeInTheDocument();
|
||||
});
|
||||
expect(container).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('factory custom not found!', async () => {
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'my-renderer',
|
||||
a: 23
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
loadRenderer: () => Promise.resolve(() => <div>Not Found</div>)
|
||||
})
|
||||
)
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(getByText('Not Found')).toBeInTheDocument();
|
||||
});
|
||||
expect(container).toMatchSnapshot(); // not found
|
||||
});
|
||||
|
||||
test('factory custom not found 2!', async () => {
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'my-renderer',
|
||||
a: 23
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
loadRenderer: () => () => <div>Not Found</div>
|
||||
})
|
||||
)
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(getByText('Not Found')).toBeInTheDocument();
|
||||
});
|
||||
expect(container).toMatchSnapshot(); // not found
|
||||
});
|
||||
|
||||
test('factory custom not found 3!', async () => {
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'my-renderer',
|
||||
a: 23
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
loadRenderer: () => <div>Not Found</div>
|
||||
})
|
||||
)
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(getByText('Not Found')).toBeInTheDocument();
|
||||
});
|
||||
expect(container).toMatchSnapshot(); // not found
|
||||
});
|
||||
|
||||
test('factory load Renderer on need', async () => {
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'my-renderer2',
|
||||
a: 23
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
session: 'loadRenderer',
|
||||
loadRenderer: schema => {
|
||||
interface MyProps extends RendererProps {
|
||||
a?: number;
|
||||
}
|
||||
|
||||
class MyComponent extends React.Component<MyProps> {
|
||||
render() {
|
||||
return <div>This is Custom Renderer2, a is {this.props.a}</div>;
|
||||
}
|
||||
}
|
||||
|
||||
registerRenderer({
|
||||
component: MyComponent,
|
||||
test: /\bmy-renderer2$/
|
||||
});
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(getByText('This is Custom Renderer2, a is 23')).toBeInTheDocument();
|
||||
});
|
||||
expect(container).toMatchSnapshot(); // not found
|
||||
});
|
||||
|
||||
test('factory:registerRenderer', () => {
|
||||
interface MyProps extends RendererProps {
|
||||
a?: number;
|
||||
}
|
||||
|
||||
class MyComponent extends React.Component<MyProps> {
|
||||
render() {
|
||||
return <div>This is Custom Renderer, a is {this.props.a}</div>;
|
||||
}
|
||||
}
|
||||
|
||||
const renderer = registerRenderer({
|
||||
component: MyComponent,
|
||||
test: /\bmy-renderer$/
|
||||
});
|
||||
|
||||
const {container} = render(
|
||||
amisRender({
|
||||
type: 'my-renderer',
|
||||
a: 23
|
||||
})
|
||||
);
|
||||
|
||||
expect(container).toMatchSnapshot();
|
||||
unRegisterRenderer(renderer);
|
||||
});
|
||||
|
||||
// test('factory:definitions', async () => {
|
||||
// const {container, getByText} = render(
|
||||
// amisRender(
|
||||
// {
|
||||
// definitions: {
|
||||
// aa: {
|
||||
// type: 'text',
|
||||
// name: 'jack',
|
||||
// value: 'ref value',
|
||||
// remark: '通过<code>\\$ref</code>引入的组件'
|
||||
// },
|
||||
// bb: {
|
||||
// type: 'combo',
|
||||
// multiple: true,
|
||||
// multiLine: true,
|
||||
// remark: '<code>combo</code>中的子项引入自身,实现嵌套的效果',
|
||||
// controls: [
|
||||
// {
|
||||
// label: 'combo 1',
|
||||
// type: 'text',
|
||||
// name: 'key'
|
||||
// },
|
||||
// {
|
||||
// label: 'combo 2',
|
||||
// name: 'value',
|
||||
// $ref: 'aa'
|
||||
// },
|
||||
// {
|
||||
// name: 'children',
|
||||
// label: 'children',
|
||||
// $ref: 'bb'
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// },
|
||||
// type: 'page',
|
||||
// title: '引用',
|
||||
// body: [
|
||||
// {
|
||||
// type: 'form',
|
||||
// api: 'api/xxx',
|
||||
// actions: [],
|
||||
// controls: [
|
||||
// {
|
||||
// label: 'text2',
|
||||
// $ref: 'aa',
|
||||
// name: 'ref1'
|
||||
// },
|
||||
// {
|
||||
// label: 'combo',
|
||||
// $ref: 'bb',
|
||||
// name: 'ref2'
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// },
|
||||
// {},
|
||||
// makeEnv({})
|
||||
// )
|
||||
// );
|
||||
|
||||
// await waitFor(() => {
|
||||
// expect(getByText('新增')).toBeInTheDocument();
|
||||
// });
|
||||
// fireEvent.click(getByText('新增'));
|
||||
// await waitFor(() => {
|
||||
// expect(getByText('combo 1')).toBeInTheDocument();
|
||||
// });
|
||||
// expect(container).toMatchSnapshot();
|
||||
// });
|
||||
|
||||
// test('factory:definitions override', async () => {
|
||||
// const {container, getByText} = render(
|
||||
// amisRender(
|
||||
// {
|
||||
// definitions: {
|
||||
// aa: {
|
||||
// type: 'text',
|
||||
// name: 'jack',
|
||||
// remark: '通过<code>\\$ref</code>引入的组件'
|
||||
// },
|
||||
// bb: {
|
||||
// type: 'combo',
|
||||
// multiple: true,
|
||||
// multiLine: true,
|
||||
// remark: '<code>combo</code>中的子项引入自身,实现嵌套的效果',
|
||||
// controls: [
|
||||
// {
|
||||
// label: 'combo 1',
|
||||
// type: 'text',
|
||||
// name: 'key'
|
||||
// },
|
||||
// {
|
||||
// label: 'combo 2',
|
||||
// name: 'value',
|
||||
// $ref: 'aa'
|
||||
// },
|
||||
// {
|
||||
// name: 'children',
|
||||
// label: 'children',
|
||||
// $ref: 'bb'
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// },
|
||||
// type: 'page',
|
||||
// title: '引用',
|
||||
// body: [
|
||||
// {
|
||||
// type: 'form',
|
||||
// api: 'api/xxx',
|
||||
// actions: [],
|
||||
// controls: [
|
||||
// {
|
||||
// label: 'text2',
|
||||
// $ref: 'aa',
|
||||
// name: 'ref1'
|
||||
// },
|
||||
// {
|
||||
// label: 'combo',
|
||||
// $ref: 'bb',
|
||||
// name: 'ref2',
|
||||
// type: 'checkboxes',
|
||||
// value: 1,
|
||||
// options: [
|
||||
// {
|
||||
// label: 'Option A',
|
||||
// value: 1
|
||||
// },
|
||||
// {
|
||||
// label: 'Option B',
|
||||
// value: 2
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
// },
|
||||
// {},
|
||||
// makeEnv({})
|
||||
// )
|
||||
// );
|
||||
|
||||
// await waitFor(() => {
|
||||
// expect(getByText('combo')).toBeInTheDocument();
|
||||
// });
|
||||
// expect(container).toMatchSnapshot();
|
||||
// });
|
||||
1
packages/amis-core/__tests__/filter.test.d.ts
vendored
Normal file
1
packages/amis-core/__tests__/filter.test.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
394
packages/amis-core/__tests__/filter.test.ts
Normal file
394
packages/amis-core/__tests__/filter.test.ts
Normal file
@@ -0,0 +1,394 @@
|
||||
import {resolveVariableAndFilter} from '../src/utils/tpl-builtin';
|
||||
import {evaluate} from 'amis-formula';
|
||||
|
||||
test(`filter:map`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a | map: toInt}', {
|
||||
a: ['123', '3434']
|
||||
})
|
||||
).toMatchObject([123, 3434]);
|
||||
});
|
||||
test(`filter:html`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a}', {
|
||||
a: '<html>'
|
||||
})
|
||||
).toEqual('<html>');
|
||||
});
|
||||
|
||||
test(`filter:complex`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${`${a}`}', {
|
||||
a: '<html>'
|
||||
})
|
||||
).toEqual('<html>');
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter('${a ? a : a}', {
|
||||
a: '<html>'
|
||||
})
|
||||
).toEqual('<html>');
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter('${b.a}', {
|
||||
a: '<html>',
|
||||
b: {
|
||||
a: '<br />'
|
||||
}
|
||||
})
|
||||
).toEqual('<br />');
|
||||
});
|
||||
|
||||
test(`filter:json`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a | json : 0}', {
|
||||
a: {a: 1}
|
||||
})
|
||||
).toEqual('{"a":1}');
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter('${a | json : 2}', {
|
||||
a: {a: 1}
|
||||
})
|
||||
).toEqual('{\n "a": 1\n}');
|
||||
});
|
||||
|
||||
test(`filter:toJson`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|toJson}', {
|
||||
a: '{"a":1}'
|
||||
})
|
||||
).toMatchObject({a: 1});
|
||||
});
|
||||
|
||||
test(`filter:toInt`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|toInt}', {
|
||||
a: '233'
|
||||
})
|
||||
).toBe(233);
|
||||
});
|
||||
|
||||
test(`filter:toFloat`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|toFloat}', {
|
||||
a: '233.233'
|
||||
})
|
||||
).toBe(233.233);
|
||||
});
|
||||
|
||||
test(`filter:toDate`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|toDate:x|date: YYYY-MM-DD}', {
|
||||
a: 1638028267226
|
||||
})
|
||||
).toBe('2021-11-27');
|
||||
});
|
||||
|
||||
test(`filter:fromNow`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|toDate:x|fromNow}', {
|
||||
a: Date.now() - 2 * 60 * 1000
|
||||
})
|
||||
).toBe('2 minutes ago');
|
||||
});
|
||||
|
||||
test(`filter:dateModify`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|toDate:x|dateModify:subtract:2:m|fromNow}', {
|
||||
a: Date.now()
|
||||
})
|
||||
).toBe('2 minutes ago');
|
||||
});
|
||||
|
||||
test(`filter:number`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|number}', {
|
||||
a: 1234
|
||||
})
|
||||
).toBe('1,234');
|
||||
});
|
||||
|
||||
test(`filter:trim`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|trim}', {
|
||||
a: ' ab '
|
||||
})
|
||||
).toBe('ab');
|
||||
});
|
||||
|
||||
test(`filter:duration`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|duration}', {
|
||||
a: 234343
|
||||
})
|
||||
).toBe('2天17时5分43秒');
|
||||
});
|
||||
|
||||
test(`filter:bytes`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|bytes}', {
|
||||
a: 234343
|
||||
})
|
||||
).toBe('234 KB');
|
||||
});
|
||||
|
||||
test(`filter:bytes:step`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|bytes:1024}', {
|
||||
a: 234343
|
||||
})
|
||||
).toBe('229 KB');
|
||||
});
|
||||
|
||||
test(`filter:round`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|round}', {
|
||||
a: 23.234
|
||||
})
|
||||
).toBe('23.23');
|
||||
});
|
||||
|
||||
test(`filter:truncate`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|truncate:5}', {
|
||||
a: 'abcdefghijklmnopqrst'
|
||||
})
|
||||
).toBe('abcde...');
|
||||
});
|
||||
|
||||
test(`filter:url_encode`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|url_encode}', {
|
||||
a: '='
|
||||
})
|
||||
).toBe('%3D');
|
||||
});
|
||||
|
||||
describe('filter:url_decode', () => {
|
||||
test(`filter:url_decode:normal`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|url_decode}', {
|
||||
a: '%3D'
|
||||
})
|
||||
).toBe('=');
|
||||
});
|
||||
|
||||
test(`filter:url_decode:error`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|url_decode}', {
|
||||
a: '%'
|
||||
})
|
||||
).toBe(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
test(`filter:default`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|default:-}', {
|
||||
a: ''
|
||||
})
|
||||
).toBe('-');
|
||||
});
|
||||
|
||||
test(`filter:join`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|join:-}', {
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toBe('1-2-3');
|
||||
});
|
||||
|
||||
test(`filter:split`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|split:-}', {
|
||||
a: '1-2-3'
|
||||
})
|
||||
).toMatchObject(['1', '2', '3']);
|
||||
});
|
||||
|
||||
test(`filter:sortBy`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|sortBy:&|join}', {
|
||||
a: ['b', 'c', 'a']
|
||||
})
|
||||
).toBe('a,b,c');
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|sortBy:&:numerical|join}', {
|
||||
a: ['023', '20', '44']
|
||||
})
|
||||
).toBe('20,023,44');
|
||||
});
|
||||
|
||||
test(`filter:objectToArray`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|objectToArray}', {
|
||||
a: {
|
||||
a: 1,
|
||||
b: 2,
|
||||
done: 'Done'
|
||||
}
|
||||
})
|
||||
).toMatchObject([
|
||||
{
|
||||
value: 'a',
|
||||
label: 1
|
||||
},
|
||||
{
|
||||
value: 'b',
|
||||
label: 2
|
||||
},
|
||||
{
|
||||
value: 'done',
|
||||
label: 'Done'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test(`filter:substring`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|substring:0:2}', {
|
||||
a: 'abcdefg'
|
||||
})
|
||||
).toBe('ab');
|
||||
expect(
|
||||
resolveVariableAndFilter('${a|substring:1:3}', {
|
||||
a: 'abcdefg'
|
||||
})
|
||||
).toBe('bc');
|
||||
});
|
||||
|
||||
test(`filter:variableInVariable`, () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${a}', {
|
||||
a: 'abc$0defg'
|
||||
})
|
||||
).toBe('abc$0defg');
|
||||
});
|
||||
|
||||
test('filter:isMatch', () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${status | isMatch:2:1|isMatch:5:1:4}', {
|
||||
status: 2
|
||||
})
|
||||
).toBe(1);
|
||||
|
||||
expect(
|
||||
resolveVariableAndFilter('${status | isMatch:2:1|isMatch:5:1:4}', {
|
||||
status: 1
|
||||
})
|
||||
).toBe(4);
|
||||
});
|
||||
|
||||
test('filter:filter:isMatch', () => {
|
||||
expect(
|
||||
resolveVariableAndFilter('${items|filter:text:match:"ab"}', {
|
||||
items: [
|
||||
{
|
||||
text: 'abc'
|
||||
},
|
||||
{
|
||||
text: 'bcd'
|
||||
},
|
||||
{
|
||||
text: 'cde'
|
||||
}
|
||||
]
|
||||
})
|
||||
).toMatchObject([
|
||||
{
|
||||
text: 'abc'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
test('evalute:conditional', () => {
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isTrue: true : false}',
|
||||
{
|
||||
a: 4
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(true);
|
||||
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isTrue: b : false}',
|
||||
{
|
||||
a: 4,
|
||||
b: 5
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(5);
|
||||
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isTrue: b : false}',
|
||||
{
|
||||
a: null,
|
||||
b: 5
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(false);
|
||||
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isTrue : trueValue : falseValue}',
|
||||
{
|
||||
a: true,
|
||||
trueValue: 5,
|
||||
falseValue: 10
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe(5);
|
||||
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isEquals: 1 : "1" |isEquals: 2 : "2" | isEquals: 3 : "3" }',
|
||||
{
|
||||
a: 3
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe('3');
|
||||
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isEquals: 1 : "1" |isEquals: 1 : "2" | isEquals: 1 : "3" }',
|
||||
{
|
||||
a: 1
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe('1');
|
||||
|
||||
expect(
|
||||
evaluate(
|
||||
'${a | isEquals: 1 : "1" : "12" |isEquals: 2 : "2" | isEquals: 3 : "3" }',
|
||||
{
|
||||
a: 2
|
||||
},
|
||||
{
|
||||
defaultFilter: 'raw'
|
||||
}
|
||||
)
|
||||
).toBe('12');
|
||||
});
|
||||
13
packages/amis-core/__tests__/helper.d.ts
vendored
Normal file
13
packages/amis-core/__tests__/helper.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import { RenderOptions } from '../src/factory';
|
||||
export declare function wait(duration: number, fnOrUseWaitFor?: Function | boolean): Promise<void>;
|
||||
export declare function makeEnv(env?: Partial<RenderOptions>): RenderOptions;
|
||||
export declare const createMockMediaMatcher: (matchesOrMapOfMatches: any) => (qs: any) => {
|
||||
matches: any;
|
||||
media: string;
|
||||
addListener: () => void;
|
||||
addEventListener: () => void;
|
||||
removeEventListener: () => void;
|
||||
onchange: () => void;
|
||||
removeListener: () => void;
|
||||
dispatchEvent: () => boolean;
|
||||
};
|
||||
78
packages/amis-core/__tests__/helper.tsx
Normal file
78
packages/amis-core/__tests__/helper.tsx
Normal file
@@ -0,0 +1,78 @@
|
||||
import {waitFor} from '@testing-library/react';
|
||||
import {RenderOptions} from '../src/factory';
|
||||
|
||||
// jest.useFakeTimers 会修改 global 的 setTimeout 所以需要把原始的记录下来。
|
||||
const timerFn = (global as any).originSetTimeout || setTimeout;
|
||||
export function wait(duration: number, fnOrUseWaitFor?: Function | boolean) {
|
||||
const useWaitFor = fnOrUseWaitFor !== false;
|
||||
const fn = (
|
||||
typeof fnOrUseWaitFor === 'function' ? fnOrUseWaitFor : undefined
|
||||
) as Function;
|
||||
|
||||
return useWaitFor
|
||||
? waitFor(
|
||||
() =>
|
||||
new Promise<void>(resolve => {
|
||||
timerFn(() => {
|
||||
fn && fn();
|
||||
resolve();
|
||||
}, duration);
|
||||
}),
|
||||
{
|
||||
timeout: duration + 100
|
||||
}
|
||||
)
|
||||
: new Promise<void>(resolve => {
|
||||
timerFn(() => {
|
||||
fn && fn();
|
||||
resolve();
|
||||
}, duration);
|
||||
});
|
||||
}
|
||||
|
||||
export function makeEnv(env?: Partial<RenderOptions>): RenderOptions {
|
||||
return {
|
||||
session: 'test-case',
|
||||
isCancel: () => false,
|
||||
notify: (msg: string) => null,
|
||||
jumpTo: (to: string) => console.info('Now should jump to ' + to),
|
||||
alert: msg => console.info(`Alert: ${msg}`),
|
||||
...env
|
||||
};
|
||||
}
|
||||
|
||||
export const createMockMediaMatcher =
|
||||
(matchesOrMapOfMatches: any) => (qs: any) => ({
|
||||
matches:
|
||||
typeof matchesOrMapOfMatches === 'object'
|
||||
? matchesOrMapOfMatches[qs]
|
||||
: matchesOrMapOfMatches,
|
||||
media: '',
|
||||
addListener: () => {},
|
||||
addEventListener: () => {},
|
||||
removeEventListener: () => {},
|
||||
onchange: () => {},
|
||||
removeListener: () => {},
|
||||
dispatchEvent: () => {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
// Mock IntersectionObserver
|
||||
class IntersectionObserver {
|
||||
observe = jest.fn();
|
||||
disconnect = jest.fn();
|
||||
unobserve = jest.fn();
|
||||
}
|
||||
|
||||
Object.defineProperty(window, 'IntersectionObserver', {
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: IntersectionObserver
|
||||
});
|
||||
|
||||
Object.defineProperty(global, 'IntersectionObserver', {
|
||||
writable: true,
|
||||
configurable: true,
|
||||
value: IntersectionObserver
|
||||
});
|
||||
1
packages/amis-core/__tests__/jest.setup.d.ts
vendored
Normal file
1
packages/amis-core/__tests__/jest.setup.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
87
packages/amis-core/__tests__/jest.setup.js
Normal file
87
packages/amis-core/__tests__/jest.setup.js
Normal file
@@ -0,0 +1,87 @@
|
||||
const originalWarn = console.warn.bind(console.warn);
|
||||
const originalGroupCollapsed = console.warn.bind(console.groupCollapsed);
|
||||
const originalGroup = console.warn.bind(console.group);
|
||||
const originalGroupEnd = console.warn.bind(console.groupEnd);
|
||||
const originalDebug = console.warn.bind(console.debug);
|
||||
const originalError = console.error.bind(console.error);
|
||||
require('@testing-library/jest-dom');
|
||||
require('moment-timezone');
|
||||
const loadAllAsyncRenderers = require('amis-core').loadAllAsyncRenderers;
|
||||
const moment = require('moment');
|
||||
moment.tz.setDefault('Asia/Shanghai');
|
||||
const cleanup = require('@testing-library/react').cleanup;
|
||||
|
||||
// https://github.com/nrwl/nx/issues/1178
|
||||
// 解决jest 运行的时候报:
|
||||
// ReferenceError: DragEvent is not defined
|
||||
if (!window.DragEvent) {
|
||||
Object.defineProperty(window, 'DragEvent', {
|
||||
value: class DragEvent {}
|
||||
});
|
||||
}
|
||||
|
||||
// Mock ResizeObserver in jest env
|
||||
global.ResizeObserver = require('resize-observer-polyfill');
|
||||
|
||||
global.__buildVersion = '';
|
||||
|
||||
global.BroadcastChannel = class {
|
||||
constructor(channelName) {
|
||||
this.channelName = channelName;
|
||||
this.listeners = [];
|
||||
}
|
||||
postMessage(message) {
|
||||
this.listeners.forEach(listener => listener({data: message}));
|
||||
}
|
||||
addEventListener(event, listener) {
|
||||
if (event === 'message') {
|
||||
this.listeners.push(listener);
|
||||
}
|
||||
}
|
||||
removeEventListener(event, listener) {
|
||||
if (event === 'message') {
|
||||
this.listeners = this.listeners.filter(l => l !== listener);
|
||||
}
|
||||
}
|
||||
close() {
|
||||
this.listeners = [];
|
||||
}
|
||||
};
|
||||
|
||||
global.beforeAll(async () => {
|
||||
console.warn =
|
||||
console.groupCollapsed =
|
||||
console.group =
|
||||
console.groupEnd =
|
||||
console.debug =
|
||||
console.error =
|
||||
msg => {
|
||||
// warning 先关了,实在太吵。
|
||||
// const str = msg.toString();
|
||||
// if (
|
||||
// str.includes('componentWillMount') ||
|
||||
// str.includes('componentWillReceiveProps')
|
||||
// ) {
|
||||
// return;
|
||||
// }
|
||||
// originalWarn(msg);
|
||||
};
|
||||
|
||||
await loadAllAsyncRenderers();
|
||||
});
|
||||
global.afterAll(() => {
|
||||
console.warn = originalWarn;
|
||||
console.groupCollapsed = originalGroupCollapsed;
|
||||
console.group = originalGroup;
|
||||
console.groupEnd = originalGroupEnd;
|
||||
console.debug = originalDebug;
|
||||
console.error = originalError;
|
||||
cleanup();
|
||||
});
|
||||
|
||||
// expect.addSnapshotSerializer({
|
||||
// test: val => typeof val === 'string' && /^[a-z0-9]{12}$/.test(val),
|
||||
// print: val => {
|
||||
// return JSON.stringify('__guid_dynamic_value__');
|
||||
// }
|
||||
// });
|
||||
58
packages/amis-core/__tests__/renderers/wrapControl.test.tsx
Normal file
58
packages/amis-core/__tests__/renderers/wrapControl.test.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* @author: ranqirong
|
||||
* @date: 2024-10-10
|
||||
* @description:
|
||||
*/
|
||||
import React from 'react';
|
||||
import {wrapControl} from '../../src/renderers/wrapControl';
|
||||
import {render, screen} from '@testing-library/react';
|
||||
import {RootStoreContext} from '../../src/WithRootStore';
|
||||
import {RendererStore} from '../../src/store/index';
|
||||
import {FormItemProps} from '../../src/renderers/Item';
|
||||
|
||||
const renderComponent = (WrappedComponent: any, props: any) => {
|
||||
const store = RendererStore.create({});
|
||||
return render(
|
||||
<RootStoreContext.Provider value={store}>
|
||||
<WrappedComponent rootStore={{}} $schema={{}} {...props} />
|
||||
</RootStoreContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
describe('constructor', () => {
|
||||
it('如果传入有defaultProps包含name属性,则创建FormItemStore', () => {
|
||||
const OriginComponent = (props: FormItemProps) => {
|
||||
// 用于断言formItem已被注册
|
||||
return <div>{props.formItem?.storeType}</div>;
|
||||
};
|
||||
OriginComponent.defaultProps = {
|
||||
name: 'test'
|
||||
};
|
||||
const WrappedComponent = wrapControl({}, OriginComponent as any);
|
||||
|
||||
renderComponent(WrappedComponent, {$schema: {}});
|
||||
expect(screen.getByText('FormItemStore')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('如果$schema中包含name属性,则创建FormItemStore', () => {
|
||||
const OriginComponent = (props: FormItemProps) => {
|
||||
// 用于断言formItem已被注册
|
||||
return <div>{props.formItem?.storeType}</div>;
|
||||
};
|
||||
const WrappedComponent = wrapControl({}, OriginComponent as any);
|
||||
|
||||
renderComponent(WrappedComponent, {$schema: {name: 'test'}});
|
||||
expect(screen.getByText('FormItemStore')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('如果$schema和defaultProps中都不包含name属性,则不创建FormItemStore', () => {
|
||||
const OriginComponent = (props: FormItemProps) => {
|
||||
// 用于断言formItem已被注册
|
||||
return <div>{props.formItem?.storeType}</div>;
|
||||
};
|
||||
const WrappedComponent = wrapControl({}, OriginComponent as any);
|
||||
|
||||
renderComponent(WrappedComponent, {$schema: {}});
|
||||
expect(screen.queryByText('FormItemStore')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
18
packages/amis-core/__tests__/scoped.test.ts
Normal file
18
packages/amis-core/__tests__/scoped.test.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import {
|
||||
registerRenderer,
|
||||
unRegisterRenderer,
|
||||
RendererProps
|
||||
} from '../src/factory';
|
||||
import {render as amisRender, splitTarget} from '../src';
|
||||
import React from 'react';
|
||||
import {render, fireEvent, waitFor} from '@testing-library/react';
|
||||
import {makeEnv} from './helper';
|
||||
|
||||
test('Scoped splitTarget', async () => {
|
||||
expect(splitTarget('abc')).toEqual(['abc']);
|
||||
expect(splitTarget('a,b,c')).toEqual(['a', 'b', 'c']);
|
||||
expect(splitTarget('a, b, c ')).toEqual(['a', 'b', 'c']);
|
||||
expect(splitTarget('a?x=1&y=2,b,c')).toEqual(['a?x=1&y=2', 'b', 'c']);
|
||||
expect(splitTarget('a?x=${[a, b]},b,c')).toEqual(['a?x=${[a, b]}', 'b', 'c']);
|
||||
expect(splitTarget('a?x=${[a, b]}')).toEqual(['a?x=${[a, b]}']);
|
||||
});
|
||||
113
packages/amis-core/__tests__/store/table.test.ts
Normal file
113
packages/amis-core/__tests__/store/table.test.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import {TableStore, SELECTED_STATUS} from '../../src/store/table';
|
||||
|
||||
import type {IRow, ITableStore} from '../../src/store/table';
|
||||
|
||||
let table: ITableStore;
|
||||
let firstLevel: IRow;
|
||||
let secondLevel: IRow;
|
||||
let secondLevel2: IRow;
|
||||
let thirdLevel: IRow;
|
||||
let thirdLevel2: IRow;
|
||||
let thirdLevel3: IRow;
|
||||
|
||||
beforeEach(() => {
|
||||
table = TableStore.create({id: 'mock-id', storeType: 'table'});
|
||||
table.initRows([
|
||||
{
|
||||
name: '手机维修',
|
||||
children: [
|
||||
{
|
||||
name: '苹果',
|
||||
children: [
|
||||
{
|
||||
name: 'iphone系列'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: '华为',
|
||||
children: [
|
||||
{
|
||||
name: 'mate系列'
|
||||
},
|
||||
{
|
||||
name: 'p系列'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
|
||||
firstLevel = table.rows[0];
|
||||
secondLevel = table.rows[0].children[0];
|
||||
secondLevel2 = table.rows[0].children[1];
|
||||
thirdLevel = table.rows[0].children[0].children[0];
|
||||
thirdLevel2 = table.rows[0].children[1].children[0];
|
||||
thirdLevel3 = table.rows[0].children[1].children[1];
|
||||
});
|
||||
|
||||
describe('Row', () => {
|
||||
it('通过parent快速拿到父节点', () => {
|
||||
expect(secondLevel.parent).toEqual(firstLevel);
|
||||
});
|
||||
|
||||
it('通过table快速拿到根节点', () => {
|
||||
expect(thirdLevel.table).toEqual(table);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TableStore', () => {
|
||||
it('选中父节点,所有子节点自动选中', () => {
|
||||
firstLevel.toggle(true);
|
||||
|
||||
const selectedRows = table.selectedRows.map(row => row.data.name);
|
||||
|
||||
expect(selectedRows).toEqual(
|
||||
expect.arrayContaining([
|
||||
'苹果',
|
||||
'iphone系列',
|
||||
'华为',
|
||||
'mate系列',
|
||||
'p系列'
|
||||
])
|
||||
);
|
||||
});
|
||||
|
||||
it('选中所有子节点,所有父节点自动选中', () => {
|
||||
thirdLevel.toggle(true);
|
||||
thirdLevel2.toggle(true);
|
||||
thirdLevel3.toggle(true);
|
||||
|
||||
const selectedRows = table.selectedRows.map(row => row.data.name);
|
||||
|
||||
expect(selectedRows).toEqual(
|
||||
expect.arrayContaining(['苹果', '华为', '手机维修'])
|
||||
);
|
||||
});
|
||||
|
||||
it('默认情况下,父节点是未选中状态,选中一个子节点切换到部分选中状态,选中所有2个子节点切换到全部选中状态', () => {
|
||||
expect(firstLevel.childrenSelected()).toBe(SELECTED_STATUS.NONE);
|
||||
expect(firstLevel.partial).toBe(false);
|
||||
|
||||
secondLevel.toggle(true);
|
||||
|
||||
expect(firstLevel.childrenSelected()).toBe(SELECTED_STATUS.PARTIAL);
|
||||
expect(firstLevel.partial).toBe(true);
|
||||
|
||||
secondLevel2.toggle(true);
|
||||
|
||||
expect(firstLevel.childrenSelected()).toBe(SELECTED_STATUS.ALL);
|
||||
expect(firstLevel.partial).toBe(false);
|
||||
});
|
||||
|
||||
it('多个子节点只选中一个,祖先节点不会被选中,但会被标记为部分选中状态', () => {
|
||||
thirdLevel2.toggle(true);
|
||||
|
||||
expect(secondLevel2.checked).toBe(false);
|
||||
expect(secondLevel2.partial).toBe(true);
|
||||
|
||||
expect(firstLevel.checked).toBe(false);
|
||||
expect(firstLevel.partial).toBe(true);
|
||||
});
|
||||
});
|
||||
1
packages/amis-core/__tests__/tokenize.test.d.ts
vendored
Normal file
1
packages/amis-core/__tests__/tokenize.test.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
26
packages/amis-core/__tests__/tokenize.test.ts
Normal file
26
packages/amis-core/__tests__/tokenize.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {tokenize} from '../src/utils/tpl-builtin';
|
||||
|
||||
test(`tokenize:null`, () => {
|
||||
expect(
|
||||
tokenize('abc${a}', {
|
||||
a: ''
|
||||
})
|
||||
).toBe('abc');
|
||||
expect(
|
||||
tokenize('abc${a}', {
|
||||
a: null
|
||||
})
|
||||
).toBe('abc');
|
||||
|
||||
expect(
|
||||
tokenize('abc${a}', {
|
||||
a: undefined
|
||||
})
|
||||
).toBe('abc');
|
||||
|
||||
expect(
|
||||
tokenize('abc${a}', {
|
||||
a: 0
|
||||
})
|
||||
).toBe('abc0');
|
||||
});
|
||||
11
packages/amis-core/__tests__/utils/arraySlice.test.ts
Normal file
11
packages/amis-core/__tests__/utils/arraySlice.test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import {arraySlice} from '../../src/utils/arraySlice';
|
||||
|
||||
test(`arrayslice:test`, () => {
|
||||
const testArray = [0, 1, 2, 3, 4, 5];
|
||||
expect(arraySlice(testArray, ':')).toEqual([0, 1, 2, 3, 4, 5]);
|
||||
expect(arraySlice(testArray, '0,3,4')).toEqual([0, 3, 4]);
|
||||
expect(arraySlice(testArray, '1:2')).toEqual([1]);
|
||||
expect(arraySlice(testArray, '0:-2')).toEqual([0, 1, 2, 3]);
|
||||
expect(arraySlice(testArray, '1:-2')).toEqual([1, 2, 3]);
|
||||
expect(arraySlice(testArray, '0,1:-2')).toEqual([0, 1, 2, 3]);
|
||||
});
|
||||
28
packages/amis-core/__tests__/utils/math.test.ts
Normal file
28
packages/amis-core/__tests__/utils/math.test.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import {safeAdd, safeSub, numberFormatter} from '../../src/utils/math';
|
||||
|
||||
test(`math safeAdd:test`, () => {
|
||||
expect(safeAdd(0.1, 0.2)).toEqual(0.3);
|
||||
expect(safeAdd(0.111, 0.455)).toEqual(0.566);
|
||||
expect(safeAdd(NaN, 1)).toEqual(NaN);
|
||||
expect(safeAdd(NaN, NaN)).toEqual(NaN);
|
||||
});
|
||||
|
||||
test(`math safeSub:test`, () => {
|
||||
expect(safeSub(0.8, 0.1)).toEqual(0.7);
|
||||
expect(safeSub(0.1, 0.111111)).toEqual(-0.011111);
|
||||
expect(safeAdd(NaN, 1)).toEqual(NaN);
|
||||
expect(safeAdd(NaN, NaN)).toEqual(NaN);
|
||||
});
|
||||
|
||||
test('numberFormatter:test', () => {
|
||||
expect(numberFormatter(0)).toEqual('0');
|
||||
expect(numberFormatter(0.123)).toEqual('0.123');
|
||||
expect(numberFormatter(0.123, 0)).toEqual('0');
|
||||
expect(numberFormatter(0, 2)).toEqual('0.00');
|
||||
expect(numberFormatter(0, 8)).toEqual('0.00000000');
|
||||
expect(numberFormatter(123456)).toEqual('123,456');
|
||||
expect(numberFormatter(123456, 2)).toEqual('123,456.00');
|
||||
expect(numberFormatter(1234567890000)).toEqual('1,234,567,890,000');
|
||||
expect(numberFormatter(1234567890000, 4)).toEqual('1,234,567,890,000.0000');
|
||||
expect(numberFormatter(1000000000000000)).toEqual('1,000,000,000,000,000');
|
||||
});
|
||||
76
packages/amis-core/__tests__/utils/styleHelper.test.ts
Normal file
76
packages/amis-core/__tests__/utils/styleHelper.test.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import {formatCustomStyle} from '../../src/utils/style-helper';
|
||||
|
||||
const customStyleCases: [any, string][] = [
|
||||
[
|
||||
{
|
||||
root: {
|
||||
color: 'red'
|
||||
}
|
||||
},
|
||||
'\n.wrapperCustomStyle-test {\n color: red;\n}'
|
||||
],
|
||||
[
|
||||
{
|
||||
root: {
|
||||
a: {
|
||||
color: 'blue'
|
||||
}
|
||||
}
|
||||
},
|
||||
'\n.wrapperCustomStyle-test a {\n color: blue;\n}'
|
||||
],
|
||||
[
|
||||
{
|
||||
root: {
|
||||
'a,i': {
|
||||
color: 'blue'
|
||||
}
|
||||
}
|
||||
},
|
||||
'\n.wrapperCustomStyle-test a,.wrapperCustomStyle-test i {\n color: blue;\n}'
|
||||
],
|
||||
[
|
||||
{
|
||||
root: {
|
||||
'a,i': {
|
||||
span: {
|
||||
color: 'blue'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'\n.wrapperCustomStyle-test a span,.wrapperCustomStyle-test i span {\n color: blue;\n}'
|
||||
],
|
||||
[
|
||||
{
|
||||
root: {
|
||||
'a,i': {
|
||||
'span,.custom': {
|
||||
color: 'blue'
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
'\n.wrapperCustomStyle-test a span,.wrapperCustomStyle-test i span,.wrapperCustomStyle-test a .custom,.wrapperCustomStyle-test i .custom {\n color: blue;\n}'
|
||||
],
|
||||
[
|
||||
{
|
||||
'.cxd-Table': {
|
||||
'td,th': {
|
||||
color: 'red'
|
||||
}
|
||||
}
|
||||
},
|
||||
'\n.wrapperCustomStyle-test .cxd-Table td,.wrapperCustomStyle-test .cxd-Table th {\n color: red;\n}'
|
||||
]
|
||||
];
|
||||
|
||||
describe('formatCustomStyle', () => {
|
||||
test.each(customStyleCases)(
|
||||
'formatCustomStyle should return styles correctly',
|
||||
(customStyle, expected) => {
|
||||
const {content} = formatCustomStyle({customStyle, id: 'test'});
|
||||
expect(content).toBe(expected);
|
||||
}
|
||||
);
|
||||
});
|
||||
107
packages/amis-core/__tests__/utils/visitedCache.test.ts
Normal file
107
packages/amis-core/__tests__/utils/visitedCache.test.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import VisitedCache from '../../src/utils/visitedCache';
|
||||
|
||||
describe('VisitedCache', () => {
|
||||
let cache: VisitedCache<string, number>;
|
||||
|
||||
beforeEach(() => {
|
||||
cache = new VisitedCache<string, number>(5);
|
||||
});
|
||||
|
||||
it('should set and get values correctly', () => {
|
||||
cache.set('a', 1);
|
||||
cache.set('b', 2);
|
||||
cache.set('c', 3);
|
||||
|
||||
expect(cache.get('a')).toBe(1);
|
||||
expect(cache.get('b')).toBe(2);
|
||||
expect(cache.get('c')).toBe(3);
|
||||
});
|
||||
|
||||
it('should evict the least visited entry when cache is full', () => {
|
||||
cache.set('a', 1);
|
||||
cache.set('b', 2);
|
||||
cache.set('c', 3);
|
||||
cache.set('d', 4);
|
||||
cache.set('e', 5);
|
||||
|
||||
// Access 'a' to increase its visit count
|
||||
cache.get('a');
|
||||
|
||||
cache.set('f', 6);
|
||||
|
||||
expect(cache.get('a')).toBe(1);
|
||||
expect(cache.get('b')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle cache misses correctly', () => {
|
||||
expect(cache.get('non-existent-key')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle multiple entries with the same visit count', () => {
|
||||
cache.set('a', 1);
|
||||
cache.set('b', 2);
|
||||
cache.set('c', 3);
|
||||
cache.set('d', 4);
|
||||
cache.set('e', 5);
|
||||
|
||||
// Access 'a', 'b', 'c' to make them have the same visit count
|
||||
cache.get('a');
|
||||
cache.get('b');
|
||||
cache.get('c');
|
||||
|
||||
cache.set('f', 6);
|
||||
|
||||
// 动态清理的个数为1,清理掉最少访问最旧的d项
|
||||
expect(cache.get('a')).toBe(1);
|
||||
expect(cache.get('b')).toBe(2);
|
||||
expect(cache.get('c')).toBe(3);
|
||||
expect(cache.get('d')).toBeUndefined();
|
||||
expect(cache.get('e')).toBe(5);
|
||||
expect(cache.get('f')).toBe(6);
|
||||
});
|
||||
|
||||
it('should handle dynamic release count', () => {
|
||||
cache = new VisitedCache<string, number>(10, 1 / 5);
|
||||
// 释放数量为 10 * 1/5 >> 0 || 1 = 2
|
||||
|
||||
cache.set('a', 1);
|
||||
cache.set('b', 2);
|
||||
cache.set('c', 3);
|
||||
cache.set('d', 4);
|
||||
cache.set('e', 5);
|
||||
cache.set('f', 6);
|
||||
cache.set('g', 7);
|
||||
cache.set('h', 8);
|
||||
cache.set('i', 9);
|
||||
cache.set('j', 10);
|
||||
|
||||
// 提升除了a之外的访问次数
|
||||
cache.get('b');
|
||||
cache.get('c');
|
||||
cache.get('d');
|
||||
cache.get('e');
|
||||
cache.get('f');
|
||||
cache.get('g');
|
||||
cache.get('h');
|
||||
cache.get('i');
|
||||
cache.get('j');
|
||||
|
||||
// 再次提升高b的访问次数
|
||||
cache.get('b');
|
||||
|
||||
// 此时应该清理掉两项,a和c
|
||||
cache.set('k', 11);
|
||||
|
||||
expect(cache.get('a')).toBeUndefined();
|
||||
expect(cache.get('b')).toBe(2);
|
||||
expect(cache.get('c')).toBeUndefined();
|
||||
expect(cache.get('d')).toBe(4);
|
||||
expect(cache.get('e')).toBe(5);
|
||||
expect(cache.get('f')).toBe(6);
|
||||
expect(cache.get('g')).toBe(7);
|
||||
expect(cache.get('h')).toBe(8);
|
||||
expect(cache.get('i')).toBe(9);
|
||||
expect(cache.get('j')).toBe(10);
|
||||
expect(cache.get('k')).toBe(11);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user