bigbuffet-rw/app/soapbox/reducers/__tests__/timelines.test.ts

124 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-07-08 15:10:01 -07:00
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
2021-07-08 15:10:01 -07:00
import {
TIMELINE_EXPAND_REQUEST,
TIMELINE_EXPAND_FAIL,
TIMELINE_EXPAND_SUCCESS,
} from 'soapbox/actions/timelines';
2022-01-10 14:01:24 -08:00
import reducer from '../timelines';
2020-06-09 18:08:07 -07:00
describe('timelines reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(ImmutableMap());
});
2021-07-08 15:10:01 -07:00
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 = ImmutableMap(fromJS({
2021-07-08 15:10:01 -07:00
home: { isLoading: true },
}));
2021-07-08 15:10:01 -07:00
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 = ImmutableMap(fromJS({
2021-07-08 15:10:01 -07:00
home: { isLoading: true },
}));
2021-07-08 15:10:01 -07:00
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 = ImmutableMap(fromJS({
2021-07-08 15:10:01 -07:00
home: { items: ImmutableOrderedSet(['5', '2', '1']) },
}));
2021-07-08 15:10:01 -07:00
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 = ImmutableMap(fromJS({
2021-07-08 15:10:01 -07:00
home: { items: ImmutableOrderedSet(['6', '4', '3']) },
}));
2021-07-08 15:10:01 -07:00
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 = ImmutableMap(fromJS({
2021-07-08 15:10:01 -07:00
'account:1:pinned': { items: ImmutableOrderedSet(['5', '2', '1']) },
}));
2021-07-08 15:10:01 -07:00
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);
});
});
2020-06-09 18:08:07 -07:00
});