@@ -1126,6 +1126,177 @@ export const Generic = () => {
11261126 } ) ;
11271127 } ) ;
11281128
1129+ it ( 'should also update the corresponding message.reaction_groups with reaction.new' , async ( ) => {
1130+ useMockedApis ( chatClient , [ queryChannelsApi ( channels ) ] ) ;
1131+ renderComponent ( ) ;
1132+ act ( ( ) => dispatchConnectionChangedEvent ( chatClient ) ) ;
1133+ await act ( async ( ) => await chatClient . offlineDb . syncManager . invokeSyncStatusListeners ( true ) ) ;
1134+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'channel-list' ) ) . toBeTruthy ( ) ) ;
1135+
1136+ const targetChannel = channels [ getRandomInt ( 0 , channels . length - 1 ) ] ;
1137+ const targetMessage =
1138+ targetChannel . messages [ getRandomInt ( 0 , targetChannel . messages . length - 1 ) ] ;
1139+ const reactionMember =
1140+ targetChannel . members [ getRandomInt ( 0 , targetChannel . members . length - 1 ) ] ;
1141+
1142+ const newReaction = generateReaction ( {
1143+ message_id : targetMessage . id ,
1144+ type : 'wow' ,
1145+ user : reactionMember . user ,
1146+ } ) ;
1147+ const newDate = new Date ( ) . toISOString ( ) ;
1148+ const messageWithNewReaction = {
1149+ ...targetMessage ,
1150+ latest_reactions : [ ...targetMessage . latest_reactions , newReaction ] ,
1151+ reaction_groups : {
1152+ ...targetMessage . reaction_groups ,
1153+ [ newReaction . type ] : {
1154+ count : 1 ,
1155+ first_reaction_at : newDate ,
1156+ last_reaction_at : newDate ,
1157+ sum_scores : 1 ,
1158+ } ,
1159+ } ,
1160+ } ;
1161+
1162+ act ( ( ) =>
1163+ dispatchReactionNewEvent (
1164+ chatClient ,
1165+ newReaction ,
1166+ messageWithNewReaction ,
1167+ targetChannel . channel ,
1168+ ) ,
1169+ ) ;
1170+
1171+ await waitFor ( async ( ) => {
1172+ const messageRows = await BetterSqlite . selectFromTable ( 'messages' ) ;
1173+ const messageWithReactionRow = messageRows . filter (
1174+ ( m ) => m . id === messageWithNewReaction . id ,
1175+ ) [ 0 ] ;
1176+
1177+ const reactionGroups = JSON . parse ( messageWithReactionRow . reactionGroups ) ;
1178+
1179+ expect ( reactionGroups [ newReaction . type ] ?. count ) . toBe ( 1 ) ;
1180+ expect ( reactionGroups [ newReaction . type ] ?. sum_scores ) . toBe ( 1 ) ;
1181+ expect ( reactionGroups [ newReaction . type ] ?. first_reaction_at ) . toBe ( newDate ) ;
1182+ expect ( reactionGroups [ newReaction . type ] ?. last_reaction_at ) . toBe ( newDate ) ;
1183+ } ) ;
1184+ } ) ;
1185+
1186+ it ( 'should also update the corresponding message.reaction_groups with reaction.updated' , async ( ) => {
1187+ useMockedApis ( chatClient , [ queryChannelsApi ( channels ) ] ) ;
1188+ renderComponent ( ) ;
1189+ act ( ( ) => dispatchConnectionChangedEvent ( chatClient ) ) ;
1190+ await act ( async ( ) => await chatClient . offlineDb . syncManager . invokeSyncStatusListeners ( true ) ) ;
1191+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'channel-list' ) ) . toBeTruthy ( ) ) ;
1192+
1193+ const targetChannel = channels [ getRandomInt ( 0 , channels . length - 1 ) ] ;
1194+ const targetMessage =
1195+ targetChannel . messages [ getRandomInt ( 0 , targetChannel . messages . length - 1 ) ] ;
1196+ const reactionMember =
1197+ targetChannel . members [ getRandomInt ( 0 , targetChannel . members . length - 1 ) ] ;
1198+
1199+ const newReaction = generateReaction ( {
1200+ message_id : targetMessage . id ,
1201+ type : 'wow' ,
1202+ user : reactionMember . user ,
1203+ } ) ;
1204+ const newDate = new Date ( ) . toISOString ( ) ;
1205+ const messageWithNewReaction = {
1206+ ...targetMessage ,
1207+ latest_reactions : [ ...targetMessage . latest_reactions , newReaction ] ,
1208+ reaction_groups : {
1209+ ...targetMessage . reaction_groups ,
1210+ [ newReaction . type ] : {
1211+ count : 1 ,
1212+ first_reaction_at : newDate ,
1213+ last_reaction_at : newDate ,
1214+ sum_scores : 1 ,
1215+ } ,
1216+ } ,
1217+ } ;
1218+
1219+ act ( ( ) =>
1220+ dispatchReactionUpdatedEvent (
1221+ chatClient ,
1222+ newReaction ,
1223+ messageWithNewReaction ,
1224+ targetChannel . channel ,
1225+ ) ,
1226+ ) ;
1227+
1228+ await waitFor ( async ( ) => {
1229+ const messageRows = await BetterSqlite . selectFromTable ( 'messages' ) ;
1230+ const messageWithReactionRow = messageRows . filter (
1231+ ( m ) => m . id === messageWithNewReaction . id ,
1232+ ) [ 0 ] ;
1233+
1234+ const reactionGroups = JSON . parse ( messageWithReactionRow . reactionGroups ) ;
1235+
1236+ expect ( reactionGroups [ newReaction . type ] ?. count ) . toBe ( 1 ) ;
1237+ expect ( reactionGroups [ newReaction . type ] ?. sum_scores ) . toBe ( 1 ) ;
1238+ expect ( reactionGroups [ newReaction . type ] ?. first_reaction_at ) . toBe ( newDate ) ;
1239+ expect ( reactionGroups [ newReaction . type ] ?. last_reaction_at ) . toBe ( newDate ) ;
1240+ } ) ;
1241+ } ) ;
1242+
1243+ it ( 'should also update the corresponding message.reaction_groups with reaction.deleted' , async ( ) => {
1244+ useMockedApis ( chatClient , [ queryChannelsApi ( channels ) ] ) ;
1245+ renderComponent ( ) ;
1246+ act ( ( ) => dispatchConnectionChangedEvent ( chatClient ) ) ;
1247+ await act ( async ( ) => await chatClient . offlineDb . syncManager . invokeSyncStatusListeners ( true ) ) ;
1248+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'channel-list' ) ) . toBeTruthy ( ) ) ;
1249+
1250+ const targetChannel = channels [ getRandomInt ( 0 , channels . length - 1 ) ] ;
1251+ const targetMessage =
1252+ targetChannel . messages [ getRandomInt ( 0 , targetChannel . messages . length - 1 ) ] ;
1253+ const reactionMember =
1254+ targetChannel . members [ getRandomInt ( 0 , targetChannel . members . length - 1 ) ] ;
1255+
1256+ const newReaction = generateReaction ( {
1257+ message_id : targetMessage . id ,
1258+ type : 'wow' ,
1259+ user : reactionMember . user ,
1260+ } ) ;
1261+ const newDate = new Date ( ) . toISOString ( ) ;
1262+ const messageWithNewReaction = {
1263+ ...targetMessage ,
1264+ latest_reactions : [ ...targetMessage . latest_reactions , newReaction ] ,
1265+ reaction_groups : {
1266+ ...targetMessage . reaction_groups ,
1267+ [ newReaction . type ] : {
1268+ count : 1 ,
1269+ first_reaction_at : newDate ,
1270+ last_reaction_at : newDate ,
1271+ sum_scores : 1 ,
1272+ } ,
1273+ } ,
1274+ } ;
1275+
1276+ act ( ( ) =>
1277+ dispatchReactionDeletedEvent (
1278+ chatClient ,
1279+ newReaction ,
1280+ messageWithNewReaction ,
1281+ targetChannel . channel ,
1282+ ) ,
1283+ ) ;
1284+
1285+ await waitFor ( async ( ) => {
1286+ const messageRows = await BetterSqlite . selectFromTable ( 'messages' ) ;
1287+ const messageWithReactionRow = messageRows . filter (
1288+ ( m ) => m . id === messageWithNewReaction . id ,
1289+ ) [ 0 ] ;
1290+
1291+ const reactionGroups = JSON . parse ( messageWithReactionRow . reactionGroups ) ;
1292+
1293+ expect ( reactionGroups [ newReaction . type ] ?. count ) . toBe ( 1 ) ;
1294+ expect ( reactionGroups [ newReaction . type ] ?. sum_scores ) . toBe ( 1 ) ;
1295+ expect ( reactionGroups [ newReaction . type ] ?. first_reaction_at ) . toBe ( newDate ) ;
1296+ expect ( reactionGroups [ newReaction . type ] ?. last_reaction_at ) . toBe ( newDate ) ;
1297+ } ) ;
1298+ } ) ;
1299+
11291300 it ( 'should add a member to DB when a new member is added to channel' , async ( ) => {
11301301 useMockedApis ( chatClient , [ queryChannelsApi ( channels ) ] ) ;
11311302 renderComponent ( ) ;
0 commit comments