diff --git a/src/newsreader/js/pages/homepage/reducers/posts.js b/src/newsreader/js/pages/homepage/reducers/posts.js index cdd44fa..2c1ff86 100644 --- a/src/newsreader/js/pages/homepage/reducers/posts.js +++ b/src/newsreader/js/pages/homepage/reducers/posts.js @@ -61,10 +61,11 @@ export const posts = (state = { ...defaultState }, action) => { case MARK_SECTION_READ: let posts = []; - // TODO: test these cases switch (action.section.type) { case CATEGORY_TYPE: - posts = [...state.items].forEach(post => { + posts = [...state.items]; + + posts.forEach(post => { if (!(post.rule.id in { ...action.section.rules })) return; post.read = true; @@ -72,7 +73,9 @@ export const posts = (state = { ...defaultState }, action) => { break; case RULE_TYPE: - posts = [...state.items].forEach(post => { + posts = [...state.items]; + + posts.forEach(post => { if (post.rule.id != action.section.id) return; post.read = true; diff --git a/src/newsreader/js/tests/homepage/reducers/post.test.js b/src/newsreader/js/tests/homepage/reducers/post.test.js index adb8983..249a9f1 100644 --- a/src/newsreader/js/tests/homepage/reducers/post.test.js +++ b/src/newsreader/js/tests/homepage/reducers/post.test.js @@ -6,7 +6,7 @@ import * as actions from '../../../pages/homepage/actions/posts.js'; import * as selectedActions from '../../../pages/homepage/actions/selected.js'; import * as constants from '../../../pages/homepage/constants.js'; -const defaultState = { items: {}, isFetching: false }; +const defaultState = { items: [], isFetching: false }; describe('post actions', () => { it('should return state after requesting posts', () => { @@ -42,7 +42,7 @@ describe('post actions', () => { ...defaultState, isFetching: false, isUpdating: false, - items: { [post.id]: post }, + items: [post], }; expect(reducer(undefined, action)).toEqual(expectedState); @@ -85,12 +85,11 @@ describe('post actions', () => { posts, }; - const expectedPosts = objectsFromArray(posts, 'id'); const expectedState = { ...defaultState, isFetching: false, isUpdating: false, - items: expectedPosts, + items: posts, }; expect(reducer(undefined, action)).toEqual(expectedState); @@ -108,7 +107,14 @@ describe('post actions', () => { author: 'Kyle Orland', publicationDate: '2020-01-24T19:50:12Z', url: 'https://arstechnica.com/?p=1648607', - rule: 5, + rule: { + id: 5, + name: 'Ars Technica', + url: 'http://feeds.arstechnica.com/arstechnica/index?fmt=xml', + favicon: 'https://cdn.arstechnica.net/favicon.ico', + category: 9, + unread: 544, + }, read: false, }, 2141: { @@ -120,7 +126,14 @@ describe('post actions', () => { author: 'WIRED', publicationDate: '2020-01-25T11:06:46Z', url: 'https://arstechnica.com/?p=1648757', - rule: 5, + rule: { + id: 5, + name: 'Ars Technica', + url: 'http://feeds.arstechnica.com/arstechnica/index?fmt=xml', + favicon: 'https://cdn.arstechnica.net/favicon.ico', + category: 9, + unread: 544, + }, read: false, }, 4637: { @@ -132,7 +145,9 @@ describe('post actions', () => { author: null, publicationDate: '2020-01-29T19:08:25Z', url: 'https://www.bbc.co.uk/news/world-asia-china-51299195', - rule: 4, + rule: { + id: 4, + }, read: false, saved: false, }, @@ -145,7 +160,9 @@ describe('post actions', () => { author: null, publicationDate: '2020-01-29T18:27:56Z', url: 'https://www.bbc.co.uk/news/world-europe-51294305', - rule: 4, + rule: { + id: 4, + }, read: false, saved: false, }, @@ -165,16 +182,17 @@ describe('post actions', () => { section: { ...rule, type: constants.RULE_TYPE }, }; - const state = { ...defaultState, items: { ...posts } }; + const state = { ...defaultState, items: Object.values(posts) }; const expectedState = { ...defaultState, isFetching: false, - items: { - ...posts, - 2067: { ...posts[2067], read: true }, - 2141: { ...posts[2141], read: true }, - }, + items: [ + { ...posts[2067], read: true }, + { ...posts[2141], read: true }, + { ...posts[4637] }, + { ...posts[4638] }, + ], }; expect(reducer(state, action)).toEqual(expectedState); @@ -192,7 +210,15 @@ describe('post actions', () => { author: 'Kyle Orland', publicationDate: '2020-01-24T19:50:12Z', url: 'https://arstechnica.com/?p=1648607', - rule: 5, + rule: { + id: 5, + name: 'BBC', + url: 'http://feeds.bbci.co.uk/news/world/rss.xml', + favicon: + 'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png', + category: 8, + unread: 632, + }, read: false, saved: false, }, @@ -205,7 +231,15 @@ describe('post actions', () => { author: 'WIRED', publicationDate: '2020-01-25T11:06:46Z', url: 'https://arstechnica.com/?p=1648757', - rule: 5, + rule: { + id: 5, + name: 'BBC', + url: 'http://feeds.bbci.co.uk/news/world/rss.xml', + favicon: + 'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png', + category: 8, + unread: 632, + }, read: false, saved: false, }, @@ -218,7 +252,15 @@ describe('post actions', () => { author: null, publicationDate: '2020-01-29T19:08:25Z', url: 'https://www.bbc.co.uk/news/world-asia-china-51299195', - rule: 4, + rule: { + id: 4, + name: 'BBC', + url: 'http://feeds.bbci.co.uk/news/world/rss.xml', + favicon: + 'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png', + category: 8, + unread: 321, + }, read: false, saved: false, }, @@ -231,7 +273,15 @@ describe('post actions', () => { author: null, publicationDate: '2020-01-29T18:27:56Z', url: 'https://www.bbc.co.uk/news/world-europe-51294305', - rule: 4, + rule: { + id: 4, + name: 'BBC', + url: 'http://feeds.bbci.co.uk/news/world/rss.xml', + favicon: + 'https://m.files.bbci.co.uk/modules/bbc-morph-news-waf-page-meta/2.5.2/apple-touch-icon-57x57-precomposed.png', + category: 8, + unread: 321, + }, read: false, saved: false, }, @@ -245,7 +295,9 @@ describe('post actions', () => { publicationDate: '2020-01-29T19:03:01Z', url: 'https://tweakers.net/nieuws/162878/analyse-nintendo-verdiende-miljard-dollar-aan-mobiele-games.html', - rule: 7, + rule: { + id: 7, + }, read: false, saved: false, }, @@ -259,7 +311,9 @@ describe('post actions', () => { publicationDate: '2020-01-29T16:29:40Z', url: 'https://tweakers.net/nieuws/162870/samsung-kondigt-eerste-tablet-met-5g-aan.html', - rule: 7, + rule: { + id: 7, + }, read: false, saved: false, }, @@ -276,7 +330,7 @@ describe('post actions', () => { unread: 321, }, 5: { - id: 4, + id: 5, name: 'BBC', url: 'http://feeds.bbci.co.uk/news/world/rss.xml', favicon: @@ -301,18 +355,19 @@ describe('post actions', () => { }, }; - const state = { ...defaultState, items: { ...posts } }; + const state = { ...defaultState, items: Object.values(posts) }; const expectedState = { ...defaultState, isFetching: false, - items: { - ...posts, - 2067: { ...posts[2067], read: true }, - 2141: { ...posts[2141], read: true }, - 4637: { ...posts[4637], read: true }, - 4638: { ...posts[4638], read: true }, - }, + items: [ + { ...posts[2067], read: true }, + { ...posts[2141], read: true }, + { ...posts[4589] }, + { ...posts[4594] }, + { ...posts[4637], read: true }, + { ...posts[4638], read: true }, + ], }; expect(reducer(state, action)).toEqual(expectedState);