-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(app): support banner component * feat(app): hook up warning component * chore(app): test support banner * fix(app): revert unecesarry type update
- Loading branch information
schultztimothy
authored
Aug 3, 2023
1 parent
2aad042
commit ef1a9cc
Showing
7 changed files
with
126 additions
and
3 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
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,51 @@ | ||
import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
import axios from "axios"; | ||
import { SupportBanner } from "../../components/SupportBanner"; | ||
import { CeramicContextState } from "../../context/ceramicContext"; | ||
import { UserContextState } from "../../context/userContext"; | ||
import { | ||
makeTestCeramicContext, | ||
makeTestUserContext, | ||
renderWithContext, | ||
} from "../../__test-fixtures__/contextTestHelpers"; | ||
|
||
jest.mock("../../utils/onboard.ts"); | ||
|
||
const mockBannerResponse = [ | ||
{ | ||
name: "Water Bottles", | ||
content: "We are selling sweet water bottles", | ||
link: "https://www.kleankanteen.com/", | ||
banner_id: 1, | ||
}, | ||
]; | ||
|
||
const mockUserContext: UserContextState = makeTestUserContext(); | ||
const mockCeramicContext: CeramicContextState = makeTestCeramicContext(); | ||
|
||
describe("SupportBanner", () => { | ||
it("should render banner", async () => { | ||
jest.spyOn(axios, "get").mockResolvedValueOnce({ data: mockBannerResponse }); | ||
renderWithContext({ ...mockUserContext, dbAccessTokenStatus: "connected" }, mockCeramicContext, <SupportBanner />); | ||
|
||
await screen.findByText(mockBannerResponse[0].content); | ||
}); | ||
it("should render banner with link", async () => { | ||
jest.spyOn(axios, "get").mockResolvedValueOnce({ data: mockBannerResponse }); | ||
renderWithContext({ ...mockUserContext, dbAccessTokenStatus: "connected" }, mockCeramicContext, <SupportBanner />); | ||
|
||
const link = await screen.findByText("More information."); | ||
expect(link).toHaveAttribute("href", mockBannerResponse[0].link); | ||
}); | ||
it("should dismiss banner", async () => { | ||
const dismissCall = jest.spyOn(axios, "post"); | ||
dismissCall.mockResolvedValueOnce({}); // Mock axios.post to resolve immediately | ||
|
||
jest.spyOn(axios, "get").mockResolvedValueOnce({ data: mockBannerResponse }); | ||
|
||
renderWithContext({ ...mockUserContext, dbAccessTokenStatus: "connected" }, mockCeramicContext, <SupportBanner />); | ||
const dismissBtn = await screen.findByText("Dismiss"); | ||
fireEvent.click(dismissBtn); | ||
await waitFor(() => expect(dismissCall).toHaveBeenCalled()); | ||
}); | ||
}); |
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
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,62 @@ | ||
import axios from "axios"; | ||
import { useContext, useEffect, useState } from "react"; | ||
import { UserContext } from "../context/userContext"; | ||
import { datadogRum } from "@datadog/browser-rum"; | ||
import Warning from "./Warning"; | ||
|
||
type SupportBannerProps = { | ||
content: string; | ||
link: string; | ||
banner_id: number; | ||
}; | ||
|
||
export function SupportBanner() { | ||
const [banners, setBanners] = useState<SupportBannerProps[]>([]); | ||
const { dbAccessToken, dbAccessTokenStatus } = useContext(UserContext); | ||
useEffect(() => { | ||
(async function checkForBanners() { | ||
if (dbAccessTokenStatus === "connected" && dbAccessToken) { | ||
const banners: { | ||
data: SupportBannerProps[]; | ||
} = await axios.get(`${process.env.NEXT_PUBLIC_SCORER_ENDPOINT}/passport-admin/banners`, { | ||
headers: { | ||
Authorization: `Bearer ${dbAccessToken}`, | ||
}, | ||
}); | ||
|
||
setBanners(banners.data); | ||
} | ||
})(); | ||
}, [dbAccessToken, dbAccessTokenStatus]); | ||
|
||
const dismissSupportBanner = async (banner_id: number) => { | ||
try { | ||
if (!dbAccessToken) throw new Error("No access token"); | ||
await axios.post( | ||
`${process.env.NEXT_PUBLIC_SCORER_ENDPOINT}/passport-admin/banners/${banner_id}/dismiss`, | ||
{}, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${dbAccessToken}`, | ||
}, | ||
} | ||
); | ||
setBanners(banners.filter((banner) => banner.banner_id !== banner_id)); | ||
} catch (err) { | ||
datadogRum.addError(err); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{banners.map((banner) => ( | ||
<Warning | ||
key={banner.banner_id} | ||
userWarning={{ content: banner.content, link: banner.link, dismissible: true }} | ||
onDismiss={() => dismissSupportBanner(banner.banner_id)} | ||
className="border-t border-accent-2 px-0" | ||
/> | ||
))} | ||
</> | ||
); | ||
} |
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
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
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