From ea86d3f1d81626e52047f7fd8e6bb8002e3d1d30 Mon Sep 17 00:00:00 2001 From: Steve Lamb Date: Tue, 1 Nov 2016 15:25:47 -0700 Subject: [PATCH 1/2] use immutable for reducer state --- example/src/components/container.js | 2 +- package.json | 1 + src/notifications.js | 4 ++-- src/reducer.js | 9 ++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/example/src/components/container.js b/example/src/components/container.js index 849baca..fe75eeb 100644 --- a/example/src/components/container.js +++ b/example/src/components/container.js @@ -55,7 +55,7 @@ Container.contextTypes = { }; Container.propTypes = { - notifications: PropTypes.array + notifications: PropTypes.object }; export default connect( diff --git a/package.json b/package.json index f019a9a..bdbafe3 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "redux": "^3.5.2" }, "dependencies": { + "immutable": "^3.8.1", "react": "^0.14 || ^15.0.0-rc || ^15.0", "react-dom": "^0.14 || ^15.0.0-rc || ^15.0", "react-notification-system": "^0.2.7" diff --git a/src/notifications.js b/src/notifications.js index c85fe7e..3aad835 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -12,7 +12,7 @@ class Notifications extends React.Component { } componentWillReceiveProps(nextProps) { - const {notifications} = nextProps; + const notifications = nextProps.notifications.toJS(); const notificationIds = notifications.map(notification => notification.uid); // Get all active notifications from react-notification-system @@ -48,7 +48,7 @@ class Notifications extends React.Component { } Notifications.propTypes = { - notifications: PropTypes.array + notifications: PropTypes.object }; Notifications.contextTypes = { diff --git a/src/reducer.js b/src/reducer.js index 9aa8c5c..686d95b 100644 --- a/src/reducer.js +++ b/src/reducer.js @@ -1,13 +1,12 @@ +import { List } from 'immutable'; import {RNS_SHOW_NOTIFICATION, RNS_HIDE_NOTIFICATION} from './const'; -export default function Notifications(state = [], action = {}) { +export default function Notifications(state = List([]), action = {}) { switch(action.type) { case RNS_SHOW_NOTIFICATION: const { type, ...rest } = action; - return [ - ...state, - { ...rest, uid: action.uid} - ]; + return state.push({ ...rest, uid: action.uid }); + case RNS_HIDE_NOTIFICATION: return state.filter(notification => { return notification.uid !== action.uid; From 4999a3eadbd1ff790c36162ec425abe117d55de6 Mon Sep 17 00:00:00 2001 From: Steve Lamb Date: Tue, 1 Nov 2016 15:41:22 -0700 Subject: [PATCH 2/2] updated tests --- src/__tests__/notifications.js | 4 ++-- src/__tests__/reducer.js | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/__tests__/notifications.js b/src/__tests__/notifications.js index 5a6bc9b..7f74ef4 100644 --- a/src/__tests__/notifications.js +++ b/src/__tests__/notifications.js @@ -9,12 +9,12 @@ describe('NotificationsComponent', () => { expect(wrapper.children()).toBeDefined(); }); - it('should warn if prop:notifications is not array', () => { + it('should warn if prop:notifications is not object', () => { spyOn(console, 'error'); const wrapper = shallow(); const warning = console.error.calls.argsFor(0)[0]; - expect(warning).toMatch(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `array`./); + expect(warning).toMatch(/Invalid prop `notifications` of type `number` supplied to `Notifications`, expected `object`./); }); }); diff --git a/src/__tests__/reducer.js b/src/__tests__/reducer.js index f872f30..9d90da6 100644 --- a/src/__tests__/reducer.js +++ b/src/__tests__/reducer.js @@ -1,25 +1,26 @@ +import { List, toJS } from 'immutable' import Reducer from '../reducer'; import * as Actions from '../actions'; describe('reducer', () => { it('initializes state with an array', () => { - expect(Reducer()).toEqual([]); + expect(Reducer().toJS()).toEqual([]); }); it('stores the notification to state', () => { const action = Actions.success(); - const state = Reducer([], action); + const state = Reducer(List([]), action); - expect(state.length).toEqual(1); + expect(state.size).toEqual(1); }); it('stores and removes notification', () => { const uid = 1; - const state = Reducer([], Actions.success({ uid })); - expect(state.length).toEqual(1); + const state = Reducer(List([]), Actions.success({ uid })); + expect(state.size).toEqual(1); const newState = Reducer(state, Actions.hide(uid)); - expect(newState.length).toEqual(0); + expect(newState.size).toEqual(0); }); });