import transformResponse from '../transform_response' const person = (id: string, name: string) => ({ type: 'Person', id, attributes: { name, avatar: null }, }) const message = ( id: string, { authorId, replyRootId }: { authorId: string; replyRootId: string | null } ) => ({ type: 'Message', id, attributes: { text: `msg ${id}` }, relationships: { author: { data: { type: 'Person', id: authorId } }, reply_root: { data: replyRootId ? { type: 'Message', id: replyRootId } : null }, reaction_counts: { data: [] }, }, }) const root = message('01ROOT', { authorId: '2', replyRootId: '01ROOT' }) const reply = message('01REPLY', { authorId: '1', replyRootId: '01ROOT' }) describe('transformResponse — reply_root include', () => { it('attaches the included root and its author to a reply', () => { const result = transformResponse({ data: [reply], included: [root, person('1', 'Replier'), person('2', 'Root Author')], }) const [transformedReply] = result.data expect(transformedReply.replyRootId).toBe('01ROOT') expect(transformedReply.replyRoot.id).toBe('01ROOT') expect(transformedReply.replyRoot.text).toBe('msg 01ROOT') expect(transformedReply.replyRoot.author.name).toBe('Root Author') }) it('drops a record’s self reference so the result stays acyclic', () => { const result = transformResponse({ data: [reply, root], included: [root, person('1', 'Replier'), person('2', 'Root Author')], }) const [transformedReply, transformedRoot] = result.data expect(transformedReply.replyRoot.replyRoot).toBeNull() expect(transformedRoot.replyRootId).toBe('01ROOT') expect(transformedRoot.replyRoot).toBeNull() expect(() => JSON.stringify(result)).not.toThrow() }) it('leaves replyRoot null when the root is not included', () => { const result = transformResponse({ data: [reply], included: [person('1', 'Replier')], }) expect(result.data[0].replyRootId).toBe('01ROOT') expect(result.data[0].replyRoot).toBeNull() }) })