mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-02-23 14:39:32 +08:00
feat(blockTools): split method tests into seperate functions and mock methods
This commit is contained in:
parent
f3b8203f24
commit
953a88d698
@ -23,6 +23,7 @@ import makeCssClass from './makeCssClass.js';
|
||||
import mediaToCssObject from './mediaToCssObject.js';
|
||||
import mockBlock from './mockBlock';
|
||||
import runBlockSchemaTests from './runBlockSchemaTests';
|
||||
import runMethodTests from './runMethodTests';
|
||||
import runRenderTests from './runRenderTests';
|
||||
import Skeleton from './Skeleton/Skeleton';
|
||||
import SkeletonAvatar from './Skeleton/SkeletonAvatar';
|
||||
@ -43,6 +44,7 @@ export {
|
||||
mediaToCssObject,
|
||||
mockBlock,
|
||||
runBlockSchemaTests,
|
||||
runMethodTests,
|
||||
runRenderTests,
|
||||
Skeleton,
|
||||
SkeletonAvatar,
|
||||
|
67
packages/blockTools/src/runMethodTests.js
Normal file
67
packages/blockTools/src/runMethodTests.js
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2020 Lowdefy, Inc
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import { type } from '@lowdefy/helpers';
|
||||
|
||||
import mockBlock from './mockBlock';
|
||||
|
||||
const runMethodTests = ({ Block, enzyme, examples, logger, meta, mocks }) => {
|
||||
const { before, methods, getProps } = mockBlock({ meta, logger });
|
||||
|
||||
beforeEach(() => {
|
||||
before();
|
||||
});
|
||||
const values = meta.values
|
||||
? [type.enforceType(meta.valueType, null), ...meta.values]
|
||||
: [type.enforceType(meta.valueType, null)];
|
||||
|
||||
examples.forEach((ex) => {
|
||||
values.forEach((value, v) => {
|
||||
if (meta.test && meta.test.methods) {
|
||||
meta.test.methods.forEach((method) => {
|
||||
mocks.forEach((mock) => {
|
||||
test(`Render for method: ${JSON.stringify(method)} - ${ex.id} - value[${v}] - ${
|
||||
mock.name
|
||||
}`, () => {
|
||||
const Shell = () => {
|
||||
const props = getProps(ex);
|
||||
props.methods = { ...methods, registerMethod: props.methods.registerMethod };
|
||||
return (
|
||||
<>
|
||||
<Block {...props} value={value} />
|
||||
<button
|
||||
id={`${ex.id}_button`}
|
||||
onClick={() => {
|
||||
props.registeredMethods[method.name](method.args);
|
||||
}}
|
||||
data-testid="btn_method"
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
const wrapper = enzyme.mount(<Shell />);
|
||||
wrapper.find('[data-testid="btn_method"]').simulate('click');
|
||||
expect(mock.fn.mock.calls).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default runMethodTests;
|
@ -23,7 +23,6 @@ import mockBlock from './mockBlock';
|
||||
|
||||
const runRenderTests = ({
|
||||
Block,
|
||||
enzyme,
|
||||
examples,
|
||||
logger,
|
||||
meta,
|
||||
@ -32,7 +31,6 @@ const runRenderTests = ({
|
||||
}) => {
|
||||
const { before, methods, getProps } = mockBlock({ meta, logger });
|
||||
|
||||
beforeEach(before);
|
||||
beforeEach(() => {
|
||||
reset();
|
||||
before();
|
||||
@ -89,32 +87,6 @@ const runRenderTests = ({
|
||||
comp.unmount();
|
||||
});
|
||||
}
|
||||
|
||||
if (meta.test && meta.test.methods) {
|
||||
meta.test.methods.forEach((method) => {
|
||||
test(`Render for method: ${JSON.stringify(method)} - ${ex.id} - value[${v}]`, () => {
|
||||
const Shell = () => {
|
||||
const props = getProps(ex);
|
||||
props.methods = { ...methods, registerMethod: props.methods.registerMethod };
|
||||
return (
|
||||
<>
|
||||
<Block {...props} value={value} />
|
||||
<button
|
||||
id={`${ex.id}_button`}
|
||||
onClick={() => {
|
||||
props.registeredMethods[method.name](method.args);
|
||||
}}
|
||||
data-testid="btn_method"
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
const wrapper = enzyme.mount(<Shell />);
|
||||
wrapper.find('[data-testid="btn_method"]').simulate('click');
|
||||
expect(document.body.innerHTML).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user