vendor: amis 6.13.0 base (fork root)
This commit is contained in:
83
packages/amis/__tests__/utils/__snapshots__/api.test.ts.snap
Normal file
83
packages/amis/__tests__/utils/__snapshots__/api.test.ts.snap
Normal file
@@ -0,0 +1,83 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`api:cache 1`] = `
|
||||
<div>
|
||||
<div
|
||||
class="cxd-Page"
|
||||
>
|
||||
<div
|
||||
class="cxd-Page-content"
|
||||
>
|
||||
<div
|
||||
class="cxd-Page-main"
|
||||
>
|
||||
<div
|
||||
class="cxd-Page-toolbar"
|
||||
>
|
||||
<button
|
||||
class="cxd-Button cxd-Button--default cxd-Button--size-default"
|
||||
type="button"
|
||||
>
|
||||
<span>
|
||||
Reload
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
class="cxd-Page-body"
|
||||
role="page-body"
|
||||
>
|
||||
<span
|
||||
class="cxd-TplField fr-view"
|
||||
>
|
||||
<span>
|
||||
The variable value is 1
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`api:cache 2`] = `
|
||||
<div>
|
||||
<div
|
||||
class="cxd-Page"
|
||||
>
|
||||
<div
|
||||
class="cxd-Page-content"
|
||||
>
|
||||
<div
|
||||
class="cxd-Page-main"
|
||||
>
|
||||
<div
|
||||
class="cxd-Page-toolbar"
|
||||
>
|
||||
<button
|
||||
class="cxd-Button cxd-Button--default cxd-Button--size-default"
|
||||
type="button"
|
||||
>
|
||||
<span>
|
||||
Reload
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
class="cxd-Page-body"
|
||||
role="page-body"
|
||||
>
|
||||
<span
|
||||
class="cxd-TplField fr-view"
|
||||
>
|
||||
<span>
|
||||
The variable value is 1
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
694
packages/amis/__tests__/utils/api.test.ts
Normal file
694
packages/amis/__tests__/utils/api.test.ts
Normal file
@@ -0,0 +1,694 @@
|
||||
import {render as amisRender} from '../../src';
|
||||
import {wait, makeEnv} from '../helper';
|
||||
import {render, fireEvent, cleanup, waitFor} from '@testing-library/react';
|
||||
import {buildApi, isApiOutdated, isValidApi} from 'amis-core';
|
||||
|
||||
test('api:buildApi', () => {
|
||||
expect(buildApi('/api/xxx')).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx'
|
||||
});
|
||||
|
||||
expect(buildApi('get:/api/xxx')).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx'
|
||||
});
|
||||
|
||||
expect(buildApi('delete:/api/xxx')).toMatchObject({
|
||||
method: 'delete',
|
||||
url: '/api/xxx'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx?a=${a}&b=${b}', {
|
||||
a: 1,
|
||||
b: 2
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=1&b=2'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi(
|
||||
{
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=${a}&b=${b}'
|
||||
},
|
||||
{
|
||||
a: 1,
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=1&b=2'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx?a=${a}', {
|
||||
a: '&'
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=' + encodeURIComponent('&')
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx?a=${a}', {
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?a[0]=1&a[1]=2&a[2]=3'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx/${x === "a" ? "A" : "B"}?a=${a}', {
|
||||
x: 'a',
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx/A?a[0]=1&a[1]=2&a[2]=3'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx/${x === "a" ? "A" : "B"}?a=${a}', {
|
||||
x: 'b',
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx/B?a[0]=1&a[1]=2&a[2]=3'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx/${x === "a" ? "A" : "B"}?a=${a}#a=1&b=2', {
|
||||
x: 'b',
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx/B?a[0]=1&a[1]=2&a[2]=3#a=1&b=2'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi(
|
||||
'/api/xxx/${x === "a" ? "A" : "B"}?a=${a}&b=${x == "a" ? "A" : "B"}',
|
||||
{
|
||||
x: 'b',
|
||||
a: [1, 2, 3]
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx/B?a[0]=1&a[1]=2&a[2]=3&b=B'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi('/api/xxx/${x === "a" ? "A" : "B"}', {
|
||||
x: 'b',
|
||||
a: [1, 2, 3]
|
||||
})
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx/B'
|
||||
});
|
||||
});
|
||||
|
||||
test('api:buildApi2', () => {
|
||||
expect(
|
||||
buildApi('http://domain.com/#/subpath?a=1&b=2', {
|
||||
a: 1
|
||||
}).url
|
||||
).toBe('http://domain.com/#/subpath?a=1&b=2');
|
||||
|
||||
expect(
|
||||
buildApi('http://domain.com/subpath?a=1&b=2#233', {
|
||||
a: 1
|
||||
}).url
|
||||
).toBe('http://domain.com/subpath?a=1&b=2#233');
|
||||
|
||||
expect(
|
||||
buildApi('http://domain.com/subpath?a=1&b=${a}#233', {
|
||||
a: 1
|
||||
}).url
|
||||
).toBe('http://domain.com/subpath?a=1&b=1#233');
|
||||
});
|
||||
|
||||
test('api:buildApi:dataMapping', () => {
|
||||
expect(
|
||||
buildApi(
|
||||
{
|
||||
method: 'post',
|
||||
url: '/api/xxx',
|
||||
data: {
|
||||
a: 1,
|
||||
b: '${b}'
|
||||
}
|
||||
},
|
||||
{
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'post',
|
||||
url: '/api/xxx',
|
||||
data: {
|
||||
a: 1,
|
||||
b: 2
|
||||
}
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi(
|
||||
{
|
||||
method: 'post',
|
||||
url: '/api/xxx',
|
||||
headers: {
|
||||
a: 1,
|
||||
b: '${b}'
|
||||
}
|
||||
},
|
||||
{
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'post',
|
||||
url: '/api/xxx',
|
||||
headers: {
|
||||
a: 1,
|
||||
b: 2
|
||||
}
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi(
|
||||
{
|
||||
method: 'get',
|
||||
url: '/api/xxx?b=${b}&c=3&d=${d}'
|
||||
},
|
||||
{
|
||||
b: 2,
|
||||
d: 'abc${123}'
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?b=2&c=3&d=abc%24%7B123%7D',
|
||||
query: {
|
||||
b: 2,
|
||||
c: '3',
|
||||
d: 'abc${123}'
|
||||
}
|
||||
});
|
||||
|
||||
expect(
|
||||
buildApi(
|
||||
{
|
||||
method: 'get',
|
||||
url: '/api/xxx?c=3&d=${d}',
|
||||
query: {
|
||||
a: 1,
|
||||
b: '${b}'
|
||||
}
|
||||
},
|
||||
{
|
||||
b: 2,
|
||||
d: 'abc${123}'
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?c=3&d=abc%24%7B123%7D&a=1&b=2',
|
||||
query: {
|
||||
a: 1,
|
||||
b: 2,
|
||||
d: 'abc${123}'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('api:buildApi:autoAppend', () => {
|
||||
expect(
|
||||
buildApi(
|
||||
{
|
||||
method: 'get',
|
||||
url: '/api/xxx'
|
||||
},
|
||||
{
|
||||
a: 1,
|
||||
b: 2
|
||||
},
|
||||
{
|
||||
autoAppend: true
|
||||
}
|
||||
)
|
||||
).toMatchObject({
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=1&b=2'
|
||||
});
|
||||
});
|
||||
|
||||
test('api:isApiOutdated', () => {
|
||||
expect(
|
||||
isApiOutdated(
|
||||
'/api/xxx?a=${a}',
|
||||
'/api/xxx?a=${a}',
|
||||
{
|
||||
a: 1,
|
||||
b: 0
|
||||
},
|
||||
{
|
||||
a: 1,
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toBeFalsy();
|
||||
|
||||
expect(
|
||||
isApiOutdated(
|
||||
'/api/xxx?a=${a}',
|
||||
'/api/xxx?a=${a}',
|
||||
{
|
||||
a: 1,
|
||||
b: 0
|
||||
},
|
||||
{
|
||||
a: 2,
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toBeTruthy();
|
||||
|
||||
expect(
|
||||
isApiOutdated(
|
||||
'/api/xxx',
|
||||
'/api/xxx',
|
||||
{
|
||||
a: 1,
|
||||
b: 0
|
||||
},
|
||||
{
|
||||
a: 2,
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toBeFalsy();
|
||||
|
||||
expect(
|
||||
isApiOutdated(
|
||||
{
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=${a}'
|
||||
},
|
||||
{
|
||||
method: 'get',
|
||||
url: '/api/xxx?a=${a}',
|
||||
sendOn: 'this.b === 0'
|
||||
},
|
||||
{
|
||||
a: 1,
|
||||
b: 0
|
||||
},
|
||||
{
|
||||
a: 2,
|
||||
b: 2
|
||||
}
|
||||
)
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
test('api:cache', async () => {
|
||||
let count = 1;
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
a: count++
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
name: 'thepage',
|
||||
initApi: {
|
||||
method: 'get',
|
||||
url: '/api/xxx?id=${id}',
|
||||
cache: 2000
|
||||
},
|
||||
toolbar: {
|
||||
type: 'button',
|
||||
label: 'Reload',
|
||||
actionType: 'reload',
|
||||
target: 'thepage'
|
||||
},
|
||||
body: 'The variable value is ${a}'
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await wait(300);
|
||||
expect(container).toMatchSnapshot();
|
||||
|
||||
fireEvent.click(getByText(/Reload/));
|
||||
|
||||
await wait(300);
|
||||
expect(fetcher).toHaveBeenCalledTimes(1); // 只请求一次,第二次请求从缓存中取
|
||||
expect(container).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('api:isvalidapi', () => {
|
||||
expect(isValidApi('api/xxx')).toBeTruthy();
|
||||
expect(isValidApi('api/xxx?a=1')).toBeTruthy();
|
||||
expect(isValidApi('/x')).toBeTruthy();
|
||||
expect(isValidApi('/api/xxx?a=1&b=2&c=3')).toBeTruthy();
|
||||
expect(isValidApi('http://xxxdomain')).toBeTruthy();
|
||||
expect(isValidApi('http://xxxdomain/')).toBeTruthy();
|
||||
expect(isValidApi('http://xxxdomain/api')).toBeTruthy();
|
||||
expect(isValidApi('app://')).toBeFalsy();
|
||||
expect(isValidApi('app://x')).toBeTruthy();
|
||||
expect(isValidApi('app://x?a=1')).toBeTruthy();
|
||||
expect(isValidApi('app://x?a=1&b=2')).toBeTruthy();
|
||||
expect(isValidApi('app://x b?a=1&b=2')).toBeFalsy();
|
||||
expect(isValidApi('app://x%20b?a=1&b=2')).toBeTruthy();
|
||||
expect(isValidApi('ftp://127.0.0.1/xxx')).toBeTruthy();
|
||||
expect(isValidApi('wss://127.0.0.1/xxx')).toBeTruthy();
|
||||
expect(isValidApi('taf://127.0.0.1/xxx')).toBeTruthy();
|
||||
expect(
|
||||
isValidApi(
|
||||
'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/sample/${id}'
|
||||
)
|
||||
).toBeTruthy();
|
||||
|
||||
expect(
|
||||
isValidApi(
|
||||
'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/sample/${id ? "a" : "b"}'
|
||||
)
|
||||
).toBeTruthy();
|
||||
|
||||
expect(
|
||||
isValidApi(
|
||||
'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com/api/amis-mock/mock2/${id ? "a" : "b"}/abc'
|
||||
)
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
test('api:requestAdaptor1', async () => {
|
||||
const notify = jest.fn();
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const requestAdaptor = jest.fn().mockImplementation(api => {
|
||||
return Promise.resolve({
|
||||
...api,
|
||||
data: {
|
||||
...api.data,
|
||||
email: 'appended@test.com'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
requestAdaptor: requestAdaptor
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'name',
|
||||
label: '姓名:',
|
||||
value: 'fex'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(requestAdaptor).toHaveBeenCalled();
|
||||
expect(fetcher).toHaveBeenCalled();
|
||||
expect(fetcher.mock.calls[0][0].data).toMatchObject({
|
||||
name: 'fex',
|
||||
email: 'appended@test.com'
|
||||
});
|
||||
});
|
||||
|
||||
test('api:requestAdaptor2', async () => {
|
||||
const notify = jest.fn();
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const requestAdaptor = jest.fn().mockImplementation(api => {
|
||||
return Promise.resolve({
|
||||
...api,
|
||||
mockResponse: {
|
||||
status: 200,
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
requestAdaptor: requestAdaptor
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id',
|
||||
label: 'Id'
|
||||
},
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'name',
|
||||
label: '姓名:',
|
||||
value: 'fex'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(requestAdaptor).toHaveBeenCalled();
|
||||
expect(fetcher).toHaveBeenCalledTimes(0);
|
||||
expect(container.querySelector('input[name="id"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id"]') as any).value).toBe('2');
|
||||
});
|
||||
|
||||
test('api:responseData1', async () => {
|
||||
const notify = jest.fn();
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 0,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
responseData: {
|
||||
id: '${id}',
|
||||
id2: '${id + 1}'
|
||||
}
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id',
|
||||
label: 'Id'
|
||||
},
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id2',
|
||||
label: 'Id2'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
expect(container.querySelector('input[name="id"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id"]') as any).value).toBe('1');
|
||||
|
||||
expect(container.querySelector('input[name="id2"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id2"]') as any).value).toBe('2');
|
||||
});
|
||||
|
||||
test('api:responseData2', async () => {
|
||||
const notify = jest.fn();
|
||||
const fetcher = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
data: {
|
||||
status: 500,
|
||||
msg: 'ok',
|
||||
data: {
|
||||
id: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
const {container, getByText} = render(
|
||||
amisRender(
|
||||
{
|
||||
type: 'page',
|
||||
body: [
|
||||
{
|
||||
type: 'form',
|
||||
id: 'form_submit',
|
||||
submitText: '提交表单',
|
||||
api: {
|
||||
method: 'post',
|
||||
url: '/api/mock2/form/saveForm',
|
||||
responseData: {
|
||||
id: '${id}',
|
||||
id2: '${id + 1}'
|
||||
}
|
||||
},
|
||||
body: [
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id',
|
||||
label: 'Id'
|
||||
},
|
||||
{
|
||||
type: 'input-text',
|
||||
name: 'id2',
|
||||
label: 'Id2'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{},
|
||||
makeEnv({
|
||||
notify,
|
||||
fetcher
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getByText('提交表单')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
fireEvent.click(getByText(/提交表单/));
|
||||
await wait(300);
|
||||
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
expect(container.querySelector('input[name="id"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id"]') as any).value).toBe('1');
|
||||
await wait(500);
|
||||
|
||||
expect(container.querySelector('input[name="id2"]')).toBeInTheDocument();
|
||||
expect((container.querySelector('input[name="id2"]') as any).value).toBe('2');
|
||||
});
|
||||
9
packages/amis/__tests__/utils/filter.test.ts
Normal file
9
packages/amis/__tests__/utils/filter.test.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import {registerFilter, resolveVariableAndFilter} from '../../src';
|
||||
|
||||
test('filter:customFilter', () => {
|
||||
registerFilter('customFilter', input => `233`);
|
||||
|
||||
expect(resolveVariableAndFilter('${a | customFilter}', {a: 'abc'})).toEqual(
|
||||
'233'
|
||||
);
|
||||
});
|
||||
39
packages/amis/__tests__/utils/style.test.ts
Normal file
39
packages/amis/__tests__/utils/style.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import '../../src';
|
||||
|
||||
import {buildStyle} from '../../src';
|
||||
|
||||
test('style var background', () => {
|
||||
expect(
|
||||
buildStyle('${style}', {
|
||||
style: {
|
||||
backgroundImage: 'http://www.example.com/a.png'
|
||||
}
|
||||
})
|
||||
).toEqual({
|
||||
backgroundImage: 'url("http://www.example.com/a.png")'
|
||||
});
|
||||
|
||||
expect(
|
||||
buildStyle(
|
||||
{
|
||||
backgroundImage: 'http://www.example.com/a.png'
|
||||
},
|
||||
{}
|
||||
)
|
||||
).toEqual({
|
||||
backgroundImage: 'url("http://www.example.com/a.png")'
|
||||
});
|
||||
});
|
||||
|
||||
test('style case', () => {
|
||||
expect(
|
||||
buildStyle(
|
||||
{
|
||||
'font-size': '10'
|
||||
},
|
||||
{}
|
||||
)
|
||||
).toEqual({
|
||||
fontSize: '10'
|
||||
});
|
||||
});
|
||||
661
packages/amis/__tests__/utils/tpl-builtin.test.ts
Normal file
661
packages/amis/__tests__/utils/tpl-builtin.test.ts
Normal file
@@ -0,0 +1,661 @@
|
||||
import '../../src';
|
||||
import {resolveVariableAndFilter, dataMapping} from '../../src';
|
||||
import moment from 'moment';
|
||||
|
||||
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(`tpl-builtin:resolveVariableAndFilter:${f.type}`, () => {
|
||||
expect(resolveVariableAndFilter(f.path, f.data, f.filter)).toEqual(
|
||||
f.expectValue
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('tpl-builtin:dataMapping', () => {
|
||||
const data = {
|
||||
a: 1,
|
||||
b: '2',
|
||||
c: {
|
||||
'1': 'first',
|
||||
'2': 'second'
|
||||
}
|
||||
};
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
'&': '$$'
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
a: 1,
|
||||
b: '2',
|
||||
c: {
|
||||
'1': 'first',
|
||||
'2': 'second'
|
||||
}
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
'&': '${b}'
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual('2');
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
'&': (data: any) => ({b: data.b})
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
b: '2'
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
all: '$$'
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
all: {
|
||||
a: 1,
|
||||
b: '2',
|
||||
c: {
|
||||
'1': 'first',
|
||||
'2': 'second'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
a: '${a}',
|
||||
b: '__undefined'
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
a: 1
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
value: {
|
||||
a: '${a}',
|
||||
d: 'd'
|
||||
}
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
value: {
|
||||
a: 1,
|
||||
d: 'd'
|
||||
}
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
value: [
|
||||
{
|
||||
a: '${a}',
|
||||
d: 'd'
|
||||
},
|
||||
{
|
||||
b: '${b}',
|
||||
d: 'd'
|
||||
}
|
||||
]
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
value: [
|
||||
{
|
||||
a: 1,
|
||||
d: 'd'
|
||||
},
|
||||
{
|
||||
b: '2',
|
||||
d: 'd'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
value: '${a}'
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
value: 1
|
||||
});
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
value: (data: any) => data.a
|
||||
},
|
||||
data
|
||||
)
|
||||
).toEqual({
|
||||
value: 1
|
||||
});
|
||||
|
||||
const data2 = {
|
||||
rows: [
|
||||
{
|
||||
label: 'A',
|
||||
value: 'a',
|
||||
size: 'sm'
|
||||
},
|
||||
{
|
||||
label: 'B',
|
||||
value: 'b',
|
||||
size: 'md'
|
||||
},
|
||||
{
|
||||
label: 'C',
|
||||
value: 'c',
|
||||
size: 'sm'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
expect(
|
||||
dataMapping(
|
||||
{
|
||||
items: {
|
||||
$rows: {
|
||||
label: '$label',
|
||||
value: '$value'
|
||||
}
|
||||
}
|
||||
},
|
||||
data2
|
||||
)
|
||||
).toEqual({
|
||||
items: [
|
||||
{
|
||||
label: 'A',
|
||||
value: 'a'
|
||||
},
|
||||
{
|
||||
label: 'B',
|
||||
value: 'b'
|
||||
},
|
||||
{
|
||||
label: 'C',
|
||||
value: 'c'
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
19
packages/amis/__tests__/utils/tpl.test.ts
Normal file
19
packages/amis/__tests__/utils/tpl.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import {filter} from '../../src';
|
||||
|
||||
import '../../src';
|
||||
import '../../src';
|
||||
test('filter', () => {
|
||||
expect(
|
||||
filter('xxx_a=${a}&b=${b}', {
|
||||
a: 1,
|
||||
b: 2
|
||||
})
|
||||
).toEqual('xxx_a=1&b=2');
|
||||
|
||||
expect(
|
||||
filter('xxx_a=<%= data.a%>&b=<%= data.b%>', {
|
||||
a: 1,
|
||||
b: 2
|
||||
})
|
||||
).toEqual('xxx_a=1&b=2');
|
||||
});
|
||||
1472
packages/amis/__tests__/utils/validations.test.ts
Normal file
1472
packages/amis/__tests__/utils/validations.test.ts
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user