Reducers: TypeScript

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2022-06-04 09:22:36 +02:00
parent 666c2dd0ce
commit 65dff4e6a1
50 changed files with 473 additions and 370 deletions

View File

@@ -1,4 +1,6 @@
import { fromJS } from 'immutable';
import { List as ImmutableList, Map as ImmutableMap, fromJS } from 'immutable';
import { normalizeStatus } from 'soapbox/normalizers';
import {
sortEmoji,
@@ -11,7 +13,7 @@ import {
simulateUnEmojiReact,
} from '../emoji_reacts';
const ALLOWED_EMOJI = fromJS([
const ALLOWED_EMOJI = ImmutableList([
'👍',
'❤',
'😂',
@@ -30,7 +32,7 @@ describe('filterEmoji', () => {
{ 'count': 1, 'me': true, 'name': '😡' },
{ 'count': 1, 'me': true, 'name': '🔪' },
{ 'count': 1, 'me': true, 'name': '😠' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
it('filters only allowed emoji', () => {
expect(filterEmoji(emojiReacts, ALLOWED_EMOJI)).toEqual(fromJS([
{ 'count': 1, 'me': true, 'name': '😂' },
@@ -49,7 +51,7 @@ describe('sortEmoji', () => {
{ 'count': 20, 'me': true, 'name': '👍' },
{ 'count': 7, 'me': true, 'name': '😂' },
{ 'count': 15, 'me': true, 'name': '❤' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
it('sorts the emoji by count', () => {
expect(sortEmoji(emojiReacts)).toEqual(fromJS([
{ 'count': 20, 'me': true, 'name': '👍' },
@@ -72,7 +74,7 @@ describe('mergeEmojiFavourites', () => {
{ 'count': 20, 'me': false, 'name': '👍' },
{ 'count': 15, 'me': false, 'name': '❤' },
{ 'count': 7, 'me': false, 'name': '😯' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
it('combines 👍 reacts with favourites', () => {
expect(mergeEmojiFavourites(emojiReacts, favouritesCount, favourited)).toEqual(fromJS([
{ 'count': 32, 'me': true, 'name': '👍' },
@@ -86,7 +88,7 @@ describe('mergeEmojiFavourites', () => {
const emojiReacts = fromJS([
{ 'count': 15, 'me': false, 'name': '❤' },
{ 'count': 7, 'me': false, 'name': '😯' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
it('adds 👍 reacts to the map equaling favourite count', () => {
expect(mergeEmojiFavourites(emojiReacts, favouritesCount, favourited)).toEqual(fromJS([
{ 'count': 15, 'me': false, 'name': '❤' },
@@ -116,7 +118,7 @@ describe('reduceEmoji', () => {
{ 'count': 15, 'me': true, 'name': '❤' },
{ 'count': 1, 'me': false, 'name': '👀' },
{ 'count': 1, 'me': false, 'name': '🍩' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
it('sorts, filters, and combines emoji and favourites', () => {
expect(reduceEmoji(emojiReacts, 7, true, ALLOWED_EMOJI)).toEqual(fromJS([
{ 'count': 27, 'me': true, 'name': '👍' },
@@ -138,7 +140,7 @@ describe('oneEmojiPerAccount', () => {
{ 'count': 2, 'me': true, 'name': '❤', accounts: [{ id: '1' }, { id: '2' }] },
{ 'count': 1, 'me': true, 'name': '😯', accounts: [{ id: '1' }] },
{ 'count': 1, 'me': false, 'name': '😂', accounts: [{ id: '3' }] },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
expect(oneEmojiPerAccount(emojiReacts, '1')).toEqual(fromJS([
{ 'count': 2, 'me': true, 'name': '👍', accounts: [{ id: '1' }, { id: '2' }] },
{ 'count': 1, 'me': false, 'name': '😂', accounts: [{ id: '3' }] },
@@ -148,7 +150,7 @@ describe('oneEmojiPerAccount', () => {
describe('getReactForStatus', () => {
it('returns a single owned react (including favourite) for the status', () => {
const status = fromJS({
const status = normalizeStatus(fromJS({
favourited: false,
pleroma: {
emoji_reactions: [
@@ -158,27 +160,27 @@ describe('getReactForStatus', () => {
{ 'count': 7, 'me': false, 'name': '😂' },
],
},
});
}));
expect(getReactForStatus(status, ALLOWED_EMOJI)).toEqual('❤');
});
it('returns a thumbs-up for a favourite', () => {
const status = fromJS({ favourites_count: 1, favourited: true });
const status = normalizeStatus(fromJS({ favourites_count: 1, favourited: true }));
expect(getReactForStatus(status)).toEqual('👍');
});
it('returns undefined when a status has no reacts (or favourites)', () => {
const status = fromJS({});
const status = normalizeStatus(fromJS({}));
expect(getReactForStatus(status)).toEqual(undefined);
});
it('returns undefined when a status has no valid reacts (or favourites)', () => {
const status = fromJS([
const status = normalizeStatus(fromJS([
{ 'count': 1, 'me': true, 'name': '🔪' },
{ 'count': 1, 'me': true, 'name': '🌵' },
{ 'count': 1, 'me': false, 'name': '👀' },
{ 'count': 1, 'me': false, 'name': '🍩' },
]);
]));
expect(getReactForStatus(status)).toEqual(undefined);
});
});
@@ -188,7 +190,7 @@ describe('simulateEmojiReact', () => {
const emojiReacts = fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
expect(simulateEmojiReact(emojiReacts, '❤')).toEqual(fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 3, 'me': true, 'name': '❤' },
@@ -199,7 +201,7 @@ describe('simulateEmojiReact', () => {
const emojiReacts = fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
expect(simulateEmojiReact(emojiReacts, '😯')).toEqual(fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
@@ -213,7 +215,7 @@ describe('simulateUnEmojiReact', () => {
const emojiReacts = fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 3, 'me': true, 'name': '❤' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
expect(simulateUnEmojiReact(emojiReacts, '❤')).toEqual(fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
@@ -225,7 +227,7 @@ describe('simulateUnEmojiReact', () => {
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },
{ 'count': 1, 'me': true, 'name': '😯' },
]);
]) as ImmutableList<ImmutableMap<string, any>>;
expect(simulateUnEmojiReact(emojiReacts, '😯')).toEqual(fromJS([
{ 'count': 2, 'me': false, 'name': '👍' },
{ 'count': 2, 'me': false, 'name': '❤' },

View File

@@ -1,71 +1,73 @@
import { fromJS } from 'immutable';
import { normalizeStatus } from 'soapbox/normalizers/status';
import { shouldFilter } from '../timelines';
describe('shouldFilter', () => {
it('returns false under normal circumstances', () => {
const columnSettings = fromJS({});
const status = fromJS({});
const status = normalizeStatus(fromJS({}));
expect(shouldFilter(status, columnSettings)).toBe(false);
});
it('reblog: returns true when `shows.reblog == false`', () => {
const columnSettings = fromJS({ shows: { reblog: false } });
const status = fromJS({ reblog: {} });
const status = normalizeStatus(fromJS({ reblog: {} }));
expect(shouldFilter(status, columnSettings)).toBe(true);
});
it('reblog: returns false when `shows.reblog == true`', () => {
const columnSettings = fromJS({ shows: { reblog: true } });
const status = fromJS({ reblog: {} });
const status = normalizeStatus(fromJS({ reblog: {} }));
expect(shouldFilter(status, columnSettings)).toBe(false);
});
it('reply: returns true when `shows.reply == false`', () => {
const columnSettings = fromJS({ shows: { reply: false } });
const status = fromJS({ in_reply_to_id: '1234' });
const status = normalizeStatus(fromJS({ in_reply_to_id: '1234' }));
expect(shouldFilter(status, columnSettings)).toBe(true);
});
it('reply: returns false when `shows.reply == true`', () => {
const columnSettings = fromJS({ shows: { reply: true } });
const status = fromJS({ in_reply_to_id: '1234' });
const status = normalizeStatus(fromJS({ in_reply_to_id: '1234' }));
expect(shouldFilter(status, columnSettings)).toBe(false);
});
it('direct: returns true when `shows.direct == false`', () => {
const columnSettings = fromJS({ shows: { direct: false } });
const status = fromJS({ visibility: 'direct' });
const status = normalizeStatus(fromJS({ visibility: 'direct' }));
expect(shouldFilter(status, columnSettings)).toBe(true);
});
it('direct: returns false when `shows.direct == true`', () => {
const columnSettings = fromJS({ shows: { direct: true } });
const status = fromJS({ visibility: 'direct' });
const status = normalizeStatus(fromJS({ visibility: 'direct' }));
expect(shouldFilter(status, columnSettings)).toBe(false);
});
it('direct: returns false for a public post when `shows.direct == false`', () => {
const columnSettings = fromJS({ shows: { direct: false } });
const status = fromJS({ visibility: 'public' });
const status = normalizeStatus(fromJS({ visibility: 'public' }));
expect(shouldFilter(status, columnSettings)).toBe(false);
});
it('multiple settings', () => {
const columnSettings = fromJS({ shows: { reblog: false, reply: false, direct: false } });
const status = fromJS({ reblog: null, in_reply_to_id: null, visibility: 'direct' });
const status = normalizeStatus(fromJS({ reblog: null, in_reply_to_id: null, visibility: 'direct' }));
expect(shouldFilter(status, columnSettings)).toBe(true);
});
it('multiple settings', () => {
const columnSettings = fromJS({ shows: { reblog: false, reply: true, direct: false } });
const status = fromJS({ reblog: null, in_reply_to_id: '1234', visibility: 'public' });
const status = normalizeStatus(fromJS({ reblog: null, in_reply_to_id: '1234', visibility: 'public' }));
expect(shouldFilter(status, columnSettings)).toBe(false);
});
it('multiple settings', () => {
const columnSettings = fromJS({ shows: { reblog: true, reply: false, direct: true } });
const status = fromJS({ reblog: {}, in_reply_to_id: '1234', visibility: 'direct' });
const status = normalizeStatus(fromJS({ reblog: {}, in_reply_to_id: '1234', visibility: 'direct' }));
expect(shouldFilter(status, columnSettings)).toBe(true);
});
});

View File

@@ -82,9 +82,9 @@ export const reduceEmoji = (emojiReacts: ImmutableList<EmojiReact>, favouritesCo
export const getReactForStatus = (status: any, allowedEmoji = ALLOWED_EMOJI): string | undefined => {
const result = reduceEmoji(
status.getIn(['pleroma', 'emoji_reactions'], ImmutableList()),
status.get('favourites_count', 0),
status.get('favourited'),
status.pleroma.get('emoji_reactions', ImmutableList()),
status.favourites_count || 0,
status.favourited,
allowedEmoji,
).filter(e => e.get('me') === true)
.getIn([0, 'name']);

View File

@@ -4,9 +4,9 @@ import type { Status as StatusEntity } from 'soapbox/types/entities';
export const shouldFilter = (status: StatusEntity, columnSettings: any) => {
const shows = ImmutableMap({
reblog: status.get('reblog') !== null,
reply: status.get('in_reply_to_id') !== null,
direct: status.get('visibility') === 'direct',
reblog: status.reblog !== null,
reply: status.in_reply_to_id !== null,
direct: status.visibility === 'direct',
});
return shows.some((value, key) => {