From 0bc27a5ff61e02de66552bce73ca07f80a2d1417 Mon Sep 17 00:00:00 2001 From: Sonny Bakker Date: Sat, 27 Feb 2021 15:45:33 +0100 Subject: [PATCH] Add new tests --- .../js/pages/homepage/actions/posts.js | 1 - .../js/tests/homepage/actions/post.test.js | 80 +++++++++++++++++-- .../tests/homepage/actions/selected.test.js | 8 ++ .../tests/homepage/reducers/selected.test.js | 13 +++ 4 files changed, 95 insertions(+), 7 deletions(-) diff --git a/src/newsreader/js/pages/homepage/actions/posts.js b/src/newsreader/js/pages/homepage/actions/posts.js index 49858fa..6a0cd7a 100644 --- a/src/newsreader/js/pages/homepage/actions/posts.js +++ b/src/newsreader/js/pages/homepage/actions/posts.js @@ -96,7 +96,6 @@ export const toggleSaved = (post, token) => { }; }; -// TODO add tests export const fetchSavedPosts = (next = false) => { return dispatch => { dispatch(requestPosts()); diff --git a/src/newsreader/js/tests/homepage/actions/post.test.js b/src/newsreader/js/tests/homepage/actions/post.test.js index 9d78aab..d30e549 100644 --- a/src/newsreader/js/tests/homepage/actions/post.test.js +++ b/src/newsreader/js/tests/homepage/actions/post.test.js @@ -26,6 +26,12 @@ describe('post actions', () => { expect(actions.markingPostRead()).toEqual(expectedAction); }); + it('should create an action to toggle post saved state', () => { + const expectedAction = { type: actions.TOGGLING_POST }; + + expect(actions.togglingPost()).toEqual(expectedAction); + }); + it('should create an action receive a post', () => { const post = { id: 2067, @@ -131,10 +137,11 @@ describe('post actions', () => { }; const expectedAction = { - type: actions.TOGGLING_POST, + type: actions.TOGGLED_POST, + post, }; - expect(actions.togglingPost(post)).toEqual(expectedAction); + expect(actions.postToggled(post)).toEqual(expectedAction); }); it('should create multiple actions to mark post read', () => { @@ -300,7 +307,7 @@ describe('post actions', () => { fetchMock.getOnce('/api/rules/4/posts/?read=false', { body: { count: 2, - next: 'https://durp.com/api/rules/4/posts/?page=2&read=false', + next: 'https://durp.com/api/rules/4/posts/?cursor=jabadabar&read=false', previous: null, results: posts, }, @@ -318,7 +325,7 @@ describe('post actions', () => { { type: actions.REQUEST_POSTS }, { type: actions.RECEIVE_POSTS, - next: 'https://durp.com/api/rules/4/posts/?page=2&read=false', + next: 'https://durp.com/api/rules/4/posts/?cursor=jabadabar&read=false', posts, }, ]; @@ -369,7 +376,7 @@ describe('post actions', () => { fetchMock.getOnce('/api/categories/1/posts/?read=false', { body: { count: 2, - next: 'https://durp.com/api/categories/4/posts/?page=2&read=false', + next: 'https://durp.com/api/categories/4/posts/?cursor=jabadabar&read=false', previous: null, results: posts, }, @@ -387,7 +394,7 @@ describe('post actions', () => { { type: actions.REQUEST_POSTS }, { type: actions.RECEIVE_POSTS, - next: 'https://durp.com/api/categories/4/posts/?page=2&read=false', + next: 'https://durp.com/api/categories/4/posts/?cursor=jabadabar&read=false', posts, }, ]; @@ -397,6 +404,67 @@ describe('post actions', () => { }); }); + it('should create multiple actions to fetch posts by saved state', () => { + const posts = [ + { + id: 2067, + remoteIdentifier: 'https://arstechnica.com/?p=1648607', + title: + 'This amazing glitch puts Star Fox 64 ships in an unmodified Zelda cartridge', + body: + '"Stale-reference manipulation," 300-character file names, and a clash between worlds.', + author: 'Kyle Orland', + publicationDate: '2020-01-24T19:50:12Z', + url: 'https://arstechnica.com/?p=1648607', + rule: 4, + read: false, + saved: true, + }, + { + id: 2141, + remoteIdentifier: 'https://arstechnica.com/?p=1648757', + title: 'The most complete brain map ever is here: A fly’s “connectome”', + body: + 'It took 12 years and at least $40 million to chart a region about 250µm across.', + author: 'WIRED', + publicationDate: '2020-01-25T11:06:46Z', + url: 'https://arstechnica.com/?p=1648757', + rule: 4, + read: false, + saved: true, + }, + ]; + + fetchMock.getOnce('/api/posts/?saved=true', { + body: { + next: 'https://durp.com/api/posts/?cursor=jabadabar&saved=true', + previous: null, + results: posts, + }, + headers: { 'content-type': 'application/json' }, + }); + + const store = mockStore({ + categories: { items: {}, isFetching: false }, + rules: { items: {}, isFetching: false }, + posts: { items: {}, isFetching: false, isUpdating: false }, + selected: { item: {}, next: false, lastReached: false, post: {} }, + }); + + const expectedActions = [ + { type: actions.REQUEST_POSTS }, + { + type: actions.RECEIVE_POSTS, + next: 'https://durp.com/api/posts/?cursor=jabadabar&saved=true', + posts, + }, + ]; + + return store.dispatch(actions.fetchSavedPosts()).then(() => { + expect(store.getActions()).toEqual(expectedActions); + }); + }); + it('should create no actions when fetching posts and section is read', () => { const rule = { id: 4, diff --git a/src/newsreader/js/tests/homepage/actions/selected.test.js b/src/newsreader/js/tests/homepage/actions/selected.test.js index b0f163c..cac7509 100644 --- a/src/newsreader/js/tests/homepage/actions/selected.test.js +++ b/src/newsreader/js/tests/homepage/actions/selected.test.js @@ -32,6 +32,14 @@ describe('selected actions', () => { expect(actions.markSectionRead(category)).toEqual(expectedAction); }); + it('should create an action to select saved items', () => { + const expectedAction = { + type: actions.SELECT_SAVED, + }; + + expect(actions.selectSaved()).toEqual(expectedAction); + }); + it('should mark a category as read', () => { const category = { id: 1, name: 'Test category', unread: 100 }; const rules = { diff --git a/src/newsreader/js/tests/homepage/reducers/selected.test.js b/src/newsreader/js/tests/homepage/reducers/selected.test.js index 215c6e1..40561a3 100644 --- a/src/newsreader/js/tests/homepage/reducers/selected.test.js +++ b/src/newsreader/js/tests/homepage/reducers/selected.test.js @@ -52,6 +52,19 @@ describe('selected reducer', () => { expect(reducer(undefined, action)).toEqual(expectedState); }); + it('should return state after selecting saved items', () => { + const action = { + type: actions.SELECT_SAVED, + }; + + const expectedState = { + ...defaultState, + item: { type: constants.SAVED_TYPE }, + }; + + expect(reducer(undefined, action)).toEqual(expectedState); + }); + it('should return state after selecting a category twice', () => { const category = { id: 9, name: 'Tech', unread: 291 };