-
Notifications
You must be signed in to change notification settings - Fork 0
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 #27 from qoretechnologies/feature/notifications
Feature/notifications
- Loading branch information
Showing
18 changed files
with
1,025 additions
and
49 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,179 @@ | ||
import { Button } from '@blueprintjs/core'; | ||
import { act, fireEvent, render, screen } from '@testing-library/react'; | ||
import React, { useContext } from 'react'; | ||
import { ReqoreNotificationsContext, ReqoreUIProvider } from '../src/index'; | ||
|
||
const AddButton = (props: any) => { | ||
const { addNotification } = useContext(ReqoreNotificationsContext); | ||
|
||
return ( | ||
<Button | ||
id='add-notification' | ||
onClick={() => | ||
addNotification({ | ||
title: 'Test Notification', | ||
content: 'I am a notification in tests', | ||
duration: 3000, | ||
id: props.id || Date.now(), | ||
...props, | ||
}) | ||
} | ||
> | ||
Add Notification | ||
</Button> | ||
); | ||
}; | ||
|
||
const UpdateButton = (props: any) => { | ||
const { addNotification } = useContext(ReqoreNotificationsContext); | ||
|
||
return ( | ||
<Button | ||
id='update-notification' | ||
onClick={() => | ||
addNotification({ | ||
title: 'Updated Notification', | ||
content: 'I am an updated notification in tests', | ||
duration: 5000, | ||
...props, | ||
}) | ||
} | ||
> | ||
Update Notification | ||
</Button> | ||
); | ||
}; | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
test('Adds notifications and dismisses them automatically', async () => { | ||
act(() => { | ||
render( | ||
<ReqoreUIProvider> | ||
<AddButton /> | ||
</ReqoreUIProvider> | ||
); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
|
||
expect(document.querySelectorAll('.reqore-notification').length).toBe(5); | ||
|
||
act(() => jest.runAllTimers()); | ||
|
||
expect(document.querySelectorAll('.reqore-notification').length).toBe(0); | ||
}); | ||
|
||
test('Adds a notification and updates it', async () => { | ||
act(() => { | ||
render( | ||
<ReqoreUIProvider> | ||
<AddButton id='test' /> | ||
<UpdateButton id='test' /> | ||
</ReqoreUIProvider> | ||
); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Add Notification')); | ||
|
||
act(() => jest.advanceTimersByTime(1000)); | ||
|
||
expect(document.querySelectorAll('.reqore-notification').length).toBe(1); | ||
|
||
fireEvent.click(screen.getByText('Update Notification')); | ||
|
||
expect(document.querySelectorAll('.reqore-notification').length).toBe(1); | ||
|
||
act(() => jest.runAllTimers()); | ||
|
||
expect(document.querySelectorAll('.reqore-notification').length).toBe(0); | ||
}); | ||
|
||
test('Notification has a click event', async () => { | ||
const clickFn = jest.fn(); | ||
|
||
act(() => { | ||
render( | ||
<ReqoreUIProvider> | ||
<AddButton id='test' onClick={clickFn} /> | ||
</ReqoreUIProvider> | ||
); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Add Notification')); | ||
|
||
act(() => jest.advanceTimersByTime(1000)); | ||
|
||
fireEvent.click(document.querySelector('.reqore-notification')); | ||
|
||
expect(clickFn).toHaveBeenCalledWith('test'); | ||
}); | ||
|
||
test('Notification has a close event', async () => { | ||
const closeFn = jest.fn(); | ||
const finishFn = jest.fn(); | ||
|
||
act(() => { | ||
render( | ||
<ReqoreUIProvider> | ||
<AddButton id='test' onClose={closeFn} onFinish={finishFn} /> | ||
</ReqoreUIProvider> | ||
); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Add Notification')); | ||
|
||
act(() => jest.advanceTimersByTime(1000)); | ||
|
||
fireEvent.click(document.querySelector('.reqore-notification-close')); | ||
|
||
expect(closeFn).toHaveBeenCalledWith('test'); | ||
expect(finishFn).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('Notification has a finish event', async () => { | ||
const finishFn = jest.fn(); | ||
|
||
act(() => { | ||
render( | ||
<ReqoreUIProvider> | ||
<AddButton id='test' onFinish={finishFn} /> | ||
</ReqoreUIProvider> | ||
); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Add Notification')); | ||
|
||
act(() => jest.runAllTimers()); | ||
|
||
expect(finishFn).toHaveBeenCalledWith('test'); | ||
}); | ||
|
||
test('Maximum of 5 notifications is shown at once', async () => { | ||
act(() => { | ||
render( | ||
<ReqoreUIProvider> | ||
<AddButton /> | ||
</ReqoreUIProvider> | ||
); | ||
}); | ||
|
||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
fireEvent.click(screen.getByText('Add Notification')); | ||
|
||
expect(document.querySelectorAll('.reqore-notification').length).toBe(5); | ||
}); |
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,58 @@ | ||
import React from 'react'; | ||
import styled, { css } from 'styled-components'; | ||
|
||
export interface IReqoreNotificationsWrapperProps { | ||
children: any; | ||
position?: IReqoreNotificationsPosition; | ||
} | ||
|
||
export type IReqoreNotificationsPosition = | ||
| 'TOP' | ||
| 'BOTTOM' | ||
| 'TOP LEFT' | ||
| 'TOP RIGHT' | ||
| 'BOTTOM LEFT' | ||
| 'BOTTOM RIGHT'; | ||
|
||
export interface IReqoreNotificationsStyle { | ||
positions: string[]; | ||
} | ||
const StyledNotificationsWrapper = styled.div<IReqoreNotificationsStyle>` | ||
position: fixed; | ||
z-index: 9999; | ||
padding: 0 20px 20px 20px; | ||
${({ positions }) => { | ||
const hasTwoPositions = positions.length > 1; | ||
if (hasTwoPositions) { | ||
return css` | ||
${positions[0]}: 30px; | ||
${positions[1]}: 30px; | ||
`; | ||
} | ||
return css` | ||
${positions[0]}: 30px; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
`; | ||
}} | ||
`; | ||
|
||
const ReqoreNotificationsWrapper: React.FC<IReqoreNotificationsWrapperProps> = ({ | ||
children, | ||
position = 'TOP', | ||
}) => ( | ||
<StyledNotificationsWrapper | ||
positions={position | ||
.split(' ') | ||
.map((p: IReqoreNotificationsPosition | string): string => | ||
p.toLowerCase() | ||
)} | ||
> | ||
{children} | ||
</StyledNotificationsWrapper> | ||
); | ||
|
||
export default ReqoreNotificationsWrapper; |
Oops, something went wrong.