Skip to content

Commit

Permalink
feat: add useNotification wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Branje <[email protected]>
  • Loading branch information
jeroenbranje committed Jan 31, 2024
1 parent 4ebce65 commit f53799e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 24 deletions.
4 changes: 2 additions & 2 deletions apps/envited.ascs.digital/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NotifiactionContainer } from '../modules/Notifications'
import { NotificationContainer } from '../modules/Notifications'
import { Providers } from '../modules/Theme/Providers'
import './global.css'

Expand All @@ -22,7 +22,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<meta name="theme-color" content="#ffffff"></meta>
</head>
<body className="bg-white dark:bg-gray-800">
<NotifiactionContainer />
<NotificationContainer />
<Providers>{children}</Providers>
</body>
</html>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @jest-environment node
*/
import { useNotification } from './useNotification.hook'
import { notification } from './useNotification.hook'

jest.mock('react-toastify', () => ({
toast: {
Expand All @@ -14,13 +14,14 @@ jest.mock('react-toastify', () => ({
describe('common/notification', () => {
describe('info', () => {
it('should return as expected', async () => {
const { info } = useNotification()
const toastifyStub = jest.fn().mockImplementation(x => x)

const { info } = notification(toastifyStub)

const result = info('INFO_TOAST')

// when ... we want to get the notification
// then ... it returns the content as expected
expect(result).toHaveBeenCalledWith()
expect(result).toEqual('INFO_TOAST')
})
})
Expand All @@ -33,13 +34,12 @@ describe('common/notification', () => {
warning: jest.fn().mockImplementation(x => x),
}

const { success } = useNotification()
const { success } = notification(toastifyStub)

const result = success('SUCCESS_TOAST')

// when ... we want to get the notification
// then ... it returns the content as expected

expect(result).toEqual('SUCCESS_TOAST')
})
})
Expand All @@ -52,7 +52,7 @@ describe('common/notification', () => {
warning: jest.fn().mockImplementation(x => x),
}

const { warning } = useNotification()
const { warning } = notification(toastifyStub)

const result = warning('WARNING_TOAST')

Expand All @@ -70,7 +70,7 @@ describe('common/notification', () => {
warning: jest.fn().mockImplementation(x => x),
}

const { error } = useNotification()
const { error } = notification(toastifyStub)

const result = error('ERROR_TOAST')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { toast } from 'react-toastify'

// export const notification = (notify: typeof toast) => {
export const useNotification = () => {
const info = (content: string) => toast(content)
export const notification = (notify: typeof toast) => {
const info = (content: string) => notify(content)

const success = (content: string) => toast.success(content)
const success = (content: string) => notify.success(content)

const error = (content: string) => toast.error(content)
const error = (content: string) => notify.error(content)

const warning = (content: string) => toast.warning(content)
const warning = (content: string) => notify.warning(content)

return { info, success, error, warning }
}

// export const useNotification = notification(toast)
export const useNotification = () => notification(toast)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, { FC } from 'react'
import { ToastContainer } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'

export const NotifiactionContainer: FC = () => (
export const NotificationContainer: FC = () => (
<ToastContainer
toastClassName="dark:bg-gray-900 dark:text-gray-300"
closeButton={({ closeToast }) => (
Expand Down
2 changes: 1 addition & 1 deletion apps/envited.ascs.digital/modules/Notifications/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { NotifiactionContainer } from './Notifications'
export { NotificationContainer } from './Notifications'
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f53799e

Please sign in to comment.