Skip to content

Commit

Permalink
fix: fix useNotifications().notify function (overload) (#432)
Browse files Browse the repository at this point in the history
- closes #411
  • Loading branch information
LouisBarranqueiro authored Sep 5, 2021
1 parent e4c9e63 commit f2e5033
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/components/NotificationsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import {ReactNode, useReducer, useCallback} from 'react'
import notificationsReducer from '../reducers/notifications/reducer'
import {NewNotification} from '../reducers/notifications/types'
import {dismissNotification, dismissNotifications, notify} from '../reducers/notifications/actions'
import {ReapopNotificationsContext} from '../contexts/reapopNotificationsContext'

Expand All @@ -14,8 +13,8 @@ export const NotificationsProvider = (props: Props) => {
const context = {
notifications,
notify: useCallback(
(notification: NewNotification) => {
const action = notify(notification)
(...args: [any, any?, any?]) => {
const action = notify(...args)
dispatch(action)
return action.payload
},
Expand Down
9 changes: 7 additions & 2 deletions src/components/tests/NotificationsProvider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ describe('<NotificationsProvider/>', () => {
expect(useReducer).toHaveBeenNthCalledWith(1, mockReducer(), [])
expect(notifications).toEqual([])

const notification = notify({id: '1'})
let notification = notify({id: '1'})
expect(notification).toMatchSnapshot()
expect(mockDispatch.mock.calls).toMatchSnapshot()
jest.clearAllMocks()

notification = notify('world!', 'info', {title: 'hello', id: '1'})
expect(notification).toMatchSnapshot()
expect(mockDispatch.mock.calls).toMatchSnapshot()
jest.clearAllMocks()

dismissNotification('1')
expect(mockDispatch.mock.calls).toMatchSnapshot()

jest.clearAllMocks()

dismissNotifications()
expect(mockDispatch.mock.calls).toMatchSnapshot()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ Array [
`;

exports[`<NotificationsProvider/> should provide reducer state and actions 3`] = `
Object {
"buttons": Array [],
"id": "1",
"message": "world!",
"status": "info",
"title": "hello",
}
`;

exports[`<NotificationsProvider/> should provide reducer state and actions 4`] = `
Array [
Array [
Object {
"payload": Object {
"buttons": Array [],
"id": "1",
"message": "world!",
"status": "info",
"title": "hello",
},
"type": "reapop/upsertNotification",
},
],
]
`;

exports[`<NotificationsProvider/> should provide reducer state and actions 5`] = `
Array [
Array [
Object {
Expand All @@ -34,7 +61,7 @@ Array [
]
`;

exports[`<NotificationsProvider/> should provide reducer state and actions 4`] = `
exports[`<NotificationsProvider/> should provide reducer state and actions 6`] = `
Array [
Array [
Object {
Expand Down
13 changes: 10 additions & 3 deletions src/contexts/reapopNotificationsContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import {createContext} from 'react'
import {NewNotification, Notification} from '../reducers/notifications/types'
import {Notification, Status} from '../reducers/notifications/types'

type NotificationsContext = {
declare function upsertNotification(notification: Partial<Notification>): Notification
declare function upsertNotification(message: string, options?: Partial<Notification>): Notification
declare function upsertNotification(message: string, status: Status, options?: Partial<Notification>): Notification
declare function upsertNotification(
...args: [Partial<Notification> | string, (Partial<Notification> | Status)?, Partial<Notification>?]
): Notification

export type NotificationsContext = {
notifications: Notification[]
notify: (notification: NewNotification) => void
notify: typeof upsertNotification
dismissNotification: (id: string) => void
dismissNotifications: () => void
}
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/notifications/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export type DismissNotificationsAction = {
function upsertNotification(notification: Partial<Notification>): UpsertNotificationAction
function upsertNotification(message: string, options?: Partial<Notification>): UpsertNotificationAction
function upsertNotification(message: string, status: Status, options?: Partial<Notification>): UpsertNotificationAction
function upsertNotification(...args: unknown[]): UpsertNotificationAction {
function upsertNotification(
...args: [Partial<Notification> | string, (Partial<Notification> | Status)?, Partial<Notification>?]
): UpsertNotificationAction {
const lastArg = args[args.length - 1]
let notification: NewNotification = {}

Expand Down

0 comments on commit f2e5033

Please sign in to comment.