diff --git a/__tests__/components/NotificationList.test.jsx b/__tests__/components/NotificationList.test.jsx index c08de77350..a743c3d81d 100644 --- a/__tests__/components/NotificationList.test.jsx +++ b/__tests__/components/NotificationList.test.jsx @@ -12,29 +12,29 @@ describe('', () => { ); }); test('fetches notifications on mount', () => { - const spy = jest.spyOn(Notifications.prototype, 'fetchNotifications'); + const spy = jest.spyOn(Notifications.prototype, 'readNotifications'); mount( @@ -42,18 +42,18 @@ describe('', () => { expect(spy).toHaveBeenCalled(); }); test('toggle success calls post', () => { - const spy = jest.spyOn(Notifications.prototype, 'postToSuccess'); + const spy = jest.spyOn(Notifications.prototype, 'createSuccess'); const wrapper = mount( @@ -62,43 +62,43 @@ describe('', () => { expect(spy).toHaveBeenCalledWith(1, true); }); test('post success makes request and updates state properly', async () => { - const postSuccessFn = jest.fn(); + const createSuccess = jest.fn(); const wrapper = mount( ).find('Notifications'); wrapper.setState({ successTemplateIds: [44] }); - await wrapper.instance().postToSuccess(44, true); - expect(postSuccessFn).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); + await wrapper.instance().createSuccess(44, true); + expect(createSuccess).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); expect(wrapper.state('successTemplateIds')).not.toContain(44); - await wrapper.instance().postToSuccess(44, false); - expect(postSuccessFn).toHaveBeenCalledWith(1, { id: 44 }); + await wrapper.instance().createSuccess(44, false); + expect(createSuccess).toHaveBeenCalledWith(1, { id: 44 }); expect(wrapper.state('successTemplateIds')).toContain(44); }); test('toggle error calls post', () => { - const spy = jest.spyOn(Notifications.prototype, 'postToError'); + const spy = jest.spyOn(Notifications.prototype, 'createError'); const wrapper = mount( @@ -107,28 +107,28 @@ describe('', () => { expect(spy).toHaveBeenCalledWith(1, true); }); test('post error makes request and updates state properly', async () => { - const postErrorFn = jest.fn(); + const createError = jest.fn(); const wrapper = mount( ).find('Notifications'); wrapper.setState({ errorTemplateIds: [44] }); - await wrapper.instance().postToError(44, true); - expect(postErrorFn).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); + await wrapper.instance().createError(44, true); + expect(createError).toHaveBeenCalledWith(1, { id: 44, disassociate: true }); expect(wrapper.state('errorTemplateIds')).not.toContain(44); - await wrapper.instance().postToError(44, false); - expect(postErrorFn).toHaveBeenCalledWith(1, { id: 44 }); + await wrapper.instance().createError(44, false); + expect(createError).toHaveBeenCalledWith(1, { id: 44 }); expect(wrapper.state('errorTemplateIds')).toContain(44); }); test('fetchNotifications', async () => { @@ -137,7 +137,7 @@ describe('', () => { page_size: 10, order_by: 'name' }; - const getNotificationsFn = jest.fn().mockResolvedValue({ + const onReadNotifications = jest.fn().mockResolvedValue({ data: { results: [ { id: 1, notification_type: 'slack' }, @@ -146,14 +146,14 @@ describe('', () => { ] } }); - const getSuccessFn = jest.fn().mockResolvedValue({ + const onReadSuccess = jest.fn().mockResolvedValue({ data: { results: [ { id: 1 } ] } }); - const getErrorFn = jest.fn().mockResolvedValue({ + const readError = jest.fn().mockResolvedValue({ data: { results: [ { id: 2 } @@ -166,22 +166,22 @@ describe('', () => { ).find('Notifications'); wrapper.instance().updateUrl = jest.fn(); - await wrapper.instance().fetchNotifications(mockQueryParams); - expect(getNotificationsFn).toHaveBeenCalledWith(1, mockQueryParams); - expect(getSuccessFn).toHaveBeenCalledWith(1, { + await wrapper.instance().readNotifications(mockQueryParams); + expect(onReadNotifications).toHaveBeenCalledWith(1, mockQueryParams); + expect(onReadSuccess).toHaveBeenCalledWith(1, { id__in: '1,2,3' }); - expect(getErrorFn).toHaveBeenCalledWith(1, { + expect(readError).toHaveBeenCalledWith(1, { id__in: '1,2,3' }); expect(wrapper.state('successTemplateIds')).toContain(1); diff --git a/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.jsx b/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.jsx index 9716eadf8a..d8e6723cb1 100644 --- a/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.jsx +++ b/__tests__/pages/Organizations/screens/Organization/OrganizationNotifications.jsx @@ -44,11 +44,11 @@ describe('', () => { /> ).find('OrganizationNotifications'); - wrapper.instance().getOrgNotifications(1, { foo: 'bar' }); + wrapper.instance().readOrgNotifications(1, { foo: 'bar' }); expect(getOrganizationNotifications).toHaveBeenCalledWith(1, { foo: 'bar' }); - wrapper.instance().getOrgNotificationSuccess(1, { foo: 'bar' }); + wrapper.instance().readOrgNotificationSuccess(1, { foo: 'bar' }); expect(getOrganizationNotificationSuccess).toHaveBeenCalledWith(1, { foo: 'bar' }); - wrapper.instance().getOrgNotificationError(1, { foo: 'bar' }); + wrapper.instance().readOrgNotificationError(1, { foo: 'bar' }); expect(getOrganizationNotificationError).toHaveBeenCalledWith(1, { foo: 'bar' }); wrapper.instance().createOrgNotificationSuccess(1, { id: 2 }); expect(createOrganizationNotificationSuccess).toHaveBeenCalledWith(1, { id: 2 }); diff --git a/src/components/NotificationsList/Notifications.list.jsx b/src/components/NotificationsList/Notifications.list.jsx index 1fe385823d..1c53ec6f31 100644 --- a/src/components/NotificationsList/Notifications.list.jsx +++ b/src/components/NotificationsList/Notifications.list.jsx @@ -49,27 +49,21 @@ class Notifications extends Component { errorTemplateIds: [] }; - this.onSearch = this.onSearch.bind(this); + this.handleSearch = this.handleSearch.bind(this); this.getQueryParams = this.getQueryParams.bind(this); - this.onSort = this.onSort.bind(this); - this.onSetPage = this.onSetPage.bind(this); - this.onSelectAll = this.onSelectAll.bind(this); + this.handleSort = this.handleSort.bind(this); + this.handleSetPage = this.handleSetPage.bind(this); + this.handleSelectAll = this.handleSelectAll.bind(this); this.toggleNotification = this.toggleNotification.bind(this); this.updateUrl = this.updateUrl.bind(this); - this.postToError = this.postToError.bind(this); - this.postToSuccess = this.postToSuccess.bind(this); - this.fetchNotifications = this.fetchNotifications.bind(this); + this.createError = this.createError.bind(this); + this.createSuccess = this.createSuccess.bind(this); + this.readNotifications = this.readNotifications.bind(this); } componentDidMount () { const queryParams = this.getQueryParams(); - this.fetchNotifications(queryParams); - } - - onSearch () { - const { sortedColumnKey, sortOrder } = this.state; - - this.onSort(sortedColumnKey, sortOrder); + this.readNotifications(queryParams); } getQueryParams (overrides = {}) { @@ -81,7 +75,7 @@ class Notifications extends Component { return Object.assign({}, this.defaultParams, searchParams, overrides); } - onSort = (sortedColumnKey, sortOrder) => { + handleSort = (sortedColumnKey, sortOrder) => { const { page_size } = this.state; let order_by = sortedColumnKey; @@ -92,19 +86,19 @@ class Notifications extends Component { const queryParams = this.getQueryParams({ order_by, page_size }); - this.fetchNotifications(queryParams); + this.readNotifications(queryParams); }; - onSetPage = (pageNumber, pageSize) => { + handleSetPage = (pageNumber, pageSize) => { const page = parseInt(pageNumber, 10); const page_size = parseInt(pageSize, 10); const queryParams = this.getQueryParams({ page, page_size }); - this.fetchNotifications(queryParams); + this.readNotifications(queryParams); }; - onSelectAll = isSelected => { + handleSelectAll = isSelected => { const { results } = this.state; const selected = isSelected ? results.map(o => o.id) : []; @@ -114,12 +108,18 @@ class Notifications extends Component { toggleNotification = (id, isCurrentlyOn, status) => { if (status === 'success') { - this.postToSuccess(id, isCurrentlyOn); + this.createSuccess(id, isCurrentlyOn); } else if (status === 'error') { - this.postToError(id, isCurrentlyOn); + this.createError(id, isCurrentlyOn); } }; + handleSearch () { + const { sortedColumnKey, sortOrder } = this.state; + + this.handleSort(sortedColumnKey, sortOrder); + } + updateUrl (queryParams) { const { history, location, match } = this.props; const pathname = match.url; @@ -130,14 +130,14 @@ class Notifications extends Component { } } - async postToError (id, isCurrentlyOn) { - const { postError, match } = this.props; + async createError (id, isCurrentlyOn) { + const { onCreateError, match } = this.props; const postParams = { id }; if (isCurrentlyOn) { postParams.disassociate = true; } try { - await postError(match.params.id, postParams); + await onCreateError(match.params.id, postParams); } catch (err) { this.setState({ error: true }); } finally { @@ -155,14 +155,14 @@ class Notifications extends Component { } } - async postToSuccess (id, isCurrentlyOn) { - const { postSuccess, match } = this.props; + async createSuccess (id, isCurrentlyOn) { + const { onCreateSuccess, match } = this.props; const postParams = { id }; if (isCurrentlyOn) { postParams.disassociate = true; } try { - await postSuccess(match.params.id, postParams); + await onCreateSuccess(match.params.id, postParams); } catch (err) { this.setState({ error: true }); } finally { @@ -180,9 +180,9 @@ class Notifications extends Component { } } - async fetchNotifications (queryParams) { + async readNotifications (queryParams) { const { noInitialResults } = this.state; - const { getNotifications, getSuccess, getError, match } = this.props; + const { onReadNotifications, onReadSuccess, onReadError, match } = this.props; const { page, page_size, order_by } = queryParams; let sortOrder = 'ascending'; @@ -196,7 +196,7 @@ class Notifications extends Component { this.setState({ error: false, loading: true }); try { - const { data } = await getNotifications(match.params.id, queryParams); + const { data } = await onReadNotifications(match.params.id, queryParams); const { count, results } = data; const pageCount = Math.ceil(count / page_size); @@ -231,10 +231,10 @@ class Notifications extends Component { let errorTemplateIds = []; if (results.length > 0) { - const successTemplatesPromise = getSuccess(match.params.id, { + const successTemplatesPromise = onReadSuccess(match.params.id, { id__in: notificationTemplateIds }); - const errorTemplatesPromise = getError(match.params.id, { + const errorTemplatesPromise = onReadError(match.params.id, { id__in: notificationTemplateIds }); const successTemplatesResult = await successTemplatesPromise; @@ -297,9 +297,9 @@ class Notifications extends Component { sortedColumnKey={sortedColumnKey} sortOrder={sortOrder} columns={this.columns} - onSearch={this.onSearch} - onSort={this.onSort} - onSelectAll={this.onSelectAll} + onSearch={this.handleSearch} + onSort={this.handleSort} + onSelectAll={this.handleSelectAll} /> {({ i18n }) => ( @@ -324,7 +324,7 @@ class Notifications extends Component { page={page} pageCount={pageCount} page_size={page_size} - onSetPage={this.onSetPage} + onSetPage={this.handleSetPage} /> )} @@ -336,11 +336,11 @@ class Notifications extends Component { } Notifications.propTypes = { - getError: PropTypes.func.isRequired, - getNotifications: PropTypes.func.isRequired, - getSuccess: PropTypes.func.isRequired, - postError: PropTypes.func.isRequired, - postSuccess: PropTypes.func.isRequired, + onReadError: PropTypes.func.isRequired, + onReadNotifications: PropTypes.func.isRequired, + onReadSuccess: PropTypes.func.isRequired, + onCreateError: PropTypes.func.isRequired, + onCreateSuccess: PropTypes.func.isRequired, }; export default Notifications; diff --git a/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx b/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx index 7cac47139f..92ade9427e 100644 --- a/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx +++ b/src/pages/Organizations/screens/Organization/OrganizationNotifications.jsx @@ -6,24 +6,24 @@ class OrganizationNotifications extends Component { constructor (props) { super(props); - this.getOrgNotifications = this.getOrgNotifications.bind(this); - this.getOrgNotificationSuccess = this.getOrgNotificationSuccess.bind(this); - this.getOrgNotificationError = this.getOrgNotificationError.bind(this); + this.readOrgNotifications = this.readOrgNotifications.bind(this); + this.readOrgNotificationSuccess = this.readOrgNotificationSuccess.bind(this); + this.readOrgNotificationError = this.readOrgNotificationError.bind(this); this.createOrgNotificationSuccess = this.createOrgNotificationSuccess.bind(this); this.createOrgNotificationError = this.createOrgNotificationError.bind(this); } - getOrgNotifications (id, reqParams) { + readOrgNotifications (id, reqParams) { const { api } = this.props; return api.getOrganizationNotifications(id, reqParams); } - getOrgNotificationSuccess (id, reqParams) { + readOrgNotificationSuccess (id, reqParams) { const { api } = this.props; return api.getOrganizationNotificationSuccess(id, reqParams); } - getOrgNotificationError (id, reqParams) { + readOrgNotificationError (id, reqParams) { const { api } = this.props; return api.getOrganizationNotificationError(id, reqParams); } @@ -47,11 +47,11 @@ class OrganizationNotifications extends Component { return (