Skip to content

Commit bbe438c

Browse files
liujupingJackLian
authored andcommitted
test: add ut for utils/build-components
1 parent f3f7703 commit bbe438c

File tree

4 files changed

+486
-0
lines changed

4 files changed

+486
-0
lines changed

.github/workflows/cov packages.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,26 @@ jobs:
7171
working-directory: packages/react-simulator-renderer
7272
test-script: npm test -- --jest-ci --jest-json --jest-coverage --jest-testLocationInResults --jest-outputFile=report.json
7373
package-manager: yarn
74+
annotations: none
75+
76+
cov-utils:
77+
runs-on: ubuntu-latest
78+
# skip fork's PR, otherwise it fails while making a comment
79+
if: ${{ github.event.pull_request.head.repo.full_name == 'alibaba/lowcode-engine' }}
80+
steps:
81+
- name: checkout
82+
uses: actions/checkout@v2
83+
84+
- uses: actions/setup-node@v2
85+
with:
86+
node-version: '14'
87+
88+
- name: install
89+
run: npm i && npm run setup:skip-build
90+
91+
- uses: ArtiomTr/jest-coverage-report-action@v2
92+
with:
93+
working-directory: packages/utils
94+
test-script: npm test
95+
package-manager: yarn
7496
annotations: none
Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
2+
import { buildComponents } from "../../../src/build-components";
3+
4+
function Button() {};
5+
6+
function WrapButton() {};
7+
8+
function ButtonGroup() {};
9+
10+
function WrapButtonGroup() {};
11+
12+
ButtonGroup.Button = Button;
13+
14+
Button.displayName = "Button";
15+
ButtonGroup.displayName = "ButtonGroup";
16+
ButtonGroup.prototype.isReactComponent = true;
17+
Button.prototype.isReactComponent = true;
18+
19+
jest.mock('../../../src/is-react', () => {
20+
const original = jest.requireActual('../../../src/is-react');
21+
return {
22+
...original,
23+
wrapReactClass(view) {
24+
return view;
25+
}
26+
}
27+
})
28+
29+
describe('build-component', () => {
30+
it('basic button', () => {
31+
expect(
32+
buildComponents(
33+
{
34+
'@alilc/button': {
35+
Button,
36+
}
37+
},
38+
{
39+
Button: {
40+
componentName: 'Button',
41+
package: '@alilc/button',
42+
destructuring: true,
43+
exportName: 'Button',
44+
subName: 'Button',
45+
}
46+
},
47+
() => {},
48+
))
49+
.toEqual({
50+
Button,
51+
});
52+
});
53+
54+
it('component is a __esModule', () => {
55+
expect(
56+
buildComponents(
57+
{
58+
'@alilc/button': {
59+
__esModule: true,
60+
default: Button,
61+
}
62+
},
63+
{
64+
Button: {
65+
componentName: 'Button',
66+
package: '@alilc/button',
67+
}
68+
},
69+
() => {},
70+
))
71+
.toEqual({
72+
Button,
73+
});
74+
})
75+
76+
it('basic warp button', () => {
77+
expect(
78+
buildComponents(
79+
{
80+
'@alilc/button': {
81+
WrapButton,
82+
}
83+
},
84+
{
85+
WrapButton: {
86+
componentName: 'WrapButton',
87+
package: '@alilc/button',
88+
destructuring: true,
89+
exportName: 'WrapButton',
90+
subName: 'WrapButton',
91+
}
92+
},
93+
() => {},
94+
))
95+
.toEqual({
96+
WrapButton,
97+
});
98+
});
99+
100+
it('destructuring is false button', () => {
101+
expect(
102+
buildComponents(
103+
{
104+
'@alilc/button': Button
105+
},
106+
{
107+
Button: {
108+
componentName: 'Button',
109+
package: '@alilc/button',
110+
destructuring: false,
111+
}
112+
},
113+
() => {},
114+
))
115+
.toEqual({
116+
Button,
117+
});
118+
});
119+
120+
it('Button and ButtonGroup', () => {
121+
expect(
122+
buildComponents(
123+
{
124+
'@alilc/button': {
125+
Button,
126+
ButtonGroup,
127+
}
128+
},
129+
{
130+
Button: {
131+
componentName: 'Button',
132+
package: '@alilc/button',
133+
destructuring: true,
134+
exportName: 'Button',
135+
subName: 'Button',
136+
},
137+
ButtonGroup: {
138+
componentName: 'ButtonGroup',
139+
package: '@alilc/button',
140+
destructuring: true,
141+
exportName: 'ButtonGroup',
142+
subName: 'ButtonGroup',
143+
}
144+
},
145+
() => {},
146+
))
147+
.toEqual({
148+
Button,
149+
ButtonGroup,
150+
});
151+
});
152+
153+
it('ButtonGroup and ButtonGroup.Button', () => {
154+
expect(
155+
buildComponents(
156+
{
157+
'@alilc/button': {
158+
ButtonGroup,
159+
}
160+
},
161+
{
162+
Button: {
163+
componentName: 'Button',
164+
package: '@alilc/button',
165+
destructuring: true,
166+
exportName: 'ButtonGroup',
167+
subName: 'ButtonGroup.Button',
168+
},
169+
ButtonGroup: {
170+
componentName: 'ButtonGroup',
171+
package: '@alilc/button',
172+
destructuring: true,
173+
exportName: 'ButtonGroup',
174+
subName: 'ButtonGroup',
175+
}
176+
},
177+
() => {},
178+
))
179+
.toEqual({
180+
Button,
181+
ButtonGroup,
182+
});
183+
});
184+
185+
it('ButtonGroup.default and ButtonGroup.Button', () => {
186+
expect(
187+
buildComponents(
188+
{
189+
'@alilc/button': ButtonGroup,
190+
},
191+
{
192+
Button: {
193+
componentName: 'Button',
194+
package: '@alilc/button',
195+
destructuring: true,
196+
exportName: 'Button',
197+
subName: 'Button',
198+
},
199+
ButtonGroup: {
200+
componentName: 'ButtonGroup',
201+
package: '@alilc/button',
202+
destructuring: true,
203+
exportName: 'default',
204+
subName: 'default',
205+
}
206+
},
207+
() => {},
208+
))
209+
.toEqual({
210+
Button,
211+
ButtonGroup,
212+
});
213+
});
214+
215+
it('no npm component', () => {
216+
expect(
217+
buildComponents(
218+
{
219+
'@alilc/button': Button,
220+
},
221+
{
222+
Button: null,
223+
},
224+
() => {},
225+
))
226+
.toEqual({});
227+
});
228+
229+
it('no npm component and global button', () => {
230+
window.Button = Button;
231+
expect(
232+
buildComponents(
233+
{},
234+
{
235+
Button: null,
236+
},
237+
() => {},
238+
))
239+
.toEqual({
240+
Button,
241+
});
242+
window.Button = null;
243+
});
244+
245+
it('componentsMap value is component funtion', () => {
246+
expect(
247+
buildComponents(
248+
{},
249+
{
250+
Button,
251+
},
252+
() => {},
253+
))
254+
.toEqual({
255+
Button,
256+
});
257+
});
258+
259+
260+
it('componentsMap value is component', () => {
261+
expect(
262+
buildComponents(
263+
{},
264+
{
265+
Button: WrapButton,
266+
},
267+
() => {},
268+
))
269+
.toEqual({
270+
Button: WrapButton,
271+
});
272+
});
273+
274+
it('componentsMap value is mix component', () => {
275+
expect(
276+
buildComponents(
277+
{},
278+
{
279+
Button: {
280+
WrapButton,
281+
Button,
282+
ButtonGroup,
283+
},
284+
},
285+
() => {},
286+
))
287+
.toEqual({
288+
Button: {
289+
WrapButton,
290+
Button,
291+
ButtonGroup,
292+
},
293+
});
294+
});
295+
296+
it('componentsMap value is Lowcode Component', () => {
297+
expect(
298+
buildComponents(
299+
{},
300+
{
301+
Button: {
302+
componentName: 'Component',
303+
schema: {},
304+
},
305+
},
306+
(component) => {
307+
return component as any;
308+
},
309+
))
310+
.toEqual({
311+
Button: {
312+
componentName: 'Component',
313+
schema: {},
314+
},
315+
});
316+
})
317+
});
318+
319+
describe('build div component', () => {
320+
it('build div component', () => {
321+
const components = buildComponents(
322+
{
323+
'@alilc/div': 'div'
324+
},
325+
{
326+
div: {
327+
componentName: 'div',
328+
package: '@alilc/div'
329+
}
330+
},
331+
() => {},
332+
);
333+
334+
expect(components['div']).not.toBeNull();
335+
})
336+
})

0 commit comments

Comments
 (0)