-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: reactions not displaying on thread main message #7621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OtavioStasiak
wants to merge
4
commits into
develop
Choose a base branch
from
fix.reactions-not-displaying-on-main-thread-message
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+245
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c2fafab
fix: reactions not displaying on thread main message
OtavioStasiak fad2dbc
Merge branch 'develop' into fix.reactions-not-displaying-on-main-thre…
OtavioStasiak e04131c
fix: guard loadThreadMessages against null messages from getThreadMes…
OtavioStasiak 4e950d7
Merge branch 'develop' into fix.reactions-not-displaying-on-main-thre…
OtavioStasiak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { loadThreadMessages } from './loadThreadMessages'; | ||
| import { type IReaction } from '../../definitions'; | ||
| import database from '../database'; | ||
| import { getThreadById } from '../database/services/Thread'; | ||
| import { Encryption } from '../encryption'; | ||
| import sdk from '../services/sdk'; | ||
|
|
||
| jest.mock('../services/sdk', () => ({ | ||
| __esModule: true, | ||
| default: { methodCallWrapper: jest.fn() } | ||
| })); | ||
|
|
||
| jest.mock('../database', () => ({ | ||
| __esModule: true, | ||
| default: { active: {} } | ||
| })); | ||
|
|
||
| jest.mock('../database/services/Thread', () => ({ | ||
| getThreadById: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('../encryption', () => ({ | ||
| Encryption: { decryptMessages: jest.fn((messages: any) => Promise.resolve(messages)) } | ||
| })); | ||
|
|
||
| jest.mock('./helpers/log', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn() | ||
| })); | ||
|
|
||
| jest.mock('@nozbe/watermelondb/RawRecord', () => ({ | ||
| sanitizedRaw: jest.fn((raw: any) => raw) | ||
| })); | ||
|
|
||
| jest.mock('ejson', () => ({ | ||
| __esModule: true, | ||
| default: { fromJSONValue: (value: any) => value } | ||
| })); | ||
|
|
||
| const mockedMethodCall = sdk.methodCallWrapper as jest.MockedFunction<typeof sdk.methodCallWrapper>; | ||
| const mockedGetThreadById = getThreadById as jest.MockedFunction<typeof getThreadById>; | ||
|
|
||
| const TMID = 'PARENT_ID'; | ||
| const RID = 'ROOM_ID'; | ||
|
|
||
| interface IParentFixture { | ||
| _id: string; | ||
| rid: string; | ||
| msg: string; | ||
| tlm: Date; | ||
| tcount: number; | ||
| _updatedAt: Date; | ||
| reactions: Partial<IReaction>[]; | ||
| } | ||
|
|
||
| interface IReplyFixture { | ||
| _id: string; | ||
| rid: string; | ||
| tmid: string; | ||
| msg: string; | ||
| _updatedAt: Date; | ||
| } | ||
|
|
||
| const buildParent = (updatedAt: Date, reactions: Partial<IReaction>[]): IParentFixture => ({ | ||
| _id: TMID, | ||
| rid: RID, | ||
| msg: 'parent', | ||
| tlm: new Date(), | ||
| tcount: 1, | ||
| _updatedAt: updatedAt, | ||
| reactions | ||
| }); | ||
|
|
||
| const buildReply = (): IReplyFixture => ({ _id: 'REPLY_ID', rid: RID, tmid: TMID, msg: 'reply', _updatedAt: new Date() }); | ||
|
|
||
| let batched: any[] = []; | ||
| let threadsCreated: any[] = []; | ||
|
|
||
| const setupDatabase = (): void => { | ||
| batched = []; | ||
| threadsCreated = []; | ||
| const threadsCollection = { | ||
| schema: {}, | ||
| prepareCreate: jest.fn((fn: any) => { | ||
| const record: any = {}; | ||
| fn(record); | ||
| threadsCreated.push(record); | ||
| return record; | ||
| }) | ||
| }; | ||
| const threadMessagesCollection = { | ||
| schema: {}, | ||
| query: jest.fn(() => ({ fetch: jest.fn(() => Promise.resolve([])) })), | ||
| prepareCreate: jest.fn((fn: any) => { | ||
| const record: any = {}; | ||
| fn(record); | ||
| return record; | ||
| }) | ||
| }; | ||
| (database as any).active = { | ||
| get: jest.fn((table: string) => (table === 'threads' ? threadsCollection : threadMessagesCollection)), | ||
| write: jest.fn((fn: any) => fn()), | ||
| batch: jest.fn((records: any[]) => { | ||
| batched = records; | ||
| }) | ||
| }; | ||
| }; | ||
|
|
||
| describe('loadThreadMessages', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| setupDatabase(); | ||
| }); | ||
|
|
||
| it('creates the threads record from the parent returned by getThreadMessages', async () => { | ||
| const parent = buildParent(new Date('2026-01-02'), [{ emoji: ':thumbsup:', usernames: ['rocket.cat'] }]); | ||
| mockedMethodCall.mockResolvedValue([parent, buildReply()] as any); | ||
| mockedGetThreadById.mockResolvedValue(null); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| expect(threadsCreated).toHaveLength(1); | ||
| expect(threadsCreated[0].reactions).toEqual(parent.reactions); | ||
| expect(batched).toContain(threadsCreated[0]); | ||
| }); | ||
|
|
||
| it('updates a stale threads record so newer reactions reach the UI', async () => { | ||
| const parent = buildParent(new Date('2026-01-02'), [{ emoji: ':thumbsup:', usernames: ['rocket.cat'] }]); | ||
| mockedMethodCall.mockResolvedValue([parent, buildReply()] as any); | ||
|
|
||
| const updated: any = {}; | ||
| const threadRecord = { | ||
| id: TMID, | ||
| _updatedAt: new Date('2026-01-01'), | ||
| prepareUpdate: jest.fn((fn: any) => { | ||
| fn(updated); | ||
| return updated; | ||
| }) | ||
| }; | ||
| mockedGetThreadById.mockResolvedValue(threadRecord as any); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| expect(threadRecord.prepareUpdate).toHaveBeenCalled(); | ||
| expect(updated.reactions).toEqual(parent.reactions); | ||
| expect(batched).toContain(updated); | ||
| }); | ||
|
|
||
| it('leaves an up-to-date threads record untouched', async () => { | ||
| mockedMethodCall.mockResolvedValue([buildParent(new Date('2026-01-01'), []), buildReply()] as any); | ||
|
|
||
| const threadRecord = { id: TMID, _updatedAt: new Date('2026-01-01'), prepareUpdate: jest.fn() }; | ||
| mockedGetThreadById.mockResolvedValue(threadRecord as any); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| expect(threadRecord.prepareUpdate).not.toHaveBeenCalled(); | ||
| expect(threadsCreated).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('does not write the parent into thread_messages', async () => { | ||
| mockedMethodCall.mockResolvedValue([buildParent(new Date('2026-01-02'), []), buildReply()] as any); | ||
| mockedGetThreadById.mockResolvedValue(null); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| const threadMessageRecords = batched.filter(r => !threadsCreated.includes(r)); | ||
| expect(threadMessageRecords).toHaveLength(1); | ||
| expect(threadMessageRecords[0]._id).toBe('REPLY_ID'); | ||
| }); | ||
|
|
||
| it('still resolves when the server returns no parent', async () => { | ||
| mockedMethodCall.mockResolvedValue([buildReply()] as any); | ||
| mockedGetThreadById.mockResolvedValue(null); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| expect(mockedGetThreadById).not.toHaveBeenCalled(); | ||
| expect(threadsCreated).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('drops messages buildMessage could not normalize before decrypting', async () => { | ||
| mockedMethodCall.mockResolvedValue([buildParent(new Date('2026-01-02'), []), null, buildReply()] as any); | ||
| mockedGetThreadById.mockResolvedValue(null); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| expect(Encryption.decryptMessages).toHaveBeenCalledWith([ | ||
| expect.objectContaining({ _id: TMID }), | ||
| expect.objectContaining({ _id: 'REPLY_ID' }) | ||
| ]); | ||
| expect(threadsCreated).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('decrypts the parent along with the replies', async () => { | ||
| const parent = buildParent(new Date('2026-01-02'), []); | ||
| mockedMethodCall.mockResolvedValue([parent, buildReply()] as any); | ||
| mockedGetThreadById.mockResolvedValue(null); | ||
|
|
||
| await loadThreadMessages({ tmid: TMID, rid: RID }); | ||
|
|
||
| expect(Encryption.decryptMessages).toHaveBeenCalledWith(expect.arrayContaining([expect.objectContaining({ _id: TMID })])); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.