Skip to content

Commit

Permalink
Merge pull request #71 from 4Catalyzer/test-named-child-routes
Browse files Browse the repository at this point in the history
Fix rerendering on retry
  • Loading branch information
taion authored Aug 10, 2017
2 parents 0679a6a + dc626f3 commit 4833144
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/modern/QuerySubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ export default class QuerySubscription {
}

snapshot = this.environment.lookup(this.operation.fragment);
this.updateReadyState(snapshot);

// retry is unset only for the initial request.
if (this.readyState.retry) {
// We've already fetched once. That means this isn't an initial
// render, so we need to trigger listeners.
this.onChange(snapshot);
} else {
// Don't trigger listeners on the initial fetch, because the
// resolver will trigger an update and make ReadyStateRenderers
// rerender anyway.
this.updateReadyState(snapshot);
}

this.rootSubscription = this.environment.subscribe(
snapshot, this.onChange,
);
Expand Down
30 changes: 25 additions & 5 deletions test/modern/retry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ describe('retry', () => {
environment = createEnvironment(fetchSpy);
});

it('should fire a new network request', async () => {
const renderSpy = jest.fn(() => null);
it('should send a new network requests and rerender', async () => {
const renderSpy = jest.fn(({ props }) => (
props && <div className={props.widget.name} />
));

const Router = createFarceRouter({
historyProtocol: new ServerProtocol('/'),
Expand All @@ -40,16 +42,34 @@ describe('retry', () => {
expect(fetchSpy.mock.calls).toHaveLength(0);

const resolver = new InstrumentedResolver(environment);
ReactTestUtils.renderIntoDocument(
const instance = ReactTestUtils.renderIntoDocument(
<Router resolver={resolver} />,
);

await resolver.done;

expect(fetchSpy.mock.calls).toHaveLength(1);
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'foo');

renderSpy.mock.calls[1][0].retry();
const retryPromise = new Promise((resolve) => {
fetchSpy.mockImplementationOnce(async () => {
// Wait until request resolves to make assertions below.
setTimeout(resolve, 20);

return {
data: {
widget: {
name: 'bar',
},
},
};
});
});

renderSpy.mock.calls.slice(-1)[0][0].retry();
await retryPromise;

await resolver.done;
expect(fetchSpy.mock.calls).toHaveLength(2);
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'bar');
});
});

0 comments on commit 4833144

Please sign in to comment.