Skip to content

Commit 5cadd29

Browse files
fix unit tests
1 parent 5a9d089 commit 5cadd29

File tree

9 files changed

+1212
-287
lines changed

9 files changed

+1212
-287
lines changed

client/packages/lowcoder/src/comps/comps/allComp.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ const COMPS_MAP = {
6565

6666
Object.entries(uiCompRegistry).forEach(async ([key, value]) => {
6767
if(value.lazyLoad) {
68-
COMPS_MAP["ui_" + key] = await import(value.compPath!);
68+
const module = await import(`../${value.compPath}`!);
69+
COMPS_MAP["ui_" + key] = module[value.compName!];
6970
} else {
7071
COMPS_MAP["ui_" + key] = value.comp!;
7172
}

client/packages/lowcoder/src/comps/comps/containerComp/containerView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ const onDrop = async (
228228

229229
let defaultDataFn = undefined;
230230
if(defaultDataFnName && defaultDataFnPath) {
231-
const module = await import(defaultDataFnPath);
231+
const module = await import(`../../${defaultDataFnPath}`);
232232
defaultDataFn = module[defaultDataFnName];
233233
}
234234
console.log(defaultDataFn);

client/packages/lowcoder/src/comps/comps/lazyLoadComp/lazyLoadComp.test.tsx

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,18 @@ import { withSimpleExposing } from "comps/generators/withExposing";
44
import { withMethodExposing } from "comps/generators/withMethodExposing";
55
import { evalAndReduce } from "comps/utils";
66
import { customAction } from "lowcoder-core";
7-
import { RemoteCompInfo } from "types/remoteComp";
87
import { lazyLoadComp } from "./lazyLoadComp";
98

10-
const npmRemoteInfo: RemoteCompInfo = {
11-
isRemote: true,
12-
source: "npm",
13-
packageName: "p",
14-
packageVersion: "v",
15-
compName: "n",
16-
};
9+
const TestComp = valueComp<number>(123);
10+
export { TestComp };
1711

18-
let TestComp = simpleMultiComp({ hello: valueComp<number>(123) });
19-
TestComp = withSimpleExposing(TestComp, (comp) => {
20-
return {
21-
hello: comp.children.hello.getView(),
22-
};
23-
});
24-
TestComp = withMethodExposing(TestComp, [
25-
{
26-
method: {
27-
name: "add",
28-
params: [{ name: "value", type: "number" }],
29-
},
30-
execute: (comp, values) => {
31-
const hello = comp.children.hello;
32-
hello.dispatchChangeValueAction(hello.getView() + (values[0] as number));
33-
},
34-
},
35-
]);
36-
export {
37-
TestComp,
38-
};
12+
const compName = 'TestComp';
13+
const compPath = 'comps/lazyLoadComp/lazyLoadComp.test.tsx';
3914

40-
// const RComp = lazyLoadComp('TestComp', './lazyLoadComp.test.tsx');
15+
const RComp = lazyLoadComp(compName, compPath);
4116

4217
test("lazyload comp", async () => {
4318
let c: any = null;
44-
const RComp = await lazyLoadComp('TestComp', './lazyLoadComp.test.tsx');
4519
c = new RComp({
4620
dispatch: (action) => {
4721
if (c) {
@@ -60,7 +34,6 @@ test("lazyload comp", async () => {
6034

6135
test("lazyload comp keep values", async () => {
6236
let c: any = null;
63-
const RComp = await lazyLoadComp('TestComp', './lazyLoadComp.test.tsx');
6437
c = new RComp({
6538
dispatch: (action) => {
6639
if (c) {
@@ -69,21 +42,20 @@ test("lazyload comp keep values", async () => {
6942
},
7043
value: 456,
7144
});
72-
45+
7346
expect(c.toJsonValue()).toBe(456);
7447
await c.load();
7548
expect(c.toJsonValue()).toBe(456);
7649
});
7750

7851
test("lazyload comp exposing data", async () => {
79-
// const EComp = lazyLoadComp('comp-path', async () => {
80-
// return withSimpleExposing(simpleMultiComp({ hello: valueComp(123) }), (comp) => {
81-
// return {
82-
// hello: comp.children.hello.getView(),
83-
// };
84-
// });
85-
// });
86-
const EComp = await lazyLoadComp('TestComp', './lazyLoadComp.test.tsx');
52+
const EComp = lazyLoadComp(compName, compPath, async () => {
53+
return withSimpleExposing(simpleMultiComp({ hello: valueComp(123) }), (comp) => {
54+
return {
55+
hello: comp.children.hello.getView(),
56+
};
57+
});
58+
});
8759

8860
let c: any = null;
8961
c = new EComp({
@@ -94,27 +66,26 @@ test("lazyload comp exposing data", async () => {
9466
},
9567
});
9668

97-
await c.load();
69+
await c.load();
9870
const c1 = evalAndReduce(c);
9971
expect(c1.exposingValues.hello).toBe(123);
10072
});
10173

10274
test("lazyload comp execute method", async () => {
103-
// const MComp = lazyLoadComp('comp-path', async () => {
104-
// return withMethodExposing(simpleMultiComp({ hello: valueComp<number>(123) }), [
105-
// {
106-
// method: {
107-
// name: "add",
108-
// params: [{ name: "value", type: "number" }],
109-
// },
110-
// execute: (comp, values) => {
111-
// const hello = comp.children.hello;
112-
// hello.dispatchChangeValueAction(hello.getView() + (values[0] as number));
113-
// },
114-
// },
115-
// ]);
116-
// });
117-
const MComp = await lazyLoadComp('TestComp', './lazyLoadComp.test.tsx');
75+
const MComp = lazyLoadComp(compName, compPath, async () => {
76+
return withMethodExposing(simpleMultiComp({ hello: valueComp<number>(123) }), [
77+
{
78+
method: {
79+
name: "add",
80+
params: [{ name: "value", type: "number" }],
81+
},
82+
execute: (comp, values) => {
83+
const hello = comp.children.hello;
84+
hello.dispatchChangeValueAction(hello.getView() + (values[0] as number));
85+
},
86+
},
87+
]);
88+
});
11889
let c: any = null;
11990
c = new MComp({
12091
dispatch: (action) => {

client/packages/lowcoder/src/comps/comps/lazyLoadComp/lazyLoadComp.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { simpleMultiComp } from "comps/generators";
33
import { withExposingConfigs } from "comps/generators/withExposing";
44
import { GreyTextColor } from "constants/style";
55
import log from "loglevel";
6-
import { Comp, CompAction, CompParams, customAction, isCustomAction } from "lowcoder-core";
6+
import { Comp, CompAction, CompConstructor, CompParams, customAction, isCustomAction } from "lowcoder-core";
77
import { WhiteLoading } from "lowcoder-design";
88
import { useState } from "react";
99
import { useMount } from "react-use";
@@ -82,9 +82,12 @@ function RemoteCompView(props: React.PropsWithChildren<RemoteCompViewProps>) {
8282
);
8383
}
8484

85+
export type LazyloadCompLoader<T = RemoteCompInfo> = () => Promise<CompConstructor | null>;
86+
8587
export function lazyLoadComp(
8688
compName?: string,
8789
compPath?: string,
90+
loader?: LazyloadCompLoader,
8891
loadingElement?: () => React.ReactNode
8992
) {
9093
class LazyLoadComp extends simpleMultiComp({}) {
@@ -100,17 +103,15 @@ export function lazyLoadComp(
100103
if (!compPath) {
101104
return;
102105
}
103-
// let finalLoader = loader;
104-
// if (!loader) {
105-
// finalLoader = loaders[remoteInfo.source];
106-
// }
107-
// if (!finalLoader) {
108-
// log.error("loader not found, remote info:", compPath);
109-
// return;
110-
// }
111-
const module = await import(compPath);
112-
const RemoteExportedComp = module[compName!];
106+
let RemoteExportedComp;
107+
if (!loader) {
108+
const module = await import(`../../${compPath}`);
109+
RemoteExportedComp = module[compName!];
110+
} else {
111+
RemoteExportedComp = await loader();
112+
}
113113
if (!RemoteExportedComp) {
114+
log.error("loader not found, lazy load info:", compPath);
114115
return;
115116
}
116117

@@ -135,7 +136,7 @@ export function lazyLoadComp(
135136

136137
getView() {
137138
// const key = `${remoteInfo?.packageName}-${remoteInfo?.packageVersion}-${remoteInfo?.compName}`;
138-
const key = `${compPath}`;
139+
const key = `${compName}`;
139140
return (
140141
<RemoteCompView key={key} loadComp={() => this.load()} loadingElement={loadingElement} />
141142
);

client/packages/lowcoder/src/comps/comps/moduleComp/moduleComp.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "comps";
2-
import { loadComps } from "comps";
2+
import { loadComps } from "comps/index-test";
33
import { ExecuteAction } from "comps/controls/actionSelector/executeCompTypes";
44
import { getCompContainer } from "comps/utils/useCompInstance";
55
import { ModuleLayoutCompName } from "constants/compConstants";
@@ -298,19 +298,15 @@ function afterInitModule(callback: (ret: InitModuleReturn) => void) {
298298
}
299299

300300
beforeAll(async () => {
301-
jest.setTimeout(30000);
302301
await loadComps();
303302
});
304303

305304
describe("module comp", () => {
306305
test("init module to ready", (done) => {
307306
afterInitModule(({ module, text1, text2 }) => {
308-
console.log('module', module());
309-
console.log('text1', text1());
310-
console.log('text2', text2());
311307
// outputs
312308
expect(Object.keys(module().getOutputNodes())).toStrictEqual(["out"]);
313-
expect(module().exposingValues.out).toBe("");
309+
expect(module().exposingValues.out).toBe("hello");
314310

315311
// inputs
316312
expect(Object.keys(module().children.inputs.getInputNodes())).toStrictEqual([]);

0 commit comments

Comments
 (0)