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

Commit

Permalink
add test to ensure loading is reset after data comes back
Browse files Browse the repository at this point in the history
  • Loading branch information
James Baxley committed May 28, 2016
1 parent 0bef253 commit ed52c59
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions test/connect/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,104 @@ describe('queries', () => {
);
});

it('resets the loading state after a refetched query', (done) => {

const query = gql`
query people {
allPeople(first: 1) {
people {
name
}
}
}
`;

const data1 = {
allPeople: {
people: [
{
name: 'Luke Skywalker',
},
],
},
};

const data2 = {
allPeople: {
people: [
{
name: 'Han Solo',
},
],
},
};

const networkInterface = mockNetworkInterface(
{
request: { query: query },
result: { data: data1 },
},
{
request: { query: query },
result: { data: data2 },
}
);

const client = new ApolloClient({
networkInterface,
});

function mapQueriesToProps() {
return {
people: { query },
};
};

let hasRefetched = false;
let hasRefetchedAndReturned = false;
@connect({ mapQueriesToProps })
class Container extends React.Component<any, any> {
componentWillReceiveProps(nextProps) {

if (hasRefetchedAndReturned) {
expect(nextProps.people.loading).to.be.false;
done();
return;
}

if (hasRefetched) {
expect(nextProps.people.loading).to.be.true;
hasRefetchedAndReturned = true;
return;
}
}

componentDidUpdate(prevProps) {

if (prevProps.people.loading && !this.props.people.loading) {

if (hasRefetched) {
return;
}

hasRefetched = true;
this.props.people.refetch()
return;
}

}
render() {
return <Passthrough {...this.props} />;
}
};

mount(
<ProviderMock client={client}>
<Container />
</ProviderMock>
);
});

it('resets the loading state when refetching', (done) => {
const store = createStore(() => ({
foo: 'bar',
Expand Down

0 comments on commit ed52c59

Please sign in to comment.