This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 793
/
redux.tsx
228 lines (186 loc) · 6.32 KB
/
redux.tsx
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import * as React from 'react';
import * as chai from 'chai';
import { mount } from 'enzyme';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { connect } from 'react-redux';
import { reducer as formReducer, reduxForm } from 'redux-form';
// import assign = require('object-assign');
import gql from 'graphql-tag';
import ApolloClient from 'apollo-client';
declare function require(name: string);
import chaiEnzyme = require('chai-enzyme');
chai.use(chaiEnzyme()); // Note the invocation at the end
const { expect } = chai;
import {
ProviderMock,
} from '../../../mocks/components';
import mockNetworkInterface from '../../../mocks/mockNetworkInterface';
import graphql from '../../../../src/graphql';
describe('redux integration', () => {
it('updates child props on state change', (done) => {
const query = gql`query people($first: Int) { allPeople(first: $first) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const variables = { first: 1 };
const data2 = { allPeople: { people: [ { name: 'Leia Skywalker' } ] } };
const variables2 = { first: 2 };
const networkInterface = mockNetworkInterface(
{ request: { query, variables }, result: { data } },
{ request: { query, variables: variables2 }, result: { data: data2 } }
);
const client = new ApolloClient({ networkInterface });
let wrapper;
function counter(state = 1, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1;
default:
return state;
}
}
// Typscript workaround
const apolloReducer = client.reducer() as () => any;
const store = createStore(
combineReducers({
counter,
apollo: apolloReducer,
}),
applyMiddleware(client.middleware())
);
@connect((state) => ({ first: state.counter }))
@graphql(query)
class Container extends React.Component<any, any> {
componentWillReceiveProps(nextProps) {
if (nextProps.first === 1) this.props.dispatch({ type: 'INCREMENT' });
if (nextProps.first === 2) {
if (nextProps.data.loading) return;
expect(nextProps.data.allPeople).to.deep.equal(data2.allPeople);
done();
}
}
render() {
return null;
}
};
wrapper = mount(
<ProviderMock store={store} client={client}>
<Container />
</ProviderMock>
) as any;
});
it('works with redux form to drive queries', (done) => {
const query = gql`query people($name: String) { allPeople(name: $name) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const variables = { name: 'Luke' };
const networkInterface = mockNetworkInterface(
{ request: { query, variables }, result: { data } }
);
const client = new ApolloClient({ networkInterface });
let wrapper;
// Typscript workaround
const apolloReducer = client.reducer() as () => any;
const store = createStore(
combineReducers({
apollo: apolloReducer,
form: formReducer,
}),
applyMiddleware(client.middleware())
);
@reduxForm({
form: 'contact',
fields: ['firstName'],
})
@graphql(query, {
options: ({ fields }) => ({
variables: { name: fields.firstName.value },
skip: !fields.firstName.value,
}),
})
class Container extends React.Component<any, any> {
componentWillReceiveProps(nextProps) {
const { value } = nextProps.fields.firstName;
if (!value) return;
expect(value).to.equal(variables.name);
if (nextProps.data.loading) return;
expect(nextProps.data.loading).to.be.false;
expect(nextProps.data.allPeople).to.deep.equal(data.allPeople);
done();
}
render() {
const { fields: { firstName }, handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<input type='text' placeholder='First Name' {...firstName}/>
</div>
<button type='submit'>Submit</button>
</form>
);
}
};
wrapper = mount(
<ProviderMock store={store} client={client}>
<Container />
</ProviderMock>
) as any;
setTimeout(() => {
wrapper.find('input').simulate('change', {
target: { value: variables.name },
});
}, 100);
});
it('works with redux form to be prefilled by queries', (done) => {
const query = gql`query people($name: String) { allPeople(name: $name) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const variables = { name: 'Luke' };
const networkInterface = mockNetworkInterface(
{ request: { query, variables }, result: { data } }
);
const client = new ApolloClient({ networkInterface });
let wrapper;
// Typscript workaround
const apolloReducer = client.reducer() as () => any;
const store = createStore(
combineReducers({
apollo: apolloReducer,
form: formReducer,
}),
applyMiddleware(client.middleware())
);
@graphql(query, { options: () => ({ variables }) })
@reduxForm({
form: 'contact',
fields: ['firstName'],
}, (state, ownProps) => ({
initialValues: {
firstName: ownProps.data.loading ? '' : ownProps.data.allPeople.people[0].name,
},
}))
class Container extends React.Component<any, any> {
componentWillReceiveProps(nextProps) {
const { value, initialValue } = nextProps.fields.firstName;
if (!value) return;
expect(initialValue).to.equal(data.allPeople.people[0].name);
expect(value).to.equal(data.allPeople.people[0].name);
done();
}
render() {
const { fields: { firstName }, handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<input type='text' placeholder='First Name' {...firstName}/>
</div>
<button type='submit'>Submit</button>
</form>
);
}
};
mount(
<ProviderMock store={store} client={client}>
<Container />
</ProviderMock>
) as any;
});
});