Skip to content
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

Trying to fix flaky unit tests and update readme #3

Merged
merged 1 commit into from
Jul 29, 2022
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ the same set of transition rules.

### **State transitions**

<img width="572" alt="image" src="https://user-images.githubusercontent.com/196860/181434040-acddacd3-7e27-448a-b751-5ec8a409412f.png">
<img width="572" alt="image" src="https://user-images.githubusercontent.com/63280641/181743461-b5780c22-5141-49f9-a188-1917032468a1.png">

- By default, the button loads in Ready state.
- From Ready, the launch button can transition to Working when the user press the button, starting the ignition process.
- If the launch button is again pressed while its working, it will transition to Errored (canceling the ongoing request inmediately)
- If the request takes too long to complete, the button will transition to Errored.
- If there is an error with the fetch operation, the button will transition to Errored.
- It will be possible to transition from Errored back to Ready only if the user has manually aborted the request before so they can retry again.
- It will be possible to transition from Errored back to Working only if the user has manually aborted the request before so they can retry again.

## Stack

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const CancellableRequestButton = ({

const onClickHandler = () => {
const isReady = currentState === "ready" && !isFetching;
const wasRequestAborted = currentState === "error" && !isAborted;
const wasRequestAborted = currentState === "error" && isAborted;

if (isReady || wasRequestAborted) {
makeRequest();
Expand Down
9 changes: 4 additions & 5 deletions src/lib/hooks/useCancellableFetch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ describe("Hooks > useCancellableFetch", () => {
});

test("It fetches data from the provided URL", async () => {
(fetch as FetchMock).mockResponseOnce(JSON.stringify({ data: "test" }));
(fetch as FetchMock).mockResponseOnce(() =>
Promise.resolve(JSON.stringify({ data: "test" }))
);

const { result, waitForNextUpdate } = renderHook(
() => useCancellableFetch({ queryKey: "test", url: "test" }),
{ wrapper }
);

await act( () => {
act(() => {
result.current.request();
});

/*
This test is flaky, sometimes it fails with a timeout error. Need to figure out why.
await waitForNextUpdate();
*/

expect(fetch).toHaveBeenCalledWith("test", expect.anything());

Expand Down