Skip to content

Make sure query is refetched specified number of times #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/sandbox/src/index.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let queryTimeMin = 1000;
let queryTimeMax = 2000;

const fetchTodos = (key, { filter } = {}) => {
console.log("fetchTodos", { filter });
console.info("fetchTodos", { filter });
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < errorRate) {
Expand All @@ -74,13 +74,13 @@ const fetchTodos = (key, { filter } = {}) => {
}, queryTimeMin + Math.random() * (queryTimeMax - queryTimeMin));
});

promise.cancel = () => console.log("cancelled", filter);
promise.cancel = () => console.info("cancelled", filter);

return promise;
};

const fetchTodoById = (key, { id }) => {
console.log("fetchTodoById", { id });
console.info("fetchTodoById", { id });
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < errorRate) {
Expand All @@ -94,7 +94,7 @@ const fetchTodoById = (key, { id }) => {
};

const postTodo = ({ name, notes }) => {
console.log("postTodo", { name, notes });
console.info("postTodo", { name, notes });
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < errorRate) {
Expand All @@ -110,7 +110,7 @@ const postTodo = ({ name, notes }) => {
};

const patchTodo = todo => {
console.log("patchTodo", todo);
console.info("patchTodo", todo);
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < errorRate) {
Expand Down
4 changes: 2 additions & 2 deletions examples/suspense/src/queries.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";

export async function fetchProjects(key) {
console.log("fetch projects");
console.info("fetch projects");
let { data } = await axios.get(
`https://api.github.com/users/tannerlinsley/repos?sort=updated`
);
Expand All @@ -10,7 +10,7 @@ export async function fetchProjects(key) {
}

export async function fetchProject(key, { id }) {
console.log("fetch project id", id);
console.info("fetch project id", id);
let { data } = await axios.get(
`https://api.github.com/repos/tannerlinsley/${id}`
);
Expand Down
4 changes: 2 additions & 2 deletions src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export function makeQueryCache() {
() => {
cache.removeQueries(d => d.queryHash === query.queryHash)
},
typeof query.state.data === 'undefined' ? 0 : query.config.cacheTime
(typeof query.state.data === 'undefined' && query.state.status !== 'error') ? 0 : query.config.cacheTime
)
}

Expand Down Expand Up @@ -276,7 +276,7 @@ export function makeQueryCache() {
if (
// Only retry if the document is visible
query.config.retry === true ||
query.state.failureCount < query.config.retry
query.state.failureCount <= query.config.retry
) {
if (!isDocumentVisible()) {
return new Promise(noop)
Expand Down
33 changes: 32 additions & 1 deletion src/tests/useQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('useQuery', () => {
() => {
return Promise.reject('Error test')
},
{ retry: false },
{ retry: false }
)

return (
Expand All @@ -179,4 +179,35 @@ describe('useQuery', () => {

await waitForElement(() => rendered.getByText('error'))
})

it('should retry specified number of times', async () => {
const queryFn = jest.fn()
queryFn.mockImplementation(() => {
return Promise.reject('Error test')
})

function Page() {
const { status, failureCount } = useQuery('test', queryFn, {
retry: 1,
retryDelay: 1,
})

return (
<div>
<h1>{status}</h1>
<h2>Failed {failureCount} times</h2>
</div>
)
}

const rendered = render(<Page />)

await waitForElement(() => rendered.getByText('loading'))
await waitForElement(() => rendered.getByText('error'))

// query should fail `retry + 1` times, since first time isn't a "retry"
await waitForElement(() => rendered.getByText('Failed 2 times'))

expect(queryFn).toHaveBeenCalledTimes(2)
})
})