2019-04-30 11:11:25 +08:00
|
|
|
import {
|
|
|
|
registerRenderer,
|
|
|
|
unRegisterRenderer,
|
|
|
|
RendererProps,
|
|
|
|
render as amisRender
|
|
|
|
} from '../src/factory';
|
|
|
|
import React = require('react');
|
|
|
|
import {render, fireEvent, cleanup} from 'react-testing-library';
|
2019-05-18 00:50:23 +08:00
|
|
|
import { wait, makeEnv } from './helper';
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2019-05-18 00:50:23 +08:00
|
|
|
test('factory unregistered Renderer', async () => {
|
2019-04-30 11:11:25 +08:00
|
|
|
const {
|
|
|
|
container,
|
|
|
|
} = render(amisRender({
|
|
|
|
type: 'my-renderer',
|
|
|
|
a: 23
|
|
|
|
}));
|
2019-05-18 00:50:23 +08:00
|
|
|
await wait(100);
|
|
|
|
expect(container).toMatchSnapshot(); // not found
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2019-05-18 00:50:23 +08:00
|
|
|
test('factory custom loadRenderer', async () => {
|
|
|
|
const {
|
|
|
|
container,
|
|
|
|
} = render(amisRender({
|
|
|
|
type: 'my-renderer',
|
|
|
|
a: 23
|
|
|
|
}, {}, makeEnv({
|
|
|
|
loadRenderer: () => Promise.resolve(() => (<div>Not Found</div>))
|
|
|
|
})));
|
|
|
|
await wait(100);
|
2019-04-30 11:11:25 +08:00
|
|
|
expect(container).toMatchSnapshot(); // not found
|
|
|
|
});
|
|
|
|
|
2019-05-18 01:05:16 +08:00
|
|
|
test('factory load Renderer on need', async () => {
|
|
|
|
const {
|
|
|
|
container,
|
|
|
|
} = 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$/
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.resolve(({
|
2019-05-18 22:00:59 +08:00
|
|
|
retry
|
|
|
|
}) => {
|
|
|
|
retry();
|
|
|
|
return null;
|
|
|
|
})
|
2019-05-18 01:05:16 +08:00
|
|
|
}
|
|
|
|
})));
|
|
|
|
await wait(200);
|
|
|
|
expect(container).toMatchSnapshot(); // not found
|
|
|
|
});
|
|
|
|
|
2019-04-30 11:11:25 +08:00
|
|
|
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);
|
|
|
|
});
|