@lowdefy/block-dev 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,88 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /*
3
+ Copyright 2020-2021 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */ import React from 'react';
17
+ const BlockSchemaErrors = ({ schemaErrors })=>{
18
+ if (!schemaErrors || schemaErrors.length === 0) return '';
19
+ return(/*#__PURE__*/ _jsxs("div", {
20
+ style: {
21
+ padding: 10,
22
+ fontSize: '0.8rem',
23
+ border: '1px solid red',
24
+ background: '#fBB',
25
+ width: '100%'
26
+ },
27
+ children: [
28
+ /*#__PURE__*/ _jsx("div", {
29
+ children: /*#__PURE__*/ _jsx("b", {
30
+ children: "Schema Errors"
31
+ })
32
+ }),
33
+ schemaErrors.map((error, i)=>/*#__PURE__*/ _jsxs("div", {
34
+ children: [
35
+ /*#__PURE__*/ _jsx("br", {
36
+ }),
37
+ /*#__PURE__*/ _jsxs("div", {
38
+ children: [
39
+ /*#__PURE__*/ _jsx("b", {
40
+ children: "keyword:"
41
+ }),
42
+ " ",
43
+ error.keyword
44
+ ]
45
+ }),
46
+ /*#__PURE__*/ _jsxs("div", {
47
+ children: [
48
+ /*#__PURE__*/ _jsx("b", {
49
+ children: "message:"
50
+ }),
51
+ " ",
52
+ error.message
53
+ ]
54
+ }),
55
+ /*#__PURE__*/ _jsxs("div", {
56
+ children: [
57
+ /*#__PURE__*/ _jsx("b", {
58
+ children: "params:"
59
+ }),
60
+ " ",
61
+ JSON.stringify(error.params)
62
+ ]
63
+ }),
64
+ /*#__PURE__*/ _jsxs("div", {
65
+ children: [
66
+ /*#__PURE__*/ _jsx("b", {
67
+ children: "dataPath:"
68
+ }),
69
+ " ",
70
+ error.dataPath
71
+ ]
72
+ }),
73
+ /*#__PURE__*/ _jsxs("div", {
74
+ children: [
75
+ /*#__PURE__*/ _jsx("b", {
76
+ children: "schemaPath:"
77
+ }),
78
+ " ",
79
+ error.schemaPath
80
+ ]
81
+ })
82
+ ]
83
+ }, i)
84
+ )
85
+ ]
86
+ }));
87
+ };
88
+ export default BlockSchemaErrors;
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ /*
2
+ Copyright 2020-2021 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import BlockSchemaErrors from './BlockSchemaErrors.js';
16
+ import mockBlock from './mockBlock.js';
17
+ import runBlockSchemaTests from './runBlockSchemaTests.js';
18
+ import runMockMethodTests from './runMockMethodTests.js';
19
+ import runMockRenderTests from './runMockRenderTests.js';
20
+ import runRenderTests from './runRenderTests.js';
21
+ import stubBlockProps from './stubBlockProps.js';
22
+ export { BlockSchemaErrors, mockBlock, runBlockSchemaTests, runMockMethodTests, runMockRenderTests, runRenderTests, stubBlockProps };
@@ -0,0 +1,103 @@
1
+ /*
2
+ Copyright 2020-2021 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import { makeCssClass } from '@lowdefy/block-utils';
16
+ import stubBlockProps from './stubBlockProps.js';
17
+ const mockBlock = ({ meta , logger , schema })=>{
18
+ const mockMath = Object.create(global.Math);
19
+ mockMath.random = ()=>0.5
20
+ ;
21
+ global.Math = mockMath;
22
+ const moveItemDown = jest.fn();
23
+ const moveItemUp = jest.fn();
24
+ const pushItem = jest.fn();
25
+ const registerEvent = jest.fn();
26
+ const registerMethod = jest.fn();
27
+ const removeItem = jest.fn();
28
+ const setValue = jest.fn();
29
+ const triggerEvent = jest.fn();
30
+ const unshiftItem = jest.fn();
31
+ const methods = {
32
+ makeCssClass,
33
+ moveItemDown,
34
+ moveItemUp,
35
+ pushItem,
36
+ registerEvent,
37
+ registerMethod,
38
+ removeItem,
39
+ setValue,
40
+ triggerEvent,
41
+ unshiftItem
42
+ };
43
+ const before = ()=>{
44
+ triggerEvent.mockReset();
45
+ moveItemDown.mockReset();
46
+ moveItemUp.mockReset();
47
+ pushItem.mockReset();
48
+ registerMethod.mockReset();
49
+ registerEvent.mockReset();
50
+ removeItem.mockReset();
51
+ setValue.mockReset();
52
+ unshiftItem.mockReset();
53
+ // for antd from:
54
+ // https://github.com/ant-design/ant-design/blob/master/tests/setup.js
55
+ // ref: https://github.com/ant-design/ant-design/issues/18774
56
+ if (!window.matchMedia) {
57
+ Object.defineProperty(window, 'matchMedia', {
58
+ writable: true,
59
+ value: jest.fn().mockImplementation((query)=>({
60
+ matches: false,
61
+ media: query.includes('max-width'),
62
+ onchange: null,
63
+ addListener: jest.fn(),
64
+ removeListener: jest.fn(),
65
+ addEventListener: jest.fn(),
66
+ removeEventListener: jest.fn(),
67
+ dispatchEvent: jest.fn()
68
+ })
69
+ )
70
+ });
71
+ }
72
+ if (typeof window !== 'undefined') {
73
+ window.resizeTo = (width, height)=>{
74
+ window.innerWidth = width || window.innerWidth;
75
+ window.innerHeight = height || window.innerHeight;
76
+ window.dispatchEvent(new Event('resize'));
77
+ };
78
+ window.scrollTo = ()=>{
79
+ };
80
+ // Fix css-animation or rc-motion deps on these
81
+ // https://github.com/react-component/motion/blob/9c04ef1a210a4f3246c9becba6e33ea945e00669/src/util/motion.ts#L27-L35
82
+ // https://github.com/yiminghe/css-animation/blob/a5986d73fd7dfce75665337f39b91483d63a4c8c/src/Event.js#L44
83
+ window.AnimationEvent = window.AnimationEvent || (()=>{
84
+ });
85
+ window.TransitionEvent = window.TransitionEvent || (()=>{
86
+ });
87
+ }
88
+ };
89
+ const getProps = (block)=>{
90
+ return stubBlockProps({
91
+ block,
92
+ meta,
93
+ logger,
94
+ schema
95
+ });
96
+ };
97
+ return {
98
+ before,
99
+ methods,
100
+ getProps
101
+ };
102
+ };
103
+ export default mockBlock;
@@ -0,0 +1,26 @@
1
+ /*
2
+ Copyright 2020-2021 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import schemaTest from './schemaTest.js';
16
+ const runBlockSchemaTests = ({ examples , schema })=>{
17
+ const validate = schemaTest(schema);
18
+ examples.forEach((block)=>{
19
+ test(`Test Schema ${block.id}`, ()=>{
20
+ const valid = validate(block);
21
+ expect(valid).toMatchSnapshot();
22
+ expect(validate.errors).toMatchSnapshot();
23
+ });
24
+ });
25
+ };
26
+ export default runBlockSchemaTests;
@@ -0,0 +1,80 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /*
3
+ Copyright 2020-2021 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */ import React from 'react';
17
+ import { type } from '@lowdefy/helpers';
18
+ import { render, screen } from '@testing-library/react';
19
+ import userEvent from '@testing-library/user-event';
20
+ import mockBlock from './mockBlock.js';
21
+ const runMockMethodTests = ({ examples , logger , meta , mocks , schema , tests })=>{
22
+ const { before , methods , getProps } = mockBlock({
23
+ meta,
24
+ logger,
25
+ schema
26
+ });
27
+ beforeEach(()=>{
28
+ before();
29
+ });
30
+ const values = meta.values ? [
31
+ type.enforceType(meta.valueType, null),
32
+ ...meta.values
33
+ ] : [
34
+ type.enforceType(meta.valueType, null)
35
+ ];
36
+ examples.forEach((ex)=>{
37
+ values.forEach((value, v)=>{
38
+ if (tests && tests.methods) {
39
+ tests.methods.forEach((method)=>{
40
+ mocks.forEach((mock)=>{
41
+ test(`Mock for method: ${JSON.stringify(method)} - ${ex.id} - value[${v}] - ${mock.name}`, async ()=>{
42
+ const mockFns = await mock.getMockFns();
43
+ const Block = await mock.getBlock();
44
+ const Shell = ()=>{
45
+ const props = getProps(ex);
46
+ props.methods = {
47
+ ...methods,
48
+ registerMethod: props.methods.registerMethod
49
+ };
50
+ return(/*#__PURE__*/ _jsxs(_Fragment, {
51
+ children: [
52
+ /*#__PURE__*/ _jsx(Block, {
53
+ ...props,
54
+ value: value
55
+ }),
56
+ /*#__PURE__*/ _jsx("button", {
57
+ id: `${ex.id}_button`,
58
+ onClick: ()=>{
59
+ props.methods[method.name](method.args);
60
+ },
61
+ "data-testid": "btn_method"
62
+ })
63
+ ]
64
+ }));
65
+ };
66
+ const { container } = render(/*#__PURE__*/ _jsx(Shell, {
67
+ }));
68
+ expect(container.firstChild).toMatchSnapshot();
69
+ userEvent.click(screen.getByTestId('btn_method'));
70
+ mockFns.forEach((mockFn)=>{
71
+ expect(mockFn.mock.calls).toMatchSnapshot();
72
+ });
73
+ });
74
+ });
75
+ });
76
+ }
77
+ });
78
+ });
79
+ };
80
+ export default runMockMethodTests;
@@ -0,0 +1,72 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /*
3
+ Copyright 2020-2021 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */ import React from 'react';
17
+ import { type } from '@lowdefy/helpers';
18
+ import { render } from '@testing-library/react';
19
+ import mockBlock from './mockBlock.js';
20
+ const runMockRenderTests = ({ examples , logger , meta , mocks , reset =()=>null
21
+ , schema })=>{
22
+ const { before , getProps } = mockBlock({
23
+ meta,
24
+ logger,
25
+ schema
26
+ });
27
+ const makeCssClass = jest.fn();
28
+ const makeCssImp = (style, op)=>JSON.stringify({
29
+ style,
30
+ options: op
31
+ })
32
+ ;
33
+ beforeEach(async ()=>{
34
+ await reset();
35
+ before();
36
+ makeCssClass.mockReset();
37
+ makeCssClass.mockImplementation(makeCssImp);
38
+ });
39
+ const values = meta.values ? [
40
+ type.enforceType(meta.valueType, null),
41
+ ...meta.values
42
+ ] : [
43
+ type.enforceType(meta.valueType, null)
44
+ ];
45
+ examples.forEach((ex)=>{
46
+ values.forEach((value, v)=>{
47
+ mocks.forEach((mock)=>{
48
+ test(`Mock render - ${ex.id} - value[${v}] - ${mock.name}`, async ()=>{
49
+ const mockFns = await mock.getMockFns();
50
+ const Block = await mock.getBlock();
51
+ const Shell = ()=>{
52
+ const props = getProps(ex);
53
+ return(/*#__PURE__*/ _jsx(Block, {
54
+ ...props,
55
+ methods: {
56
+ ...props.methods,
57
+ makeCssClass
58
+ },
59
+ value: value
60
+ }));
61
+ };
62
+ render(/*#__PURE__*/ _jsx(Shell, {
63
+ }));
64
+ mockFns.forEach((mockFn)=>{
65
+ expect(mockFn.mock.calls).toMatchSnapshot();
66
+ });
67
+ });
68
+ });
69
+ });
70
+ });
71
+ };
72
+ export default runMockRenderTests;
@@ -0,0 +1,92 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /*
3
+ Copyright 2020-2021 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */ import React from 'react';
17
+ import { render, waitFor } from '@testing-library/react';
18
+ import { type } from '@lowdefy/helpers';
19
+ import mockBlock from './mockBlock.js';
20
+ const runRenderTests = ({ Block , examples , logger , meta , reset =()=>null
21
+ , schema , tests , validationsExamples , })=>{
22
+ const { before , methods , getProps } = mockBlock({
23
+ meta,
24
+ logger,
25
+ schema
26
+ });
27
+ beforeEach(()=>{
28
+ reset();
29
+ before();
30
+ });
31
+ examples.forEach((ex)=>{
32
+ const values = [
33
+ type.enforceType(meta.valueType, null)
34
+ ];
35
+ if (!type.isNone(ex.value)) {
36
+ values.push(ex.value);
37
+ }
38
+ if (type.isArray(meta.values)) {
39
+ values.push(...meta.values);
40
+ }
41
+ values.forEach((value, v)=>{
42
+ test(`Render ${ex.id} - value[${v}]`, async ()=>{
43
+ // create shell to setup react hooks with getProps before render;
44
+ const Shell = ()=>/*#__PURE__*/ _jsx(Block, {
45
+ ...getProps(ex),
46
+ value: value,
47
+ methods: methods
48
+ })
49
+ ;
50
+ const { container } = render(/*#__PURE__*/ _jsx(Shell, {
51
+ }));
52
+ await waitFor(()=>expect(container.firstChild).toMatchSnapshot()
53
+ );
54
+ });
55
+ if (tests && tests.validation) {
56
+ (validationsExamples || []).map((validationEx)=>{
57
+ test(`Render validation.status = ${validationEx.status} ${ex.id} - value[${v}]`, async ()=>{
58
+ // create shell to setup react hooks with getProps before render;
59
+ const Shell = ()=>/*#__PURE__*/ _jsx(Block, {
60
+ ...getProps(ex),
61
+ value: value,
62
+ methods: methods,
63
+ validation: validationEx
64
+ })
65
+ ;
66
+ const { container } = render(/*#__PURE__*/ _jsx(Shell, {
67
+ }));
68
+ await waitFor(()=>expect(container.firstChild).toMatchSnapshot()
69
+ );
70
+ });
71
+ });
72
+ }
73
+ if (tests && tests.required) {
74
+ test(`Render required = true ${ex.id} - value[${v}]`, async ()=>{
75
+ // create shell to setup react hooks with getProps before render;
76
+ const Shell = ()=>/*#__PURE__*/ _jsx(Block, {
77
+ ...getProps(ex),
78
+ value: value,
79
+ methods: methods,
80
+ required: true
81
+ })
82
+ ;
83
+ const { container } = render(/*#__PURE__*/ _jsx(Shell, {
84
+ }));
85
+ await waitFor(()=>expect(container.firstChild).toMatchSnapshot()
86
+ );
87
+ });
88
+ }
89
+ });
90
+ });
91
+ };
92
+ export default runRenderTests;
@@ -0,0 +1,62 @@
1
+ /*
2
+ Copyright 2020-2021 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import Ajv from 'ajv';
16
+ import AjvErrors from 'ajv-errors';
17
+ import { blockSchema } from '@lowdefy/block-utils';
18
+ const testSchemaProperties = {
19
+ value: {
20
+ },
21
+ methods: {
22
+ type: 'object'
23
+ },
24
+ schemaErrors: {
25
+ },
26
+ eventLog: {
27
+ type: 'array'
28
+ },
29
+ pageId: {
30
+ type: 'string'
31
+ },
32
+ blockId: {
33
+ type: 'string'
34
+ },
35
+ content: {
36
+ type: 'object'
37
+ },
38
+ list: {
39
+ type: 'array'
40
+ }
41
+ };
42
+ const initAjv = (options)=>{
43
+ const ajv = new Ajv({
44
+ allErrors: true,
45
+ strict: false,
46
+ ...options
47
+ });
48
+ AjvErrors(ajv, options);
49
+ return ajv;
50
+ };
51
+ const ajvInstance = initAjv();
52
+ const sch = JSON.parse(JSON.stringify(blockSchema));
53
+ const schemaTest = (schema)=>{
54
+ sch.properties = {
55
+ ...sch.properties,
56
+ ...schema.properties.properties,
57
+ ...testSchemaProperties
58
+ };
59
+ sch.additionalProperties = true;
60
+ return ajvInstance.compile(sch);
61
+ };
62
+ export default schemaTest;
@@ -0,0 +1,146 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /*
3
+ Copyright 2020-2021 Lowdefy, Inc
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ */ import React, { useState } from 'react';
17
+ import { type } from '@lowdefy/helpers';
18
+ import { makeCssClass, createIcon } from '@lowdefy/block-utils';
19
+ import schemaTest from './schemaTest.js';
20
+ const validate = {
21
+ };
22
+ const Icons = {
23
+ AiIcon: (props)=>/*#__PURE__*/ _jsx("svg", {
24
+ ...props,
25
+ "data-testid": "AiIcon"
26
+ })
27
+ ,
28
+ AiOutlineExclamationCircle: (props)=>/*#__PURE__*/ _jsx("svg", {
29
+ ...props,
30
+ "data-testid": "AiOutlineExclamationCircle"
31
+ })
32
+ ,
33
+ AiOutlineLoading3Quarters: (props)=>/*#__PURE__*/ _jsx("svg", {
34
+ ...props,
35
+ "data-testid": "AiOutlineLoading3Quarters"
36
+ })
37
+ ,
38
+ ErrorIcon: ()=>{
39
+ throw new Error('ErrorIcon');
40
+ }
41
+ };
42
+ const IconComponent = createIcon(Icons);
43
+ const stubBlockProps = ({ block , meta , logger , initialValue , schema })=>{
44
+ const [value, setState] = useState(type.enforceType(meta.valueType, block.value || initialValue));
45
+ const setValue = (val)=>{
46
+ setState(type.enforceType(meta.valueType, val));
47
+ };
48
+ let log = alert;
49
+ if (logger) log = logger;
50
+ // evaluate block schema
51
+ if (!validate[block.type]) {
52
+ try {
53
+ validate[block.type] = schemaTest(schema);
54
+ } catch (error) {
55
+ throw new Error(`Schema error in ${block.type} - ${error.message}`);
56
+ }
57
+ }
58
+ block.schemaErrors = !validate[block.type](block);
59
+ if (block.schemaErrors) block.schemaErrors = validate[block.type].errors;
60
+ // block defaults
61
+ block.blockId = block.id;
62
+ if (meta.category === 'list' || meta.category === 'container' || meta.category === 'context') {
63
+ if (!block.areas) block.areas = {
64
+ };
65
+ if (!block.areas.content) block.areas.content = {
66
+ };
67
+ if (block.blocks) block.areas.content.blocks = block.blocks;
68
+ }
69
+ block.events = block.events || {
70
+ };
71
+ block.eventLog = [];
72
+ block.components = {
73
+ Icon: IconComponent,
74
+ Link: (props)=>/*#__PURE__*/ _jsx("a", {
75
+ "data-testid": `link-${props.href}`,
76
+ ...props,
77
+ children: props.children
78
+ })
79
+ };
80
+ // mock default block methods
81
+ block.methods = {
82
+ makeCssClass,
83
+ registerEvent: ({ name , actions })=>{
84
+ block.events[name] = actions;
85
+ return;
86
+ },
87
+ registerMethod: (method, methodFn)=>{
88
+ block.methods[method] = methodFn;
89
+ return;
90
+ },
91
+ triggerEvent: (event)=>block.eventLog.unshift(event)
92
+ };
93
+ // block category defaults
94
+ if (meta.category === 'list') {
95
+ block.list = [];
96
+ (block.areas.content.blocks || []).forEach((bl, i)=>{
97
+ block.list.push({
98
+ content: ()=>/*#__PURE__*/ _jsx("div", {
99
+ "data-testid": `list-${i}-${bl.id}`,
100
+ style: {
101
+ border: '1px solid red',
102
+ padding: 10
103
+ },
104
+ children: bl.id
105
+ }, bl.id)
106
+ });
107
+ });
108
+ block.methods = {
109
+ pushItem: ()=>log('List pushItem')
110
+ ,
111
+ unshiftItem: ()=>log('List unshiftItem')
112
+ ,
113
+ removeItem: (i)=>log(`List removeItem ${i}`)
114
+ ,
115
+ moveItemDown: (i)=>log(`List moveItemDown ${i}`)
116
+ ,
117
+ moveItemUp: (i)=>log(`List moveItemUp ${i}`)
118
+ ,
119
+ ...block.methods
120
+ };
121
+ }
122
+ if (meta.category === 'container') {
123
+ block.content = {
124
+ };
125
+ Object.keys(block.areas).forEach((key)=>{
126
+ block.content[key] = ()=>/*#__PURE__*/ _jsx("div", {
127
+ "data-testid": `area-${key}`,
128
+ style: {
129
+ border: '1px solid red',
130
+ padding: 10
131
+ },
132
+ children: key
133
+ }, key)
134
+ ;
135
+ });
136
+ }
137
+ if (meta.category === 'input') {
138
+ block.methods = {
139
+ setValue,
140
+ ...block.methods
141
+ };
142
+ block.value = value;
143
+ }
144
+ return block;
145
+ };
146
+ export default stubBlockProps;
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@lowdefy/block-dev",
3
+ "version": "4.0.0-alpha.1",
4
+ "licence": "Apache-2.0",
5
+ "description": "Lowdefy Block Development Tools",
6
+ "homepage": "https://lowdefy.com",
7
+ "keywords": [
8
+ "lowdefy",
9
+ "utils"
10
+ ],
11
+ "bugs": {
12
+ "url": "https://github.com/lowdefy/lowdefy/issues"
13
+ },
14
+ "contributors": [
15
+ {
16
+ "name": "Sam Tolmay",
17
+ "url": "https://github.com/SamTolmay"
18
+ },
19
+ {
20
+ "name": "Gerrie van Wyk",
21
+ "url": "https://github.com/Gervwyk"
22
+ }
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/lowdefy/lowdefy.git"
27
+ },
28
+ "type": "module",
29
+ "exports": "./dist/index.js",
30
+ "files": [
31
+ "dist/*"
32
+ ],
33
+ "scripts": {
34
+ "build": "yarn swc",
35
+ "clean": "rm -rf dist",
36
+ "prepare": "yarn build",
37
+ "swc": "swc src --out-dir dist --config-file ../../../.swcrc --delete-dir-on-start"
38
+ },
39
+ "dependencies": {
40
+ "@emotion/jest": "11.5.0",
41
+ "@lowdefy/block-utils": "4.0.0-alpha.1",
42
+ "@lowdefy/helpers": "4.0.0-alpha.1",
43
+ "@testing-library/dom": "8.10.1",
44
+ "@testing-library/react": "13.0.0-alpha.4",
45
+ "@testing-library/user-event": "14.0.0-alpha.14",
46
+ "ajv": "8.8.2",
47
+ "ajv-errors": "3.0.0",
48
+ "jest": "27.3.1",
49
+ "jest-serializer-html": "7.1.0",
50
+ "react": "18.0.0-alpha-327d5c484-20211106",
51
+ "react-dom": "18.0.0-alpha-327d5c484-20211106",
52
+ "yaml-loader": "0.6.0"
53
+ },
54
+ "devDependencies": {
55
+ "@swc/cli": "0.1.52",
56
+ "@swc/core": "1.2.112"
57
+ },
58
+ "publishConfig": {
59
+ "access": "public"
60
+ },
61
+ "gitHead": "c97a8fa6b5a641e7d50df09f5601a9c586eeb65a"
62
+ }