From 7183154a663701c24d9c573832288d66e9214565 Mon Sep 17 00:00:00 2001 From: Matvei Andrienko Date: Tue, 30 Apr 2024 14:58:08 +0200 Subject: [PATCH] feat: add reaction groups fallback (#1286) --- .lintstagedrc.json | 2 +- src/types.ts | 2 +- src/utils.ts | 31 +++++++++++++++++++++++++++++++ test/unit/utils.js | 41 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/.lintstagedrc.json b/.lintstagedrc.json index a86b3ff7f..8bdbf1e89 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,4 +1,4 @@ { "{**/*.{js,ts,md,css,scss,json}, .eslintrc.json, .prettierrc, .babelrc}": "prettier --list-different", - "**/*.{js,md,ts}": "eslint --max-warnings 0" + "**/*.{js,md,ts}, !test": "eslint --max-warnings 0" } diff --git a/src/types.ts b/src/types.ts index 2db5673b7..45d789687 100644 --- a/src/types.ts +++ b/src/types.ts @@ -627,7 +627,6 @@ export type MessageResponse< export type MessageResponseBase< StreamChatGenerics extends ExtendableGenerics = DefaultGenerics > = MessageBase & { - reaction_groups: Record; type: MessageLabel; args?: string; before_message_send_failed?: boolean; @@ -651,6 +650,7 @@ export type MessageResponseBase< pinned_by?: UserResponse | null; poll?: PollResponse; reaction_counts?: { [key: string]: number } | null; + reaction_groups?: { [key: string]: ReactionGroupResponse } | null; reaction_scores?: { [key: string]: number } | null; reply_count?: number; shadowed?: boolean; diff --git a/src/utils.ts b/src/utils.ts index 9d20e49e6..cb0609bc0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,6 +8,7 @@ import { UserResponse, MessageResponse, FormatMessageResponse, + ReactionGroupResponse, } from './types'; import { AxiosRequestConfig } from 'axios'; @@ -295,6 +296,11 @@ export function formatMessage { } }); }); + +describe('reaction groups fallback', () => { + it('uses groups if present', () => { + const date = '2024-04-30T11:03:39.217974Z'; + const groups = { + love: { + count: 1, + sum_scores: 1, + first_reaction_at: date, + last_reaction_at: date, + }, + }; + + const message = formatMessage({ reaction_groups: groups }); + expect(message.reaction_groups).to.be.equal(groups); + }); + + it('falls back to counts + scores', () => { + const counts = { love: 1, sad: 1 }; + const scores = { love: 1, sad: 2 }; + + const message = formatMessage({ + reaction_groups: null, + reaction_counts: counts, + reaction_scores: scores, + }); + + expect(message.reaction_groups).to.deep.equal({ + love: { + count: 1, + sum_scores: 1, + }, + sad: { + count: 1, + sum_scores: 2, + }, + }); + }); +});