-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #636 from systemli/Add-Notification-Provider
✨ Add Notification Provider
- Loading branch information
Showing
13 changed files
with
235 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import App from './App' | ||
|
||
describe('App', () => { | ||
it('should render', () => { | ||
render(<App />) | ||
|
||
expect(screen.getByText('Ticker Login')).toBeInTheDocument() | ||
}) | ||
}) |
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,36 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { NotificationProvider } from '../contexts/NotificationContext' | ||
import Notification from './Notification' | ||
|
||
describe('Notification', () => { | ||
function setup() { | ||
return render( | ||
<NotificationProvider> | ||
<Notification /> | ||
</NotificationProvider> | ||
) | ||
} | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it('should render empty notification', () => { | ||
setup() | ||
}) | ||
|
||
it('should render a notification', () => { | ||
vi.mock('../contexts/useNotification', () => ({ | ||
__esModule: true, | ||
default: () => ({ | ||
isOpen: true, | ||
notification: { content: 'Hello, World!' }, | ||
closeNotification: vi.fn(), | ||
}), | ||
})) | ||
|
||
setup() | ||
|
||
expect(screen.getByText('Hello, World!')).toBeInTheDocument() | ||
}) | ||
}) |
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,25 @@ | ||
import { Alert, Snackbar } from '@mui/material' | ||
import { FC } from 'react' | ||
import useNotification from '../contexts/useNotification' | ||
|
||
interface Props { | ||
autoHideDuration?: number | null | ||
} | ||
|
||
const Notification: FC<Props> = ({ autoHideDuration = 6000 }) => { | ||
const { notification, isOpen, closeNotification } = useNotification() | ||
|
||
if (isOpen && notification?.content) { | ||
return ( | ||
<Snackbar anchorOrigin={{ vertical: 'top', horizontal: 'right' }} autoHideDuration={autoHideDuration} onClose={closeNotification} open={isOpen}> | ||
<Alert onClose={closeNotification} severity={notification.severity || 'info'}> | ||
{notification.content} | ||
</Alert> | ||
</Snackbar> | ||
) | ||
} | ||
|
||
return null | ||
} | ||
|
||
export default Notification |
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,29 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import NotificationContext, { NotificationProvider } from './NotificationContext' | ||
|
||
describe('NotificationContext', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
function setup() { | ||
return render( | ||
<NotificationProvider> | ||
<NotificationContext.Consumer> | ||
{value => ( | ||
<div> | ||
{value?.notification?.content} | ||
{value?.isOpen?.toString()} | ||
</div> | ||
)} | ||
</NotificationContext.Consumer> | ||
</NotificationProvider> | ||
) | ||
} | ||
|
||
it('should render', async () => { | ||
setup() | ||
|
||
expect(await screen.findByText('false')).toBeInTheDocument() | ||
}) | ||
}) |
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,45 @@ | ||
import { ReactNode, createContext, useMemo, useState } from 'react' | ||
|
||
type Severity = 'success' | 'error' | 'warning' | 'info' | ||
|
||
export interface Notification { | ||
content: ReactNode | ||
severity?: Severity | ||
} | ||
|
||
interface NotificationContextType { | ||
notification?: Notification | ||
isOpen?: boolean | ||
createNotification: (notification: Notification) => void | ||
closeNotification: () => void | ||
} | ||
|
||
const NotificationContext = createContext<NotificationContextType | undefined>(undefined) | ||
|
||
export function NotificationProvider({ children }: Readonly<{ children: ReactNode }>): JSX.Element { | ||
const [notification, setNotification] = useState<Notification | undefined>(undefined) | ||
const [isOpen, setIsOpen] = useState<boolean>(false) | ||
|
||
const createNotification = (notification: Notification): void => { | ||
setNotification(notification) | ||
setIsOpen(true) | ||
} | ||
|
||
const closeNotification = (): void => { | ||
setIsOpen(false) | ||
} | ||
|
||
const contextValue = useMemo( | ||
() => ({ | ||
notification, | ||
isOpen, | ||
createNotification, | ||
closeNotification, | ||
}), | ||
[notification, isOpen] | ||
) | ||
|
||
return <NotificationContext.Provider value={contextValue}>{children}</NotificationContext.Provider> | ||
} | ||
|
||
export default NotificationContext |
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,24 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { ReactNode } from 'react' | ||
import { NotificationProvider } from './NotificationContext' | ||
import useNotification from './useNotification' | ||
|
||
describe('useNotification', () => { | ||
it('throws error when not rendered within NotificationProvider', () => { | ||
const { result } = renderHook(() => useNotification()) | ||
|
||
expect(result.error).toEqual(Error('useNotification must be used within a NotificationProvider')) | ||
}) | ||
|
||
it('returns context when rendered within NotificationProvider', async () => { | ||
const wrapper = ({ children }: { children: ReactNode }) => <NotificationProvider>{children}</NotificationProvider> | ||
const { result } = renderHook(() => useNotification(), { wrapper }) | ||
|
||
expect(result.current).toEqual({ | ||
isOpen: false, | ||
notification: undefined, | ||
closeNotification: expect.any(Function), | ||
createNotification: expect.any(Function), | ||
}) | ||
}) | ||
}) |
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,12 @@ | ||
import { useContext } from 'react' | ||
import NotificationContext from './NotificationContext' | ||
|
||
const useNotification = () => { | ||
const context = useContext(NotificationContext) | ||
if (!context) { | ||
throw new Error('useNotification must be used within a NotificationProvider') | ||
} | ||
return context | ||
} | ||
|
||
export default useNotification |
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