-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests to the get upgrade info hook
- Loading branch information
Showing
4 changed files
with
389 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
web/src/components/upgrade_service/hooks/getUpgradeInfo.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @jest-environment jest-fixed-jsdom | ||
*/ | ||
import { http, HttpResponse } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { useGetUpgradeInfo } from "./getUpgradeInfo"; | ||
|
||
describe("useGetUpgradeInfo", () => { | ||
const api = "http://test-api"; | ||
let server; | ||
let queryClient; | ||
let wrapper; | ||
|
||
afterEach(() => { | ||
// Remove any handlers you may have added | ||
// in individual tests (runtime handlers). | ||
server.resetHandlers(); | ||
server.close(); | ||
}); | ||
|
||
beforeEach(() => { | ||
queryClient = new QueryClient(); | ||
wrapper = function wrapperFunc({ children }) { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
}; | ||
}); | ||
|
||
it("normal response", async () => { | ||
const slug = "my-test-app"; | ||
server = setupServer( | ||
http.get(`${api}/upgrade-service/app/${slug}`, () => { | ||
console.log("On handler"); | ||
return HttpResponse.json({ | ||
isConfigurable: true, | ||
hasPreflight: false, | ||
}); | ||
}) | ||
); | ||
server.listen(); | ||
|
||
const { result, waitFor } = renderHook(useGetUpgradeInfo, { | ||
initialProps: { api, slug }, | ||
wrapper, | ||
}); | ||
|
||
await waitFor(() => result.current.isSuccess); | ||
expect(result.current.data.isConfigurable).toBe.true; | ||
Check failure on line 53 in web/src/components/upgrade_service/hooks/getUpgradeInfo.test.jsx GitHub Actions / lint-web
|
||
expect(result.current.data.hasPreflight).toBe.false; | ||
Check failure on line 54 in web/src/components/upgrade_service/hooks/getUpgradeInfo.test.jsx GitHub Actions / lint-web
|
||
}); | ||
|
||
it("non JSON response throws an error and is handled by the hook", async () => { | ||
const slug = "my-test-app"; | ||
server = setupServer( | ||
http.get(`${api}/upgrade-service/app/${slug}`, () => { | ||
return HttpResponse.text("this should produce an error"); | ||
}) | ||
); | ||
server.listen(); | ||
|
||
const { result, waitFor } = renderHook(useGetUpgradeInfo, { | ||
initialProps: { api, slug, retry: 0 }, | ||
wrapper, | ||
}); | ||
|
||
await waitFor(() => result.current.isError); | ||
expect(result.current.error).toBeDefined(); | ||
}); | ||
|
||
it("4xx response throws an error and is handled by the hook", async () => { | ||
const slug = "my-test-app"; | ||
server = setupServer( | ||
http.get(`${api}/upgrade-service/app/${slug}`, () => { | ||
return new HttpResponse("Not found", { status: 404 }); | ||
}) | ||
); | ||
server.listen(); | ||
|
||
const { result, waitFor } = renderHook(useGetUpgradeInfo, { | ||
initialProps: { api, slug, retry: 0 }, | ||
wrapper, | ||
}); | ||
|
||
await waitFor(() => result.current.isError); | ||
expect(result.current.error).toBeDefined(); | ||
}); | ||
|
||
it("5xx response throws an error and is handled by the hook", async () => { | ||
const slug = "my-test-app"; | ||
server = setupServer( | ||
http.get(`${api}/upgrade-service/app/${slug}`, () => { | ||
return new HttpResponse("Something is really broken", { status: 503 }); | ||
}) | ||
); | ||
server.listen(); | ||
|
||
const { result, waitFor } = renderHook(useGetUpgradeInfo, { | ||
initialProps: { api, slug, retry: 0 }, | ||
wrapper, | ||
}); | ||
|
||
await waitFor(() => result.current.isError); | ||
expect(result.current.error).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.