Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 0fa1b90

Browse files
Tom ColemanJames Baxley
authored andcommitted
Added test for #188 (#198)
1 parent 4a9ffec commit 0fa1b90

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/react-web/client/graphql/queries.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,4 +1182,64 @@ describe('queries', () => {
11821182
mount(<ProviderMock client={client}><Container /></ProviderMock>);
11831183
});
11841184

1185+
it('reruns props function after query results change via fetchMore', (done) => {
1186+
const query = gql`query people($cursor: Int) {
1187+
allPeople(cursor: $cursor) { cursor, people { name } }
1188+
}`;
1189+
const vars1 = { cursor: null };
1190+
const data1 = { allPeople: { cursor: 1, people: [ { name: 'Luke Skywalker' } ] } };
1191+
const vars2 = { cursor: 1 };
1192+
const data2 = { allPeople: { cursor: 2, people: [ { name: 'Leia Skywalker' } ] } };
1193+
const networkInterface = mockNetworkInterface(
1194+
{ request: { query, variables: vars1 }, result: { data: data1 } },
1195+
{ request: { query, variables: vars2 }, result: { data: data2 } }
1196+
);
1197+
const client = new ApolloClient({ networkInterface });
1198+
1199+
let isUpdated = false;
1200+
@graphql(query, {
1201+
// XXX: I think we should be able to avoid this https://github.com/apollostack/react-apollo/issues/197
1202+
options: { variables: { cursor: null } },
1203+
props({ data: { loading, allPeople, fetchMore } }) {
1204+
if (loading) return { loading };
1205+
1206+
const { cursor, people } = allPeople;
1207+
return {
1208+
people,
1209+
getMorePeople: () => fetchMore({
1210+
variables: { cursor },
1211+
updateQuery(prev, { fetchMoreResult }) {
1212+
const { data: { allPeople: { cursor, people } } } = fetchMoreResult;
1213+
return {
1214+
allPeople: {
1215+
cursor,
1216+
people: [...people, ...prev.allPeople.people],
1217+
},
1218+
};
1219+
}
1220+
}),
1221+
}
1222+
}
1223+
})
1224+
class Container extends React.Component<any, any> {
1225+
componentWillReceiveProps(props) {
1226+
if (props.loading) {
1227+
return;
1228+
} else if (isUpdated) {
1229+
expect(props.people.length).to.equal(2);
1230+
done();
1231+
return;
1232+
} else {
1233+
isUpdated = true;
1234+
expect(props.people).to.deep.equal(data1.allPeople.people);
1235+
props.getMorePeople();
1236+
}
1237+
}
1238+
render() {
1239+
return null;
1240+
}
1241+
};
1242+
1243+
mount(<ProviderMock client={client}><Container /></ProviderMock>);
1244+
});
11851245
});

0 commit comments

Comments
 (0)