From 5f40ae1d4840cfee0ab4d3068696e868d4875a39 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 8 Jul 2021 17:10:01 -0500 Subject: [PATCH] Timelines: add tests --- .../reducers/__tests__/status_lists-test.js | 10 +- .../reducers/__tests__/timelines-test.js | 115 +++++++++++++++++- 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/app/soapbox/reducers/__tests__/status_lists-test.js b/app/soapbox/reducers/__tests__/status_lists-test.js index f24bc4d39..e3e6a1971 100644 --- a/app/soapbox/reducers/__tests__/status_lists-test.js +++ b/app/soapbox/reducers/__tests__/status_lists-test.js @@ -1,5 +1,5 @@ import reducer from '../status_lists'; -import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; +import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet } from 'immutable'; describe('status_lists reducer', () => { it('should return the initial state', () => { @@ -7,22 +7,22 @@ describe('status_lists reducer', () => { favourites: ImmutableMap({ next: null, loaded: false, - items: ImmutableList(), + items: ImmutableOrderedSet(), }), bookmarks: ImmutableMap({ next: null, loaded: false, - items: ImmutableList(), + items: ImmutableOrderedSet(), }), pins: ImmutableMap({ next: null, loaded: false, - items: ImmutableList(), + items: ImmutableOrderedSet(), }), scheduled_statuses: ImmutableMap({ next: null, loaded: false, - items: ImmutableList(), + items: ImmutableOrderedSet(), }), })); }); diff --git a/app/soapbox/reducers/__tests__/timelines-test.js b/app/soapbox/reducers/__tests__/timelines-test.js index 0bf748dc7..dfa5a62d4 100644 --- a/app/soapbox/reducers/__tests__/timelines-test.js +++ b/app/soapbox/reducers/__tests__/timelines-test.js @@ -1,8 +1,121 @@ import reducer from '../timelines'; -import { Map as ImmutableMap } from 'immutable'; +import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable'; +import { + TIMELINE_EXPAND_REQUEST, + TIMELINE_EXPAND_FAIL, + TIMELINE_EXPAND_SUCCESS, +} from 'soapbox/actions/timelines'; describe('timelines reducer', () => { it('should return the initial state', () => { expect(reducer(undefined, {})).toEqual(ImmutableMap()); }); + + describe('TIMELINE_EXPAND_REQUEST', () => { + it('sets loading to true', () => { + const action = { + type: TIMELINE_EXPAND_REQUEST, + timeline: 'home', + }; + + const result = reducer(undefined, action); + expect(result.getIn(['home', 'isLoading'])).toBe(true); + }); + }); + + describe('TIMELINE_EXPAND_FAIL', () => { + it('sets loading to false', () => { + const state = fromJS({ + home: { isLoading: true }, + }); + + const action = { + type: TIMELINE_EXPAND_FAIL, + timeline: 'home', + }; + + const result = reducer(state, action); + expect(result.getIn(['home', 'isLoading'])).toBe(false); + }); + }); + + describe('TIMELINE_EXPAND_SUCCESS', () => { + it('sets loading to false', () => { + const state = fromJS({ + home: { isLoading: true }, + }); + + const action = { + type: TIMELINE_EXPAND_SUCCESS, + timeline: 'home', + }; + + const result = reducer(state, action); + expect(result.getIn(['home', 'isLoading'])).toBe(false); + }); + + it('adds the status IDs', () => { + const expected = ImmutableOrderedSet(['1', '2', '5']); + + const action = { + type: TIMELINE_EXPAND_SUCCESS, + timeline: 'home', + statuses: [{ id: '1' }, { id: '2' }, { id: '5' }], + }; + + const result = reducer(undefined, action); + expect(result.getIn(['home', 'items'])).toEqual(expected); + }); + + it('merges new status IDs', () => { + const state = fromJS({ + home: { items: ImmutableOrderedSet(['5', '2', '1']) }, + }); + + const expected = ImmutableOrderedSet(['6', '5', '4', '2', '1']); + + const action = { + type: TIMELINE_EXPAND_SUCCESS, + timeline: 'home', + statuses: [{ id: '6' }, { id: '5' }, { id: '4' }], + }; + + const result = reducer(state, action); + expect(result.getIn(['home', 'items'])).toEqual(expected); + }); + + it('merges old status IDs', () => { + const state = fromJS({ + home: { items: ImmutableOrderedSet(['6', '4', '3']) }, + }); + + const expected = ImmutableOrderedSet(['6', '4', '3', '5', '2', '1']); + + const action = { + type: TIMELINE_EXPAND_SUCCESS, + timeline: 'home', + statuses: [{ id: '5' }, { id: '2' }, { id: '1' }], + }; + + const result = reducer(state, action); + expect(result.getIn(['home', 'items'])).toEqual(expected); + }); + + it('overrides pinned post IDs', () => { + const state = fromJS({ + 'account:1:pinned': { items: ImmutableOrderedSet(['5', '2', '1']) }, + }); + + const expected = ImmutableOrderedSet(['9', '8', '7']); + + const action = { + type: TIMELINE_EXPAND_SUCCESS, + timeline: 'home', + statuses: [{ id: '9' }, { id: '8' }, { id: '7' }], + }; + + const result = reducer(state, action); + expect(result.getIn(['home', 'items'])).toEqual(expected); + }); + }); });