mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-26 11:59:41 +00:00
test: add ut for utils/build-components
This commit is contained in:
parent
f3f7703f98
commit
bbe438cf83
22
.github/workflows/cov packages.yml
vendored
22
.github/workflows/cov packages.yml
vendored
@ -72,3 +72,25 @@ jobs:
|
||||
test-script: npm test -- --jest-ci --jest-json --jest-coverage --jest-testLocationInResults --jest-outputFile=report.json
|
||||
package-manager: yarn
|
||||
annotations: none
|
||||
|
||||
cov-utils:
|
||||
runs-on: ubuntu-latest
|
||||
# skip fork's PR, otherwise it fails while making a comment
|
||||
if: ${{ github.event.pull_request.head.repo.full_name == 'alibaba/lowcode-engine' }}
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: '14'
|
||||
|
||||
- name: install
|
||||
run: npm i && npm run setup:skip-build
|
||||
|
||||
- uses: ArtiomTr/jest-coverage-report-action@v2
|
||||
with:
|
||||
working-directory: packages/utils
|
||||
test-script: npm test
|
||||
package-manager: yarn
|
||||
annotations: none
|
||||
336
packages/utils/test/src/build-components/buildComponents.test.ts
Normal file
336
packages/utils/test/src/build-components/buildComponents.test.ts
Normal file
@ -0,0 +1,336 @@
|
||||
|
||||
import { buildComponents } from "../../../src/build-components";
|
||||
|
||||
function Button() {};
|
||||
|
||||
function WrapButton() {};
|
||||
|
||||
function ButtonGroup() {};
|
||||
|
||||
function WrapButtonGroup() {};
|
||||
|
||||
ButtonGroup.Button = Button;
|
||||
|
||||
Button.displayName = "Button";
|
||||
ButtonGroup.displayName = "ButtonGroup";
|
||||
ButtonGroup.prototype.isReactComponent = true;
|
||||
Button.prototype.isReactComponent = true;
|
||||
|
||||
jest.mock('../../../src/is-react', () => {
|
||||
const original = jest.requireActual('../../../src/is-react');
|
||||
return {
|
||||
...original,
|
||||
wrapReactClass(view) {
|
||||
return view;
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
describe('build-component', () => {
|
||||
it('basic button', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': {
|
||||
Button,
|
||||
}
|
||||
},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Button',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'Button',
|
||||
subName: 'Button',
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
});
|
||||
});
|
||||
|
||||
it('component is a __esModule', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': {
|
||||
__esModule: true,
|
||||
default: Button,
|
||||
}
|
||||
},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Button',
|
||||
package: '@alilc/button',
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
});
|
||||
})
|
||||
|
||||
it('basic warp button', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': {
|
||||
WrapButton,
|
||||
}
|
||||
},
|
||||
{
|
||||
WrapButton: {
|
||||
componentName: 'WrapButton',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'WrapButton',
|
||||
subName: 'WrapButton',
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
WrapButton,
|
||||
});
|
||||
});
|
||||
|
||||
it('destructuring is false button', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': Button
|
||||
},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Button',
|
||||
package: '@alilc/button',
|
||||
destructuring: false,
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
});
|
||||
});
|
||||
|
||||
it('Button and ButtonGroup', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': {
|
||||
Button,
|
||||
ButtonGroup,
|
||||
}
|
||||
},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Button',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'Button',
|
||||
subName: 'Button',
|
||||
},
|
||||
ButtonGroup: {
|
||||
componentName: 'ButtonGroup',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'ButtonGroup',
|
||||
subName: 'ButtonGroup',
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
ButtonGroup,
|
||||
});
|
||||
});
|
||||
|
||||
it('ButtonGroup and ButtonGroup.Button', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': {
|
||||
ButtonGroup,
|
||||
}
|
||||
},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Button',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'ButtonGroup',
|
||||
subName: 'ButtonGroup.Button',
|
||||
},
|
||||
ButtonGroup: {
|
||||
componentName: 'ButtonGroup',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'ButtonGroup',
|
||||
subName: 'ButtonGroup',
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
ButtonGroup,
|
||||
});
|
||||
});
|
||||
|
||||
it('ButtonGroup.default and ButtonGroup.Button', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': ButtonGroup,
|
||||
},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Button',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'Button',
|
||||
subName: 'Button',
|
||||
},
|
||||
ButtonGroup: {
|
||||
componentName: 'ButtonGroup',
|
||||
package: '@alilc/button',
|
||||
destructuring: true,
|
||||
exportName: 'default',
|
||||
subName: 'default',
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
ButtonGroup,
|
||||
});
|
||||
});
|
||||
|
||||
it('no npm component', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{
|
||||
'@alilc/button': Button,
|
||||
},
|
||||
{
|
||||
Button: null,
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({});
|
||||
});
|
||||
|
||||
it('no npm component and global button', () => {
|
||||
window.Button = Button;
|
||||
expect(
|
||||
buildComponents(
|
||||
{},
|
||||
{
|
||||
Button: null,
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
});
|
||||
window.Button = null;
|
||||
});
|
||||
|
||||
it('componentsMap value is component funtion', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{},
|
||||
{
|
||||
Button,
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('componentsMap value is component', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{},
|
||||
{
|
||||
Button: WrapButton,
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button: WrapButton,
|
||||
});
|
||||
});
|
||||
|
||||
it('componentsMap value is mix component', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{},
|
||||
{
|
||||
Button: {
|
||||
WrapButton,
|
||||
Button,
|
||||
ButtonGroup,
|
||||
},
|
||||
},
|
||||
() => {},
|
||||
))
|
||||
.toEqual({
|
||||
Button: {
|
||||
WrapButton,
|
||||
Button,
|
||||
ButtonGroup,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('componentsMap value is Lowcode Component', () => {
|
||||
expect(
|
||||
buildComponents(
|
||||
{},
|
||||
{
|
||||
Button: {
|
||||
componentName: 'Component',
|
||||
schema: {},
|
||||
},
|
||||
},
|
||||
(component) => {
|
||||
return component as any;
|
||||
},
|
||||
))
|
||||
.toEqual({
|
||||
Button: {
|
||||
componentName: 'Component',
|
||||
schema: {},
|
||||
},
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
describe('build div component', () => {
|
||||
it('build div component', () => {
|
||||
const components = buildComponents(
|
||||
{
|
||||
'@alilc/div': 'div'
|
||||
},
|
||||
{
|
||||
div: {
|
||||
componentName: 'div',
|
||||
package: '@alilc/div'
|
||||
}
|
||||
},
|
||||
() => {},
|
||||
);
|
||||
|
||||
expect(components['div']).not.toBeNull();
|
||||
})
|
||||
})
|
||||
@ -0,0 +1,43 @@
|
||||
import { getProjectUtils } from "../../../src/build-components";
|
||||
|
||||
const sampleUtil = () => 'I am a sample util';
|
||||
const sampleUtil2 = () => 'I am a sample util 2';
|
||||
|
||||
describe('get project utils', () => {
|
||||
it('get utils with destructuring true', () => {
|
||||
expect(getProjectUtils(
|
||||
{
|
||||
'@alilc/utils': {
|
||||
destructuring: true,
|
||||
sampleUtil,
|
||||
sampleUtil2,
|
||||
}
|
||||
},
|
||||
[{
|
||||
name: 'sampleUtils',
|
||||
npm: {
|
||||
package: '@alilc/utils'
|
||||
}
|
||||
}]
|
||||
)).toEqual({
|
||||
sampleUtil,
|
||||
sampleUtil2,
|
||||
})
|
||||
});
|
||||
|
||||
it('get utils with name', () => {
|
||||
expect(getProjectUtils(
|
||||
{
|
||||
'@alilc/utils': sampleUtil
|
||||
},
|
||||
[{
|
||||
name: 'sampleUtil',
|
||||
npm: {
|
||||
package: '@alilc/utils'
|
||||
}
|
||||
}]
|
||||
)).toEqual({
|
||||
sampleUtil,
|
||||
})
|
||||
});
|
||||
})
|
||||
@ -0,0 +1,85 @@
|
||||
import { getSubComponent } from '../../../src/build-components';
|
||||
|
||||
function Button() {}
|
||||
|
||||
function ButtonGroup() {}
|
||||
|
||||
ButtonGroup.Button = Button;
|
||||
|
||||
function OnlyButtonGroup() {}
|
||||
|
||||
describe('getSubComponent library is object', () => {
|
||||
it('get Button from Button', () => {
|
||||
expect(getSubComponent({
|
||||
Button,
|
||||
}, ['Button'])).toBe(Button);
|
||||
});
|
||||
|
||||
it('get ButtonGroup.Button from ButtonGroup', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup,
|
||||
}, ['ButtonGroup', 'Button'])).toBe(Button);
|
||||
});
|
||||
|
||||
it('get ButtonGroup from ButtonGroup', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup,
|
||||
}, ['ButtonGroup'])).toBe(ButtonGroup);
|
||||
});
|
||||
|
||||
it('get ButtonGroup.Button from OnlyButtonGroup', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup: OnlyButtonGroup,
|
||||
}, ['ButtonGroup', 'Button'])).toBe(OnlyButtonGroup);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSubComponent library is null', () => {
|
||||
it('getSubComponent library is null', () => {
|
||||
expect(getSubComponent(null, ['ButtonGroup', 'Button'])).toBeNull();
|
||||
});
|
||||
})
|
||||
|
||||
describe('getSubComponent paths is []', () => {
|
||||
it('getSubComponent paths is []', () => {
|
||||
expect(getSubComponent(Button, [])).toBe(Button);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getSubComponent make error', () => {
|
||||
it('library is string', () => {
|
||||
expect(getSubComponent(true, ['Button'])).toBe(null);
|
||||
});
|
||||
|
||||
it('library is boolean', () => {
|
||||
expect(getSubComponent('I am a string', ['Button'])).toBe(null);
|
||||
});
|
||||
|
||||
it('library is number', () => {
|
||||
expect(getSubComponent(123, ['Button'])).toBe(null);
|
||||
});
|
||||
|
||||
it('library ButtonGroup is null', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup: null,
|
||||
}, ['ButtonGroup', 'Button'])).toBe(null);
|
||||
});
|
||||
|
||||
it('library ButtonGroup.Button is null', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup: null,
|
||||
}, ['ButtonGroup', 'Button', 'SubButton'])).toBe(null);
|
||||
});
|
||||
|
||||
it('path s is [[]]', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup: null,
|
||||
}, [['ButtonGroup'] as any, 'Button'])).toBe(null);
|
||||
});
|
||||
|
||||
it('ButtonGroup is undefined', () => {
|
||||
expect(getSubComponent({
|
||||
ButtonGroup: undefined,
|
||||
}, ['ButtonGroup', 'Button'])).toBe(null);
|
||||
});
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user