Update post reducer & tests

This commit is contained in:
Sonny Bakker 2023-09-27 22:43:12 +02:00
parent c12ba968b8
commit 3de915bb64
2 changed files with 90 additions and 32 deletions

View file

@ -61,10 +61,11 @@ export const posts = (state = { ...defaultState }, action) => {
case MARK_SECTION_READ: case MARK_SECTION_READ:
let posts = []; let posts = [];
// TODO: test these cases
switch (action.section.type) { switch (action.section.type) {
case CATEGORY_TYPE: case CATEGORY_TYPE:
posts = [...state.items].forEach(post => { posts = [...state.items];
posts.forEach(post => {
if (!(post.rule.id in { ...action.section.rules })) return; if (!(post.rule.id in { ...action.section.rules })) return;
post.read = true; post.read = true;
@ -72,7 +73,9 @@ export const posts = (state = { ...defaultState }, action) => {
break; break;
case RULE_TYPE: case RULE_TYPE:
posts = [...state.items].forEach(post => { posts = [...state.items];
posts.forEach(post => {
if (post.rule.id != action.section.id) return; if (post.rule.id != action.section.id) return;
post.read = true; post.read = true;

View file

@ -6,7 +6,7 @@ import * as actions from '../../../pages/homepage/actions/posts.js';
import * as selectedActions from '../../../pages/homepage/actions/selected.js'; import * as selectedActions from '../../../pages/homepage/actions/selected.js';
import * as constants from '../../../pages/homepage/constants.js'; import * as constants from '../../../pages/homepage/constants.js';
const defaultState = { items: {}, isFetching: false }; const defaultState = { items: [], isFetching: false };
describe('post actions', () => { describe('post actions', () => {
it('should return state after requesting posts', () => { it('should return state after requesting posts', () => {
@ -42,7 +42,7 @@ describe('post actions', () => {
...defaultState, ...defaultState,
isFetching: false, isFetching: false,
isUpdating: false, isUpdating: false,
items: { [post.id]: post }, items: [post],
}; };
expect(reducer(undefined, action)).toEqual(expectedState); expect(reducer(undefined, action)).toEqual(expectedState);
@ -85,12 +85,11 @@ describe('post actions', () => {
posts, posts,
}; };
const expectedPosts = objectsFromArray(posts, 'id');
const expectedState = { const expectedState = {
...defaultState, ...defaultState,
isFetching: false, isFetching: false,
isUpdating: false, isUpdating: false,
items: expectedPosts, items: posts,
}; };
expect(reducer(undefined, action)).toEqual(expectedState); expect(reducer(undefined, action)).toEqual(expectedState);
@ -108,7 +107,14 @@ describe('post actions', () => {
author: 'Kyle Orland', author: 'Kyle Orland',
publicationDate: '2020-01-24T19:50:12Z', publicationDate: '2020-01-24T19:50:12Z',
url: 'https://arstechnica.com/?p=1648607', 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, read: false,
}, },
2141: { 2141: {
@ -120,7 +126,14 @@ describe('post actions', () => {
author: 'WIRED', author: 'WIRED',
publicationDate: '2020-01-25T11:06:46Z', publicationDate: '2020-01-25T11:06:46Z',
url: 'https://arstechnica.com/?p=1648757', 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, read: false,
}, },
4637: { 4637: {
@ -132,7 +145,9 @@ describe('post actions', () => {
author: null, author: null,
publicationDate: '2020-01-29T19:08:25Z', publicationDate: '2020-01-29T19:08:25Z',
url: 'https://www.bbc.co.uk/news/world-asia-china-51299195', url: 'https://www.bbc.co.uk/news/world-asia-china-51299195',
rule: 4, rule: {
id: 4,
},
read: false, read: false,
saved: false, saved: false,
}, },
@ -145,7 +160,9 @@ describe('post actions', () => {
author: null, author: null,
publicationDate: '2020-01-29T18:27:56Z', publicationDate: '2020-01-29T18:27:56Z',
url: 'https://www.bbc.co.uk/news/world-europe-51294305', url: 'https://www.bbc.co.uk/news/world-europe-51294305',
rule: 4, rule: {
id: 4,
},
read: false, read: false,
saved: false, saved: false,
}, },
@ -165,16 +182,17 @@ describe('post actions', () => {
section: { ...rule, type: constants.RULE_TYPE }, section: { ...rule, type: constants.RULE_TYPE },
}; };
const state = { ...defaultState, items: { ...posts } }; const state = { ...defaultState, items: Object.values(posts) };
const expectedState = { const expectedState = {
...defaultState, ...defaultState,
isFetching: false, isFetching: false,
items: { items: [
...posts, { ...posts[2067], read: true },
2067: { ...posts[2067], read: true }, { ...posts[2141], read: true },
2141: { ...posts[2141], read: true }, { ...posts[4637] },
}, { ...posts[4638] },
],
}; };
expect(reducer(state, action)).toEqual(expectedState); expect(reducer(state, action)).toEqual(expectedState);
@ -192,7 +210,15 @@ describe('post actions', () => {
author: 'Kyle Orland', author: 'Kyle Orland',
publicationDate: '2020-01-24T19:50:12Z', publicationDate: '2020-01-24T19:50:12Z',
url: 'https://arstechnica.com/?p=1648607', 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, read: false,
saved: false, saved: false,
}, },
@ -205,7 +231,15 @@ describe('post actions', () => {
author: 'WIRED', author: 'WIRED',
publicationDate: '2020-01-25T11:06:46Z', publicationDate: '2020-01-25T11:06:46Z',
url: 'https://arstechnica.com/?p=1648757', 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, read: false,
saved: false, saved: false,
}, },
@ -218,7 +252,15 @@ describe('post actions', () => {
author: null, author: null,
publicationDate: '2020-01-29T19:08:25Z', publicationDate: '2020-01-29T19:08:25Z',
url: 'https://www.bbc.co.uk/news/world-asia-china-51299195', 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, read: false,
saved: false, saved: false,
}, },
@ -231,7 +273,15 @@ describe('post actions', () => {
author: null, author: null,
publicationDate: '2020-01-29T18:27:56Z', publicationDate: '2020-01-29T18:27:56Z',
url: 'https://www.bbc.co.uk/news/world-europe-51294305', 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, read: false,
saved: false, saved: false,
}, },
@ -245,7 +295,9 @@ describe('post actions', () => {
publicationDate: '2020-01-29T19:03:01Z', publicationDate: '2020-01-29T19:03:01Z',
url: url:
'https://tweakers.net/nieuws/162878/analyse-nintendo-verdiende-miljard-dollar-aan-mobiele-games.html', 'https://tweakers.net/nieuws/162878/analyse-nintendo-verdiende-miljard-dollar-aan-mobiele-games.html',
rule: 7, rule: {
id: 7,
},
read: false, read: false,
saved: false, saved: false,
}, },
@ -259,7 +311,9 @@ describe('post actions', () => {
publicationDate: '2020-01-29T16:29:40Z', publicationDate: '2020-01-29T16:29:40Z',
url: url:
'https://tweakers.net/nieuws/162870/samsung-kondigt-eerste-tablet-met-5g-aan.html', 'https://tweakers.net/nieuws/162870/samsung-kondigt-eerste-tablet-met-5g-aan.html',
rule: 7, rule: {
id: 7,
},
read: false, read: false,
saved: false, saved: false,
}, },
@ -276,7 +330,7 @@ describe('post actions', () => {
unread: 321, unread: 321,
}, },
5: { 5: {
id: 4, id: 5,
name: 'BBC', name: 'BBC',
url: 'http://feeds.bbci.co.uk/news/world/rss.xml', url: 'http://feeds.bbci.co.uk/news/world/rss.xml',
favicon: favicon:
@ -301,18 +355,19 @@ describe('post actions', () => {
}, },
}; };
const state = { ...defaultState, items: { ...posts } }; const state = { ...defaultState, items: Object.values(posts) };
const expectedState = { const expectedState = {
...defaultState, ...defaultState,
isFetching: false, isFetching: false,
items: { items: [
...posts, { ...posts[2067], read: true },
2067: { ...posts[2067], read: true }, { ...posts[2141], read: true },
2141: { ...posts[2141], read: true }, { ...posts[4589] },
4637: { ...posts[4637], read: true }, { ...posts[4594] },
4638: { ...posts[4638], read: true }, { ...posts[4637], read: true },
}, { ...posts[4638], read: true },
],
}; };
expect(reducer(state, action)).toEqual(expectedState); expect(reducer(state, action)).toEqual(expectedState);