Skip to content
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

Allow setting the ID of the notification manually #8

Merged
merged 4 commits into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ addNotification(notification);

| Property | Type | Default | Description |
| ------------ | ---------------- | ------- | ----------- |
| id | Object | | ID of the notification. If not provided during creation, will be generated automatically using the current timestamp. |
| title | String | | Title of the notification |
| message | String | | Message of the notification |
| image | String | | URL of an image. When an image is defined, status of the notification is set to `default`. |
Expand Down Expand Up @@ -113,12 +114,13 @@ updateNotification(notification);

| Parameter | Type | Description |
| ------------ | -------- | ----------- |
| notification | Object | A [notification](https://github.com/LouisBarranqueiro/reapop/blob/master/docs/api.md#notification-object-properties-1) object |
| notification | Object | A [notification](https://github.com/LouisBarranqueiro/reapop/blob/master/docs/api.md#notification-object-properties-1) object or ID of the notification |

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? If you pass a notification ID, nothing gonna change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, my bad


#### Notification object properties

| Property | Type | Default | Description |
| ------------ | ---------------- | ------- | ----------- |
| id | Object | | ID of the notification. |
| title | String | | Title of the notification |
| message | String | | Message of the notification |
| image | String | | URL of an image. When an image is defined, status of the notification is set to `default`. |
Expand Down Expand Up @@ -176,7 +178,7 @@ removeNotification(id);

| Parameter | Type | Description |
| ----------- | ------ | ----------- |
| id | Number | id of the notification |
| id | Object | ID of the notification |

## Theme

Expand Down
4 changes: 3 additions & 1 deletion src/store/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';
* @returns {Object} notification
*/
export const addNotification = (notification) => (dispatch) => {
notification.id = new Date().getTime();
if (!notification.id) {
notification.id = new Date().getTime();
}
notification = treatNotification(notification);
// if there is an image, we preload it
// and add notification when image is loaded
Expand Down
23 changes: 22 additions & 1 deletion test/store/notifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,28 @@ describe('notifications', () => {
});

it('should create an action to add a notification ' +
'(add `id` property and convert status)', () => {
'(add `id` property if not given and convert status)', () => {
const customId = 'ACTION_ID';
notification.id = customId;
// we remove the image, otherwise `treatNotification()` helper will update
// status of notification
notification.image = null;
// here we simulate an HTTP success status code (200 = OK)
notification.status = 200;
const notificationAdded = store.dispatch(addNotification(notification));
const expectedAction = [{
type: types.ADD_NOTIFICATION,
payload: Object.assign({}, notification, {
id: notificationAdded.id,
status: SUCCESS_STATUS
})
}];
expect(notificationAdded.id).toEqual(customId);
expect(store.getActions()).toEqual(expectedAction);
});

it('should create an action to add a notification ' +
'(and convert status)', () => {
notification.id = null;
// we remove the image, otherwise `treatNotification()` helper will update
// status of notification
Expand Down