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

119 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-04-21 15:35:01 -07:00
import {
Map as ImmutableMap,
OrderedSet as ImmutableOrderedSet,
fromJS,
is,
2021-04-21 15:35:01 -07:00
} from 'immutable';
import { STATUS_IMPORT } from 'soapbox/actions/importer';
import { CONTEXT_FETCH_SUCCESS } from 'soapbox/actions/statuses';
import { TIMELINE_DELETE } from 'soapbox/actions/timelines';
2022-05-13 13:08:38 -07:00
import { applyActions } from 'soapbox/jest/test-helpers';
2022-05-13 12:42:15 -07:00
import reducer, { ReducerRecord } from '../contexts';
2020-06-09 18:08:07 -07:00
describe('contexts reducer', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {} as any)).toEqual(ReducerRecord({
2020-06-09 18:08:07 -07:00
inReplyTos: ImmutableMap(),
replies: ImmutableMap(),
}));
});
2020-09-18 15:03:00 -07:00
describe(CONTEXT_FETCH_SUCCESS, () => {
it('inserts a tombstone connecting an orphaned descendant', () => {
const status = { id: 'A', in_reply_to_id: null };
2022-05-13 13:08:38 -07:00
const context = {
id: 'A',
ancestors: [],
descendants: [
{ id: 'C', in_reply_to_id: 'B' },
],
};
2020-09-18 15:03:00 -07:00
const actions = [
{ type: STATUS_IMPORT, status },
{ type: CONTEXT_FETCH_SUCCESS, ...context },
];
const result = applyActions(undefined, actions, reducer);
expect(result.inReplyTos.get('C')).toBe('C-tombstone');
expect(result.replies.get('A').toArray()).toEqual(['C-tombstone']);
});
it('inserts a tombstone connecting an orphaned descendant (with null in_reply_to_id)', () => {
const status = { id: 'A', in_reply_to_id: null };
const context = {
id: 'A',
ancestors: [],
descendants: [
{ id: 'C', in_reply_to_id: null },
],
};
const actions = [
{ type: STATUS_IMPORT, status },
{ type: CONTEXT_FETCH_SUCCESS, ...context },
];
const result = applyActions(undefined, actions, reducer);
expect(result.inReplyTos.get('C')).toBe('C-tombstone');
expect(result.replies.get('A').toArray()).toEqual(['C-tombstone']);
});
2022-05-13 15:11:31 -07:00
it('doesn\'t explode when it encounters a loop', () => {
const status = { id: 'A', in_reply_to_id: null };
const context = {
id: 'A',
ancestors: [],
descendants: [
{ id: 'C', in_reply_to_id: 'E' },
{ id: 'D', in_reply_to_id: 'C' },
{ id: 'E', in_reply_to_id: 'D' },
{ id: 'F', in_reply_to_id: 'F' },
],
};
const actions = [
{ type: STATUS_IMPORT, status },
{ type: CONTEXT_FETCH_SUCCESS, ...context },
];
const result = applyActions(undefined, actions, reducer);
// These checks are superficial. We just don't want a stack overflow!
expect(result.inReplyTos.get('C')).toBe('C-tombstone');
expect(result.replies.get('A').toArray()).toEqual(['C-tombstone', 'F-tombstone']);
});
2020-09-18 15:03:00 -07:00
});
2021-04-21 15:35:01 -07:00
describe(TIMELINE_DELETE, () => {
it('deletes the status', () => {
const action = { type: TIMELINE_DELETE, id: 'B' };
2022-05-13 12:42:15 -07:00
const state = ReducerRecord({
inReplyTos: fromJS({
2021-04-21 15:35:01 -07:00
B: 'A',
C: 'B',
}) as ImmutableMap<string, string>,
2022-05-13 12:42:15 -07:00
replies: fromJS({
2021-04-21 15:35:01 -07:00
A: ImmutableOrderedSet(['B']),
B: ImmutableOrderedSet(['C']),
}) as ImmutableMap<string, ImmutableOrderedSet<string>>,
2021-04-21 15:35:01 -07:00
});
2022-05-13 12:42:15 -07:00
const expected = ReducerRecord({
inReplyTos: fromJS({}) as ImmutableMap<string, string>,
2022-05-13 12:42:15 -07:00
replies: fromJS({
2021-04-21 15:35:01 -07:00
A: ImmutableOrderedSet(),
}) as ImmutableMap<string, ImmutableOrderedSet<string>>,
2021-04-21 15:35:01 -07:00
});
expect(is(reducer(state, action), expected)).toBe(true);
2021-04-21 15:35:01 -07:00
});
});
2020-06-09 18:08:07 -07:00
});