1 | import React from 'react';
|
2 | import {mount} from 'enzyme';
|
3 |
|
4 | import compose from '..';
|
5 |
|
6 | describe('react-compose-enhancers', () => {
|
7 | describe('when invoked on a classical component', () => {
|
8 | it('returns an enhancer that takes a component and returns a component wrapping the original', () => {
|
9 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
10 | ClassicalComponent,
|
11 | );
|
12 |
|
13 | const result = mount(<Component baz="baz" />);
|
14 | const wrappedComponent = result.find(ClassicalComponent);
|
15 |
|
16 | expect(wrappedComponent.exists()).toBe(true);
|
17 | });
|
18 |
|
19 | it('passes props on the wrapper down to the wrapped component', () => {
|
20 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
21 | ClassicalComponent,
|
22 | );
|
23 |
|
24 | const expectedBaz = 'test';
|
25 | const result = mount(<Component baz={expectedBaz} />);
|
26 | const actualBaz = result.find(ClassicalComponent).prop('baz');
|
27 |
|
28 | expect(actualBaz).toBe(expectedBaz);
|
29 | });
|
30 |
|
31 | it('injects props from all enhancers passed to it to the resulting component', () => {
|
32 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
33 | ClassicalComponent,
|
34 | );
|
35 |
|
36 | const expectedProps = {
|
37 | foo: 'foo',
|
38 | bar: 'bar',
|
39 | baz: 'baz',
|
40 | };
|
41 | const result = mount(<Component baz={expectedProps.baz} />);
|
42 | const actualProps = result.find(ClassicalComponent).props();
|
43 |
|
44 | expect(actualProps).toEqual(expectedProps);
|
45 | });
|
46 |
|
47 | it('hoists static members to wrapper class', () => {
|
48 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
49 | ClassicalComponent,
|
50 | );
|
51 |
|
52 | expect(Component.someStatic).toEqual(ClassicalComponent.someStatic);
|
53 | });
|
54 | });
|
55 |
|
56 | describe('when invoked on a functional component', () => {
|
57 | it('returns an enhancer that takes a component and returns a component wrapping the original', () => {
|
58 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
59 | StatelessComponent,
|
60 | );
|
61 |
|
62 | const result = mount(<Component baz="baz" />);
|
63 | const wrappedComponent = result.find(StatelessComponent);
|
64 |
|
65 | expect(wrappedComponent.exists()).toBe(true);
|
66 | });
|
67 |
|
68 | it('passes props on the wrapper down to the wrapped component', () => {
|
69 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
70 | StatelessComponent,
|
71 | );
|
72 |
|
73 | const expectedBaz = 'test';
|
74 | const result = mount(<Component baz={expectedBaz} />);
|
75 | const actualBaz = result.find(StatelessComponent).prop('baz');
|
76 |
|
77 | expect(actualBaz).toBe(expectedBaz);
|
78 | });
|
79 |
|
80 | it('injects props from all enhancers passed to it to the resulting component', () => {
|
81 | const Component = compose<Props>(fooEnhancer, barEnhancer)(
|
82 | StatelessComponent,
|
83 | );
|
84 |
|
85 | const expectedProps = {
|
86 | foo: 'foo',
|
87 | bar: 'bar',
|
88 | baz: 'baz',
|
89 | };
|
90 | const result = mount(<Component baz={expectedProps.baz} />);
|
91 | const actualProps = result.find(StatelessComponent).props();
|
92 |
|
93 | expect(actualProps).toEqual(expectedProps);
|
94 | });
|
95 | });
|
96 | });
|
97 |
|
98 | interface FooProps {
|
99 | foo: string;
|
100 | }
|
101 |
|
102 | interface BarProps {
|
103 | bar: string;
|
104 | }
|
105 |
|
106 | interface Props {
|
107 | baz: string;
|
108 | }
|
109 |
|
110 | type ComposedProps = FooProps & BarProps & Props;
|
111 |
|
112 |
|
113 | class ClassicalComponent extends React.Component<ComposedProps, never> {
|
114 | static someStatic = 'some static';
|
115 |
|
116 | render() {
|
117 | return <div>classical component</div>;
|
118 | }
|
119 | }
|
120 |
|
121 | function StatelessComponent({foo, bar, baz}: ComposedProps) {
|
122 | return (
|
123 | <div>
|
124 | stateless component {foo} {bar} {baz}
|
125 | </div>
|
126 | );
|
127 | }
|
128 |
|
129 | function fooEnhancer(Component) {
|
130 | return function Wrapper(props: Object) {
|
131 | return <Component foo="foo" {...props} />;
|
132 | };
|
133 | }
|
134 |
|
135 | function barEnhancer(Component) {
|
136 | return function Wrapper(props: Object) {
|
137 | return <Component bar="bar" {...props} />;
|
138 | };
|
139 | }
|