-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds the PromoBar
component and a new custom hook useNotificationMessage
to the PatronPage
#606
Merged
kasperbirch1
merged 17 commits into
danskernesdigitalebibliotek:develop
from
reload:DDFLSBP-84-brugerprofil-systemfeedback-mangler-pa-gem
Oct 30, 2023
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
61be2ba
Add `promobar` to `PatronPage` + Introduce `useNotificationMessage` hook
kasperbirch1 0b3374e
Add margin to the icon in `PromoBarIcon`
kasperbirch1 5df01b9
Add new translations for `promobar` in `PatronPage`
kasperbirch1 15f855a
Handle button loading state in `PatronPage`
kasperbirch1 36d2d3a
Translate string to english
kasperbirch1 6bc7f33
Refactor `useNotificationMessage`
kasperbirch1 72210b7
Prefix Patron translations with 'patronPage'
kasperbirch1 174dd93
Separate `NotificationComponent` from `useNotificationMessage` hook
kasperbirch1 64c268f
Refactor logic to decide returning `<NotificationComponent />` or null.
kasperbirch1 1743168
Add tests for the useNotificationMessage hook
spaceo 8d94fba
Add assertion for scroll in notification message test
spaceo c69fb9d
Add assertion for notification message will disappear
kasperbirch1 2f49d9c
Refactor test and component for `useNotificationMessage` hook
kasperbirch1 a7e4e68
Improve message existence checks in `useNotificationMessage` hook tests
kasperbirch1 ac3628c
Enhance testing for `useNotificationMessage` hook with custom options
kasperbirch1 7de603d
Remove unnecessary use of async
kasperbirch1 31af99b
Correct test description
kasperbirch1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from "react"; | ||
import PromoBar from "../promo-bar/PromoBar"; | ||
|
||
interface NotificationComponentProps { | ||
notificationMessage: string; | ||
} | ||
|
||
const NotificationComponent: React.FC<NotificationComponentProps> = ({ | ||
notificationMessage | ||
}) => { | ||
return <PromoBar text={notificationMessage} type="info" />; | ||
}; | ||
|
||
export default NotificationComponent; |
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,39 @@ | ||
import React, { useState } from "react"; | ||
import NotificationComponent from "../../components/notification/NotificationComponent"; | ||
|
||
export type UseNotificationOptionsType = { | ||
timeout?: number; | ||
scrollToTop?: boolean; | ||
}; | ||
type UseNotificationReturnType = [React.FC, (text: string) => void]; | ||
|
||
export const useNotificationMessage = ({ | ||
timeout = 5000, | ||
scrollToTop = true | ||
}: UseNotificationOptionsType = {}): UseNotificationReturnType => { | ||
const [notificationMessage, setNotificationMessage] = useState<string | null>( | ||
null | ||
); | ||
|
||
const handleNotificationMessage = (text: string) => { | ||
setNotificationMessage(text); | ||
if (scrollToTop) { | ||
window.scrollTo(0, 0); | ||
} | ||
if (timeout) { | ||
setTimeout(() => { | ||
setNotificationMessage(null); | ||
}, timeout); | ||
} | ||
}; | ||
|
||
return [ | ||
() => | ||
notificationMessage ? ( | ||
<NotificationComponent notificationMessage={notificationMessage} /> | ||
) : null, | ||
handleNotificationMessage | ||
]; | ||
}; | ||
|
||
export default {}; |
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,136 @@ | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
import React from "react"; | ||
import { cleanup, fireEvent, render, screen } from "@testing-library/react"; | ||
import { | ||
UseNotificationOptionsType, | ||
useNotificationMessage | ||
} from "../../core/utils/useNotificationMessage"; | ||
|
||
// Define a test component that utilizes the useNotificationMessage hook | ||
const ComponentWithNotificationMessage = ({ | ||
timeout, | ||
scrollToTop | ||
}: UseNotificationOptionsType) => { | ||
const [NotificationMessage, handler] = useNotificationMessage({ | ||
timeout, | ||
scrollToTop | ||
}); | ||
|
||
return ( | ||
<div data-testid="wrapper"> | ||
<NotificationMessage /> | ||
<button | ||
data-testid="button" | ||
type="button" | ||
onClick={() => handler("Some message")} | ||
> | ||
Click me | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
describe("useNotificationMessage hook", () => { | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it("should not display a message before the button is clicked", () => { | ||
const { getByTestId } = render(<ComponentWithNotificationMessage />); | ||
const wrapper = getByTestId("wrapper"); | ||
|
||
// Expectations before the button is clicked | ||
expect(screen.queryByText(/Some message/)).toBeNull(); // Expect the message to not be displayed | ||
kasperbirch1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Assert that the wrapper does not contain the message initially | ||
expect(wrapper).toMatchInlineSnapshot(` | ||
<div | ||
data-testid="wrapper" | ||
spaceo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> | ||
<button | ||
data-testid="button" | ||
type="button" | ||
> | ||
Click me | ||
</button> | ||
</div> | ||
`); | ||
}); | ||
|
||
it("should display a message after button activation with default timeout and scrollToTop settings initialized", () => { | ||
vi.spyOn(window, "scrollTo"); | ||
vi.spyOn(window, "setTimeout"); | ||
|
||
const { getByTestId } = render(<ComponentWithNotificationMessage />); | ||
const wrapper = getByTestId("wrapper"); | ||
const button = getByTestId("button"); | ||
|
||
// Simulate button click | ||
fireEvent.click(button); | ||
|
||
// Expectations after the button is clicked | ||
expect(window.scrollTo).toHaveBeenCalledWith(0, 0); // Expect page to scroll to top | ||
expect(window.setTimeout).toHaveBeenCalledTimes(1); // Expect setTimeout to be called once | ||
expect(screen.queryByText(/Some message/)).toBeTruthy(); // Expect the message to be displayed | ||
|
||
// Assert final state of the wrapper | ||
expect(wrapper).toMatchInlineSnapshot(` | ||
<div | ||
data-testid="wrapper" | ||
> | ||
<section | ||
class="promo-bar" | ||
> | ||
<img | ||
alt="" | ||
class="ml-4" | ||
src="/node_modules/@danskernesdigitalebibliotek/dpl-design-system/build/icons/basic/icon-info.svg" | ||
/> | ||
<p | ||
class="text-small-caption" | ||
> | ||
Some message | ||
</p> | ||
</section> | ||
<button | ||
data-testid="button" | ||
type="button" | ||
> | ||
Click me | ||
</button> | ||
</div> | ||
`); | ||
}); | ||
|
||
it("should keep displaying a message indefinitely if timeout is removed", () => { | ||
vi.spyOn(window, "setTimeout"); | ||
|
||
const { getByTestId } = render( | ||
<ComponentWithNotificationMessage timeout={0} /> | ||
); | ||
const button = getByTestId("button"); | ||
|
||
// Simulate button click | ||
fireEvent.click(button); | ||
|
||
// Expectations | ||
expect(window.setTimeout).not.toHaveBeenCalled(); // Expect setTimeout to not be called | ||
expect(screen.queryByText(/Some message/)).toBeTruthy(); // Expect the message to be displayed indefinitely | ||
}); | ||
|
||
it("should not scroll to top if scrollToTop is false", () => { | ||
vi.spyOn(window, "scrollTo"); | ||
|
||
const { getByTestId } = render( | ||
<ComponentWithNotificationMessage scrollToTop={false} /> | ||
); | ||
const button = getByTestId("button"); | ||
|
||
// Simulate button click | ||
fireEvent.click(button); | ||
|
||
// Expectations | ||
expect(window.scrollTo).not.toHaveBeenCalled(); // Expect page to not scroll to top | ||
expect(screen.queryByText(/Some message/)).toBeTruthy(); // Expect the message to be displayed | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have refactored this data prop, what about dpl-cms then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is fixed here: https://github.com/danskernesdigitalebibliotek/dpl-cms/pull/427/files