Skip to content

Commit

Permalink
trying to fix flaky unit tests and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardotwilio committed Jul 29, 2022
1 parent 67410c7 commit 1c796d2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
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

0 comments on commit 1c796d2

Please sign in to comment.