Skip to content

Commit 6160056

Browse files
liujupingJackLian
authored andcommitted
fix(utils): isReactComponent not including react.memo
1 parent f1ff1a0 commit 6160056

File tree

8 files changed

+124
-11
lines changed

8 files changed

+124
-11
lines changed

.github/workflows/cov packages.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
package-manager: yarn
7474
annotations: none
7575

76-
cov-utils:
76+
cov-utils:
7777
runs-on: ubuntu-latest
7878
# skip fork's PR, otherwise it fails while making a comment
7979
if: ${{ github.event.pull_request.head.repo.full_name == 'alibaba/lowcode-engine' }}
@@ -91,6 +91,6 @@ cov-utils:
9191
- uses: ArtiomTr/jest-coverage-report-action@v2
9292
with:
9393
working-directory: packages/utils
94-
test-script: npm test
94+
test-script: npm test -- --jest-ci --jest-json --jest-coverage --jest-testLocationInResults --jest-outputFile=report.json
9595
package-manager: yarn
9696
annotations: none

.github/workflows/test packages.yml

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- name: test
4444
run: cd packages/designer && npm test
4545

46-
editor-skeleton:
46+
test-editor-skeleton:
4747
runs-on: ubuntu-latest
4848
steps:
4949
- name: checkout
@@ -57,4 +57,52 @@ jobs:
5757
run: npm i && npm run setup:skip-build
5858

5959
- name: test
60-
run: cd packages/editor-skeleton && npm test
60+
run: cd packages/editor-skeleton && npm test
61+
62+
test-renderer-core:
63+
runs-on: ubuntu-latest
64+
steps:
65+
- name: checkout
66+
uses: actions/checkout@v2
67+
68+
- uses: actions/setup-node@v2
69+
with:
70+
node-version: '14'
71+
72+
- name: install
73+
run: npm i && npm run setup:skip-build
74+
75+
- name: test
76+
run: cd packages/renderer-core && npm test
77+
78+
test-react-simulator-renderer:
79+
runs-on: ubuntu-latest
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+
- name: test
92+
run: cd packages/react-simulator-renderer && npm test
93+
94+
test-utils:
95+
runs-on: ubuntu-latest
96+
steps:
97+
- name: checkout
98+
uses: actions/checkout@v2
99+
100+
- uses: actions/setup-node@v2
101+
with:
102+
node-version: '14'
103+
104+
- name: install
105+
run: npm i && npm run setup:skip-build
106+
107+
- name: test
108+
run: cd packages/utils && npm test

packages/renderer-core/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const jestConfig = {
1111
// },
1212
// testMatch: ['(/tests?/.*(test))\\.[jt]s$'],
1313
// testMatch: ['**/*/base.test.tsx'],
14+
// testMatch: ['**/utils/common.test.ts'],
1415
transformIgnorePatterns: [
1516
`/node_modules/(?!${esModules})/`,
1617
],

packages/renderer-core/tests/utils/common.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe('test parseThisRequiredExpression', () => {
374374
};
375375
const fn = logger.error = jest.fn();
376376
parseThisRequiredExpression(mockExpression, { state: { text: 'text' } });
377-
expect(fn).toBeCalledWith('parseExpression.error', new ReferenceError('state is not defined'), {"type": "JSExpression", "value": "state.text"}, {"state": {"text": "text"}});
377+
expect(fn).toBeCalledWith(' parseExpression.error', new ReferenceError('state is not defined'), {"type": "JSExpression", "value": "state.text"}, {"state": {"text": "text"}});
378378
});
379379

380380
it('[success] JSExpression handle without this use scopeValue', () => {

packages/utils/jest.config.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
module.exports = {
1+
const fs = require('fs');
2+
const { join } = require('path');
3+
const pkgNames = fs.readdirSync(join('..')).filter(pkgName => !pkgName.startsWith('.'));
4+
5+
const jestConfig = {
26
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
3-
collectCoverage: true,
7+
collectCoverage: false,
48
collectCoverageFrom: [
5-
'src/**/*.{ts,tsx}',
9+
'src/**/*.ts',
10+
'!src/**/*.d.ts',
611
'!**/node_modules/**',
712
'!**/vendor/**',
813
],
914
};
15+
16+
// 只对本仓库内的 pkg 做 mapping
17+
jestConfig.moduleNameMapper = {};
18+
jestConfig.moduleNameMapper[`^@alilc/lowcode\\-(${pkgNames.join('|')})$`] = '<rootDir>/../$1/src';
19+
20+
module.exports = jestConfig;

packages/utils/src/is-react.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { cloneEnumerableProperty } from './clone-enumerable-property';
33

44
const hasSymbol = typeof Symbol === 'function' && Symbol.for;
55
const REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0;
6+
const REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3;
67

78
export function isReactClass(obj: any): obj is ComponentClass<any> {
89
return obj && obj.prototype && (obj.prototype.isReactComponent || obj.prototype instanceof Component);
@@ -16,8 +17,16 @@ function isForwardRefType(obj: any): boolean {
1617
return obj?.$$typeof && obj?.$$typeof === REACT_FORWARD_REF_TYPE;
1718
}
1819

20+
function isMemoType(obj: any): boolean {
21+
return obj?.$$typeof && obj.$$typeof === REACT_MEMO_TYPE;
22+
}
23+
1924
export function isReactComponent(obj: any): obj is ComponentType<any> {
20-
return obj && (isReactClass(obj) || typeof obj === 'function' || isForwardRefType(obj));
25+
if (!obj) {
26+
return false;
27+
}
28+
29+
return Boolean(isReactClass(obj) || typeof obj === 'function' || isForwardRefType(obj) || isMemoType(obj));
2130
}
2231

2332
export function wrapReactClass(view: FunctionComponent) {

packages/utils/test/src/build-components/buildComponents.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,14 @@ describe('build-component', () => {
309309
))
310310
.toEqual({
311311
Button: {
312-
componentName: 'Component',
313-
schema: {},
312+
componentsMap: [],
313+
componentsTree: [
314+
{
315+
componentName: 'Component',
316+
schema: {},
317+
}
318+
],
319+
version: "",
314320
},
315321
});
316322
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from "react";
2+
import { isReactComponent, wrapReactClass } from "../../src/is-react";
3+
4+
class reactDemo extends React.Component {
5+
6+
}
7+
8+
const reactMemo = React.memo(reactDemo);
9+
10+
const reactForwardRef = React.forwardRef((props, ref): any => {
11+
return '';
12+
});
13+
14+
describe('is-react-ut', () => {
15+
it('isReactComponent', () => {
16+
expect(isReactComponent(null)).toBeFalsy();
17+
expect(isReactComponent(() => {})).toBeTruthy();
18+
expect(isReactComponent({
19+
$$typeof: Symbol.for('react.memo')
20+
})).toBeTruthy();
21+
expect(isReactComponent({
22+
$$typeof: Symbol.for('react.forward_ref')
23+
})).toBeTruthy();
24+
expect(isReactComponent(reactDemo)).toBeTruthy();
25+
expect(isReactComponent(reactMemo)).toBeTruthy();
26+
expect(isReactComponent(reactForwardRef)).toBeTruthy();
27+
28+
});
29+
30+
it('wrapReactClass', () => {
31+
const wrap = wrapReactClass(() => {});
32+
expect(isReactComponent(wrap)).toBeTruthy();
33+
34+
const fun = () => {};
35+
fun.displayName = 'mock';
36+
expect(wrapReactClass(fun).displayName).toBe('mock');
37+
})
38+
})

0 commit comments

Comments
 (0)