From 01560b6fbe0a06cf955dc2bea5fd61aa292c2e89 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Sun, 4 Feb 2024 11:37:38 -0500 Subject: [PATCH] Add test when no site data available --- app/routes/dashboard.test.tsx | 36 +++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/app/routes/dashboard.test.tsx b/app/routes/dashboard.test.tsx index 43433ebf..372701ff 100644 --- a/app/routes/dashboard.test.tsx +++ b/app/routes/dashboard.test.tsx @@ -43,12 +43,8 @@ describe("Dashboard route", () => { test("redirects to ?site=siteId if no siteId is provided via query string", async () => { // response for getSitesByOrderedHits fetch.mockResolvedValueOnce( - new Promise((resolve) => { - resolve( - createFetchResponse({ - data: [{ siteId: "test-siteid", count: 1 }], - }), - ); + createFetchResponse({ + data: [{ siteId: "test-siteid", count: 1 }], }), ); @@ -72,6 +68,34 @@ describe("Dashboard route", () => { ); }); + test("redirects to ?site= if no siteId is provided via query string / no site data", async () => { + // response for getSitesByOrderedHits + fetch.mockResolvedValueOnce( + createFetchResponse({ + data: [], + }), + ); + + const response = await loader({ + context: { + env: { + CF_BEARER_TOKEN: "fake", + CF_ACCOUNT_ID: "fake", + }, + }, + // @ts-expect-error we don't need to provide all the properties of the request object + request: { + url: "http://localhost:3000/dashboard", // no site query param + }, + }); + + // expect redirect + expect(response.status).toBe(302); + expect(response.headers.get("Location")).toBe( + "http://localhost:3000/dashboard?site=", + ); + }); + test("assembles data returned from CF API", async () => { // response for getSitesByOrderedHits fetch.mockResolvedValueOnce(