-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * wip * prettier * added more tests * fixed linting issues * ci fix due to node version * prettier * tested telemetry
- Loading branch information
1 parent
0ec1bd4
commit 103020b
Showing
15 changed files
with
515 additions
and
236 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
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
This file was deleted.
Oops, something went wrong.
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,57 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { Message, useBannerMessageConfig } from "./useBannerMessageConfig"; | ||
|
||
describe("useBannerMessageConfig", () => { | ||
let messages: Message[]; | ||
|
||
beforeEach(() => { | ||
messages = [ | ||
{ start: new Date(Date.now() + 3000) }, | ||
{ | ||
start: new Date(Date.now() - 2000), | ||
ends: new Date(Date.now() + 2000), | ||
}, | ||
{ ends: new Date(Date.now() + 3000) }, | ||
{ start: new Date(Date.now() - 3000) }, | ||
{ | ||
start: new Date(Date.now() + 2000), | ||
ends: new Date(Date.now() + 4000), | ||
}, | ||
{ ends: new Date(Date.now() + 4000) }, | ||
{ ends: new Date(Date.now() - 4000) }, | ||
{ start: new Date(Date.now() + 4000) }, | ||
].map((m, i) => ({ | ||
...m, | ||
background: `bg_${i}`, | ||
content: <p>index {i}</p>, | ||
})); | ||
}); | ||
|
||
it("should get as a first priority a message that has no start date and ends in the future", () => { | ||
const { result } = renderHook(() => useBannerMessageConfig(messages)); | ||
expect(result.current).toEqual(messages[2]); | ||
}); | ||
|
||
it("should get as a second priority a message that has not started and ends in the future", () => { | ||
const { result } = renderHook(() => | ||
useBannerMessageConfig([ | ||
messages[0], | ||
messages[1], | ||
/** messages[2] this one would be the priority if set */ | ||
messages[3], | ||
messages[4], | ||
messages[5], | ||
messages[6], | ||
messages[7], | ||
]) | ||
); | ||
expect(result.current).toEqual(messages[5]); | ||
}); | ||
|
||
it("should return null when no message matches the criteria", () => { | ||
const { result } = renderHook(() => | ||
useBannerMessageConfig([messages[0], messages[6]]) | ||
); | ||
expect(result.current).toEqual(null); | ||
}); | ||
}); |
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,36 @@ | ||
import { useMemo } from "react"; | ||
|
||
export interface Message { | ||
background: string; | ||
button?: { | ||
href: string; | ||
label?: string; | ||
background: string; | ||
}; | ||
content: JSX.Element; | ||
start?: Date; | ||
ends?: Date; | ||
} | ||
|
||
const criteria = (left: Message, right: Message): number => { | ||
if (left.start && right.start) { | ||
return right.start.getTime() - left.start.getTime(); | ||
} | ||
if (left.start) return 1; | ||
if (right.start) return -1; | ||
return 0; | ||
}; | ||
|
||
export const useBannerMessageConfig = (messages: Message[]): Message | null => { | ||
return useMemo(() => { | ||
const now = new Date(); | ||
const message = [...messages] | ||
.sort(criteria) | ||
.find( | ||
(message) => | ||
(!message.start || message.start < now) && | ||
(!message.ends || message.ends > now) | ||
); | ||
return message || null; | ||
}, [messages]); | ||
}; |
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,64 @@ | ||
import { renderHook, waitFor } from "@testing-library/react"; | ||
import { useMessages } from "./useMessages"; | ||
import { PropsWithChildren } from "react"; | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
|
||
const Wrapper = ({ children }: PropsWithChildren) => ( | ||
<QueryClientProvider client={new QueryClient()}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
|
||
describe("useMessages", () => { | ||
beforeEach(() => { | ||
global.fetch = jest | ||
.fn() | ||
.mockReturnValue({ status: 200, json: async () => [] }); | ||
}); | ||
|
||
it("should get relevant parsed messages", async () => { | ||
const irrelevant = { | ||
id: "first", | ||
background: "yellow", | ||
content: { color: "black", text: "first message" }, | ||
since: Date.now() - 5000, | ||
until: Date.now() - 4000, | ||
}; | ||
const relevant = { | ||
id: "second", | ||
background: "green", | ||
content: { color: "white", text: "second message" }, | ||
since: Date.now() - 1000, | ||
until: Date.now() + 1000, | ||
}; | ||
global.fetch = jest.fn().mockResolvedValue({ | ||
status: 200, | ||
json: async () => [irrelevant, relevant], | ||
}); | ||
const { result } = renderHook(() => useMessages(), { wrapper: Wrapper }); | ||
|
||
expect(result.current).toEqual(undefined); | ||
await waitFor(() => { | ||
expect(result.current).toEqual([ | ||
{ | ||
background: "green", | ||
button: undefined, | ||
content: { color: "white", size: undefined, text: "second message" }, | ||
id: "second", | ||
since: expect.any(Date), | ||
until: expect.any(Date), | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
it("should fallback to an empty array when fetching fails", async () => { | ||
global.fetch = jest.fn().mockResolvedValue({ status: 500 }); | ||
const { result } = renderHook(() => useMessages(), { wrapper: Wrapper }); | ||
|
||
expect(result.current).toEqual(undefined); | ||
await waitFor(() => { | ||
expect(result.current).toEqual([]); | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
export interface Banner { | ||
id: string; | ||
background: string; | ||
button?: { | ||
href: string; | ||
label?: string; | ||
background: string; | ||
}; | ||
content: { | ||
text: string; | ||
color?: string; | ||
size?: string; | ||
}; | ||
since: Date; | ||
until: Date; | ||
} | ||
|
||
const parse = (banner: Record<string, any>): Banner => { | ||
const id = banner.id; | ||
const background = banner.background; | ||
const since = new Date(banner.since); | ||
const until = new Date(banner.until); | ||
const content = { | ||
text: banner.content.text, | ||
color: banner.content.color, | ||
size: banner.content.size, | ||
}; | ||
const button = banner.button; | ||
return { id, background, since, until, content, button }; | ||
}; | ||
|
||
const fetchMessages = async ( | ||
location: string = "/data/banners.json" | ||
): Promise<Banner[]> => { | ||
const response = await fetch(location); | ||
if (response.status !== 200) return []; | ||
const json = await response.json(); | ||
return json.map((banner: Record<string, any>) => parse(banner)); | ||
}; | ||
|
||
export const useMessages = () => { | ||
const now = new Date(); | ||
const allMessages = useQuery({ | ||
queryKey: ["messages"], | ||
queryFn: () => fetchMessages(), | ||
}); | ||
return allMessages.data?.filter( | ||
({ until, since }: Banner) => since < now && until > now | ||
); | ||
}; |
Oops, something went wrong.