feat(blockTools): test for all modules

This commit is contained in:
Gervwyk 2020-10-26 20:32:06 +02:00
parent 14dd133663
commit 5519789581
27 changed files with 498 additions and 163 deletions

View File

@ -39,7 +39,9 @@ class ErrorBoundary extends Component {
if (renderError) {
return (
<div>
{`Error: ${(message || error.message) + (description && `<br /> ${description}`)}`}
{`Error: ${message || error.message}`}
{description && <br />}
{description}
</div>
);
}

View File

@ -0,0 +1,153 @@
/*
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 { create, act } from 'react-test-renderer';
import ErrorBoundary from './ErrorBoundary';
test('no error', () => {
let comp;
act(() => {
comp = create(
<ErrorBoundary>
<div>one</div>
</ErrorBoundary>
);
});
act(() => {
comp.update(
<ErrorBoundary>
<div>one</div>
</ErrorBoundary>
);
});
expect(comp.toJSON()).toMatchInlineSnapshot(`
<div>
one
</div>
`);
});
test('display no error message on error generated by child', () => {
console.error = () => undefined;
const ProblemChild = () => {
throw new Error('ErrorBoundary test error');
};
let comp;
act(() => {
comp = create(
<ErrorBoundary>
<ProblemChild />
</ErrorBoundary>
);
});
act(() => {
comp.update(
<ErrorBoundary>
<ProblemChild />
</ErrorBoundary>
);
});
expect(comp.toJSON()).toMatchInlineSnapshot(`""`);
});
test('display error message on error generated by child', () => {
console.error = () => undefined;
const ProblemChild = () => {
throw new Error('ErrorBoundary test error');
};
let comp;
act(() => {
comp = create(
<ErrorBoundary renderError>
<ProblemChild />
</ErrorBoundary>
);
});
act(() => {
comp.update(
<ErrorBoundary renderError>
<ProblemChild />
</ErrorBoundary>
);
});
expect(comp.toJSON()).toMatchInlineSnapshot(`
<div>
Error: ErrorBoundary test error
</div>
`);
});
test('display error message and description on error generated by child', () => {
console.error = () => undefined;
const ProblemChild = () => {
throw new Error('ErrorBoundary test error');
};
let comp;
act(() => {
comp = create(
<ErrorBoundary renderError description="error description">
<ProblemChild />
</ErrorBoundary>
);
});
act(() => {
comp.update(
<ErrorBoundary renderError description="error description">
<ProblemChild />
</ErrorBoundary>
);
});
expect(comp.toJSON()).toMatchInlineSnapshot(`
<div>
Error: ErrorBoundary test error
<br />
error description
</div>
`);
});
test('display fallback component on error generated by child', () => {
console.error = () => undefined;
const ProblemChild = () => {
throw new Error('ErrorBoundary test error');
};
let comp;
act(() => {
comp = create(
<ErrorBoundary fallback={(error) => <div>fallback: {error.message}</div>}>
<ProblemChild />
</ErrorBoundary>
);
});
act(() => {
comp.update(
<ErrorBoundary fallback={(error) => <div>fallback: {error.message}</div>}>
<ProblemChild />
</ErrorBoundary>
);
});
expect(comp.toJSON()).toMatchInlineSnapshot(`
<div>
fallback:
ErrorBoundary test error
</div>
`);
});

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/Skeleton.yaml';
import runExampleTests from './runExampleTests';
import { Skeleton } from '../src';
import examples from '../../demo/examples/Skeleton.yaml';
import runRenderTests from '../runRenderTests';
import Skeleton from './Skeleton';
runExampleTests(examples, Skeleton);
runRenderTests(examples, Skeleton);

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/SkeletonAvatar.yaml';
import runExampleTests from './runExampleTests';
import { SkeletonAvatar } from '../src';
import examples from '../../demo/examples/SkeletonAvatar.yaml';
import runRenderTests from '../runRenderTests';
import SkeletonAvatar from './SkeletonAvatar';
runExampleTests(examples, SkeletonAvatar);
runRenderTests(examples, SkeletonAvatar);

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/SkeletonButton.yaml';
import runExampleTests from './runExampleTests';
import { SkeletonButton } from '../src';
import examples from '../../demo/examples/SkeletonButton.yaml';
import runRenderTests from '../runRenderTests';
import SkeletonButton from './SkeletonButton';
runExampleTests(examples, SkeletonButton);
runRenderTests(examples, SkeletonButton);

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/SkeletonInput.yaml';
import runExampleTests from './runExampleTests';
import { SkeletonInput } from '../src';
import examples from '../../demo/examples/SkeletonInput.yaml';
import runRenderTests from '../runRenderTests';
import SkeletonInput from './SkeletonInput';
runExampleTests(examples, SkeletonInput);
runRenderTests(examples, SkeletonInput);

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/SkeletonParagraph.yaml';
import runExampleTests from './runExampleTests';
import { SkeletonParagraph } from '../src';
import examples from '../../demo/examples/SkeletonParagraph.yaml';
import runRenderTests from '../runRenderTests';
import SkeletonParagraph from './SkeletonParagraph';
runExampleTests(examples, SkeletonParagraph);
runRenderTests(examples, SkeletonParagraph);

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<div
className="skeleton {}"
style={
@ -12,7 +12,7 @@ exports[`default 1`] = `
/>
`;
exports[`sizedefault 1`] = `
exports[`Render sizedefault 1`] = `
<div
className="skeleton {}"
style={
@ -24,7 +24,7 @@ exports[`sizedefault 1`] = `
/>
`;
exports[`sizesmall 1`] = `
exports[`Render sizesmall 1`] = `
<div
className="skeleton {}"
style={

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":16}}"
style={
@ -12,7 +12,7 @@ exports[`default 1`] = `
/>
`;
exports[`shapesquare 1`] = `
exports[`Render shapesquare 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":\\"0\\"}}"
style={
@ -24,7 +24,7 @@ exports[`shapesquare 1`] = `
/>
`;
exports[`sizedefault 1`] = `
exports[`Render sizedefault 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":16}}"
style={
@ -36,7 +36,7 @@ exports[`sizedefault 1`] = `
/>
`;
exports[`sizelarge 1`] = `
exports[`Render sizelarge 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":20}}"
style={
@ -48,7 +48,7 @@ exports[`sizelarge 1`] = `
/>
`;
exports[`sizesmall 1`] = `
exports[`Render sizesmall 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":12}}"
style={

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":false}}"
style={
@ -12,7 +12,7 @@ exports[`default 1`] = `
/>
`;
exports[`shaperound 1`] = `
exports[`Render shaperound 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":16}}"
style={
@ -24,7 +24,7 @@ exports[`shaperound 1`] = `
/>
`;
exports[`sizedefault 1`] = `
exports[`Render sizedefault 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":false}}"
style={
@ -36,7 +36,7 @@ exports[`sizedefault 1`] = `
/>
`;
exports[`sizelarge 1`] = `
exports[`Render sizelarge 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":false}}"
style={
@ -48,7 +48,7 @@ exports[`sizelarge 1`] = `
/>
`;
exports[`sizesmall 1`] = `
exports[`Render sizesmall 1`] = `
<div
className="skeleton {\\"style\\":{\\"borderRadius\\":false}}"
style={

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"
@ -23,7 +23,7 @@ exports[`default 1`] = `
</div>
`;
exports[`inputHeight80 1`] = `
exports[`Render inputHeight80 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"
@ -46,7 +46,7 @@ exports[`inputHeight80 1`] = `
</div>
`;
exports[`labelHeight40 1`] = `
exports[`Render labelHeight40 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"
@ -69,7 +69,7 @@ exports[`labelHeight40 1`] = `
</div>
`;
exports[`labelWidth80 1`] = `
exports[`Render labelWidth80 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"
@ -92,7 +92,7 @@ exports[`labelWidth80 1`] = `
</div>
`;
exports[`labelfalse 1`] = `
exports[`Render labelfalse 1`] = `
<div>
<div
className="skeleton {}"
@ -106,7 +106,7 @@ exports[`labelfalse 1`] = `
</div>
`;
exports[`sizedefault 1`] = `
exports[`Render sizedefault 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"
@ -129,7 +129,7 @@ exports[`sizedefault 1`] = `
</div>
`;
exports[`sizelarge 1`] = `
exports[`Render sizelarge 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"
@ -152,7 +152,7 @@ exports[`sizelarge 1`] = `
</div>
`;
exports[`sizesmall 1`] = `
exports[`Render sizesmall 1`] = `
<div>
<div
className="skeleton {\\"style\\":{\\"marginBottom\\":10}}"

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<div
style={
Object {
@ -47,7 +47,7 @@ exports[`default 1`] = `
</div>
`;
exports[`lines1 1`] = `
exports[`Render lines1 1`] = `
<div
style={
Object {
@ -67,7 +67,7 @@ exports[`lines1 1`] = `
</div>
`;
exports[`lines7 1`] = `
exports[`Render lines7 1`] = `
<div
style={
Object {
@ -141,7 +141,7 @@ exports[`lines7 1`] = `
</div>
`;
exports[`width200 1`] = `
exports[`Render width200 1`] = `
<div
style={
Object {

View File

@ -49,7 +49,7 @@ const IconSpinner = ({ properties, methods }) => {
width={size}
height={size}
fill="currentColor"
ariaHidden="true"
aria-hidden="true"
className="icon-spinner"
>
<path

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/IconSpinner.yaml';
import runExampleTests from './runExampleTests';
import { IconSpinner } from '../src';
import examples from '../../demo/examples/IconSpinner.yaml';
import runRenderTests from '../runRenderTests';
import IconSpinner from './IconSpinner';
runExampleTests(examples, IconSpinner);
runRenderTests(examples, IconSpinner);

View File

@ -14,8 +14,8 @@
limitations under the License.
*/
import examples from '../demo/examples/Spinner.yaml';
import runExampleTests from './runExampleTests';
import { Spinner } from '../src';
import examples from '../../demo/examples/Spinner.yaml';
import runRenderTests from '../runRenderTests';
import Spinner from './Spinner';
runExampleTests(examples, Spinner);
runRenderTests(examples, Spinner);

View File

@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<span
className="{\\"style\\":{\\"height\\":20,\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\"}}"
>
<svg
ariaHidden="true"
aria-hidden="true"
className="icon-spinner"
data-icon="loading-3-quarters"
fill="currentColor"
@ -22,12 +22,12 @@ exports[`default 1`] = `
</span>
`;
exports[`sizedefault 1`] = `
exports[`Render sizedefault 1`] = `
<span
className="{\\"style\\":{\\"height\\":24,\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\"}}"
>
<svg
ariaHidden="true"
aria-hidden="true"
className="icon-spinner"
data-icon="loading-3-quarters"
fill="currentColor"
@ -44,12 +44,12 @@ exports[`sizedefault 1`] = `
</span>
`;
exports[`sizelarge 1`] = `
exports[`Render sizelarge 1`] = `
<span
className="{\\"style\\":{\\"height\\":32,\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\"}}"
>
<svg
ariaHidden="true"
aria-hidden="true"
className="icon-spinner"
data-icon="loading-3-quarters"
fill="currentColor"
@ -66,12 +66,12 @@ exports[`sizelarge 1`] = `
</span>
`;
exports[`sizesmall 1`] = `
exports[`Render sizesmall 1`] = `
<span
className="{\\"style\\":{\\"height\\":20,\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\"}}"
>
<svg
ariaHidden="true"
aria-hidden="true"
className="icon-spinner"
data-icon="loading-3-quarters"
fill="currentColor"

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`default 1`] = `
exports[`Render default 1`] = `
<div
className="{\\"style\\":{\\"height\\":\\"100%\\",\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\",\\"background\\":false}}"
>
@ -58,7 +58,7 @@ exports[`default 1`] = `
</div>
`;
exports[`height100 1`] = `
exports[`Render height100 1`] = `
<div
className="{\\"style\\":{\\"height\\":100,\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\",\\"background\\":false}}"
>
@ -179,7 +179,7 @@ exports[`message 1`] = `
</div>
`;
exports[`shaded 1`] = `
exports[`Render shaded 1`] = `
<div
className="{\\"style\\":{\\"height\\":\\"100%\\",\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\",\\"background\\":\\"#f1f1f1\\"}}"
>
@ -237,7 +237,7 @@ exports[`shaded 1`] = `
</div>
`;
exports[`styleheight 1`] = `
exports[`Render styleheight 1`] = `
<div
className="{\\"style\\":{\\"height\\":\\"100%\\",\\"display\\":\\"flex\\",\\"alignItems\\":\\"center\\",\\"justifyContent\\":\\"center\\",\\"background\\":false}}"
>

