-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathwithPlugins.test.jsx
More file actions
82 lines (71 loc) · 2.74 KB
/
withPlugins.test.jsx
File metadata and controls
82 lines (71 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import PropTypes from 'prop-types';
import { render, screen, within } from '@tests/utils/test-utils';
import { withPlugins } from '../../../src/extend/withPlugins';
import { PluginHook } from '../../../src/components/PluginHook';
import PluginContext from '../../../src/extend/PluginContext';
/** Mock target component */
const Target = ({ ...props }) => (
<div data-testid="target" {...props}>
Hello
</div>
);
/** create wrapper */
function createPluginHoc(pluginMap) {
const props = { bar: 2, foo: 1 };
const PluginHoc = withPlugins('Target', Target);
return render(
<PluginContext.Provider value={pluginMap}>
<PluginHoc {...props} />
</PluginContext.Provider>,
);
}
describe('withPlugins', () => {
it('should return a function (normal function call)', () => {
expect(withPlugins('Target', Target)).toBeInstanceOf(Object);
});
it('should return a function (curry function call)', () => {
expect(withPlugins('Target')(Target)).toBeInstanceOf(Object);
});
it('displayName prop of returned function is based on target name argument', () => {
expect(withPlugins('Bubu', Target).displayName)
.toBe('WithPlugins(Bubu)');
});
});
describe('PluginHoc: if no plugin exists for the target', () => {
it('renders the target component', () => {
createPluginHoc({});
expect(screen.getByTestId('target')).toHaveAttribute('foo', '1');
expect(screen.getByTestId('target')).toHaveAttribute('bar', '2');
});
});
describe('PluginHoc: if wrap plugins exist for target', () => {
it('renders the first wrap plugin', () => {
/** */ const WrapPluginComponentA = props => <div data-testid="plugin">look i am a plugin</div>;
const pluginMap = {
Target: {
wrap: [
{ component: WrapPluginComponentA, mode: 'wrap', target: 'Target' },
],
},
};
createPluginHoc(pluginMap);
expect(screen.getByTestId('plugin')).toBeInTheDocument();
expect(screen.queryByTestId('target')).not.toBeInTheDocument();
});
it('passes the whole wrapped stack to the plugins', () => {
/** */ const WrapPluginComponentA = ({ children }) => <div data-testid="pluginA">{children}</div>;
WrapPluginComponentA.propTypes = { children: PropTypes.node.isRequired };
/** */ const WrapPluginComponentB = props => <div data-testid="pluginB">look i am a plugin</div>;
const pluginMap = {
Target: {
wrap: [
{ component: WrapPluginComponentA, mode: 'wrap', target: 'Target' },
{ component: WrapPluginComponentB, mode: 'wrap', target: 'Target' },
],
},
};
createPluginHoc(pluginMap);
expect(screen.getByTestId('pluginA')).toBeInTheDocument();
expect(within(screen.getByTestId('pluginA')).getByTestId('pluginB')).toBeInTheDocument();
});
});