View File

@ -17,11 +17,13 @@
import React from 'react';
import makeCssClass from './makeCssClass';
const noMethod = () => undefined;
const defaultMethods = (methods) => ({
makeCssClass,
callAction: methods.callAction || (() => undefined),
registerAction: methods.registerAction || (() => undefined),
registerMethod: methods.registerMethod || (() => undefined),
callAction: methods.callAction || noMethod,
registerAction: methods.registerAction || noMethod,
registerMethod: methods.registerMethod || noMethod,
...methods,
});
@ -31,8 +33,9 @@ const blockDefaults = (Comp) => {
blockId,
Components,
content,
methods,
list,
menus,
methods,
properties,
user,
validate,
@ -45,6 +48,7 @@ const blockDefaults = (Comp) => {
methods={defaultMethods(methods || {})}
Components={Components || {}}
content={content || {}}
list={list || {}}
menus={menus || []}
properties={properties || {}}
user={user || {}}

View File

@ -0,0 +1,180 @@
/*
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 blockDefaults from './blockDefaults';
const mockMath = Object.create(global.Math);
mockMath.random = () => 0.123456789;
global.Math = mockMath;
test('default', () => {
const Comp = () => <div>Comp</div>;
const res = blockDefaults(Comp);
const props = {};
expect(res(props)).toMatchInlineSnapshot(`
<Comp
Components={Object {}}
actions={Object {}}
blockId="blockId_1f9add"
content={Object {}}
list={Object {}}
menus={Array []}
methods={
Object {
"callAction": [Function],
"makeCssClass": [Function],
"registerAction": [Function],
"registerMethod": [Function],
}
}
properties={Object {}}
user={Object {}}
validate={Array []}
/>
`);
});
test('with values', () => {
const Comp = () => <div>Comp</div>;
const res = blockDefaults(Comp);
const props = {
actions: { actions: 1 },
Components: { Components: 1 },
blockId: '1',
content: { content: 1 },
list: { list: 1 },
menus: ['menus'],
properties: { properties: 1 },
user: { user: 1 },
validate: ['validate'],
};
expect(res(props)).toMatchInlineSnapshot(`
<Comp
Components={
Object {
"Components": 1,
}
}
actions={
Object {
"actions": 1,
}
}
blockId="1"
content={
Object {
"content": 1,
}
}
list={
Object {
"list": 1,
}
}
menus={
Array [
"menus",
]
}
methods={
Object {
"callAction": [Function],
"makeCssClass": [Function],
"registerAction": [Function],
"registerMethod": [Function],
}
}
properties={
Object {
"properties": 1,
}
}
user={
Object {
"user": 1,
}
}
validate={
Array [
"validate",
]
}
/>
`);
});
test('with no methods', () => {
const Comp = () => <div>Comp</div>;
const res = blockDefaults(Comp);
const props = {
methods: {},
};
expect(res(props)).toMatchInlineSnapshot(`
<Comp
Components={Object {}}
actions={Object {}}
blockId="blockId_1f9add"
content={Object {}}
list={Object {}}
menus={Array []}
methods={
Object {
"callAction": [Function],
"makeCssClass": [Function],
"registerAction": [Function],
"registerMethod": [Function],
}
}
properties={Object {}}
user={Object {}}
validate={Array []}
/>
`);
});
test('with methods', () => {
const Comp = () => <div>Comp</div>;
const res = blockDefaults(Comp);
const props = {
methods: {
callAction: 'callAction',
registerAction: 'registerAction',
registerMethod: 'registerMethod',
},
};
expect(res(props)).toMatchInlineSnapshot(`
<Comp
Components={Object {}}
actions={Object {}}
blockId="blockId_1f9add"
content={Object {}}
list={Object {}}
menus={Array []}
methods={
Object {
"callAction": "callAction",
"makeCssClass": [Function],
"registerAction": "registerAction",
"registerMethod": "registerMethod",
}
}
properties={Object {}}
user={Object {}}
validate={Array []}
/>
`);
});

View File

@ -0,0 +1,46 @@
/*
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 getEmotionCss from './getEmotionCss';
import createEmotion from 'create-emotion';
jest.mock('create-emotion', () => {
const emotion = jest.fn();
return emotion;
});
beforeEach(() => {
createEmotion.mockReset();
createEmotion.mockImplementation(() => ({ css: 'emotionCssMock' }));
});
test('catch emotion error', () => {
createEmotion.mockImplementation(() => {
throw new Error('No emotion');
});
expect(() => getEmotionCss()).toThrowErrorMatchingInlineSnapshot(
`"Emotion failed to initilize: No emotion"`
);
});
test('default and memoize', () => {
const css = getEmotionCss();
expect(css).toEqual('emotionCssMock');
expect(createEmotion).toHaveBeenCalledTimes(1);
const csstwo = getEmotionCss();
expect(csstwo).toEqual('emotionCssMock');
expect(createEmotion).toHaveBeenCalledTimes(1);
});

View File

@ -14,11 +14,11 @@
limitations under the License.
*/
import React from 'react';
import { useLayoutEffect, useRef } from 'react';
const useRunAfterUpdate = () => {
const afterPaintRef = React.useRef(null);
React.useLayoutEffect(() => {
const afterPaintRef = useRef(null);
useLayoutEffect(() => {
if (afterPaintRef.current) {
afterPaintRef.current();
afterPaintRef.current = null;

View File

@ -0,0 +1,39 @@
/*
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 useRunAfterUpdate from './useRunAfterUpdate';
import { useRef, useLayoutEffect } from 'react';
jest.mock('react', () => {
const useLayoutEffect = (fn) => fn();
const ref = { current: jest.fn() };
const useRef = () => ref;
return { useLayoutEffect, useRef };
});
const ref = useRef();
const { current } = ref;
beforeEach(() => {
ref.current.mockReset();
});
test('default call', () => {
const res = useRunAfterUpdate();
expect(current).toBeCalledTimes(1);
res('one');
expect(ref.current).toEqual('one');
});

View File

@ -1,38 +0,0 @@
/*
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 { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
import { ErrorBoundary } from '../src';
const ProblemChild = () => {
throw new Error('Error thrown from problem child');
return <div>Error</div>; // eslint-disable-line
};
it('displays error message on error generated by child', () => {
const wrapper = shallow(
<ErrorBoundary>
<ProblemChild />
</ErrorBoundary>
);
expect(() => {
wrapper.dive().html();
}).toThrowError('Error thrown from problem child');
});

View File

@ -1,51 +0,0 @@
/*
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 renderer from 'react-test-renderer';
const runExampleTests = (examples, Component) => {
const makeCssClass = jest.fn();
const makeCssImp = (style, op) => JSON.stringify({ style, options: op });
beforeEach(() => {
makeCssClass.mockReset();
makeCssClass.mockImplementation(makeCssImp);
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
});
examples.forEach((ex) => {
test(ex.id, () => {
const component = renderer.create(<Component {...ex} methods={{ makeCssClass }} />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
});
};
export default runExampleTests;