|
| 1 | +import '../../../setup' |
| 2 | +// Regression test for https://github.com/contember/bindx/issues/53 (nested hasMany with orderBy reads empty from a list-loaded accessor) |
| 3 | +import { afterEach, describe, expect, test } from 'bun:test' |
| 4 | +import { cleanup, render, waitFor } from '@testing-library/react' |
| 5 | +import React from 'react' |
| 6 | +import { BindxProvider, MockAdapter, defineSchema, entityDef, hasMany, scalar, useEntityList } from '@contember/bindx-react' |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + cleanup() |
| 10 | +}) |
| 11 | + |
| 12 | +interface Tag { |
| 13 | + id: string |
| 14 | + name: string |
| 15 | + order: number |
| 16 | +} |
| 17 | + |
| 18 | +interface Article { |
| 19 | + id: string |
| 20 | + title: string |
| 21 | + tags: Tag[] |
| 22 | +} |
| 23 | + |
| 24 | +interface TestSchema { |
| 25 | + Article: Article |
| 26 | + Tag: Tag |
| 27 | +} |
| 28 | + |
| 29 | +const schema = defineSchema<TestSchema>({ |
| 30 | + entities: { |
| 31 | + Article: { |
| 32 | + fields: { |
| 33 | + id: scalar(), |
| 34 | + title: scalar(), |
| 35 | + tags: hasMany('Tag'), |
| 36 | + }, |
| 37 | + }, |
| 38 | + Tag: { |
| 39 | + fields: { |
| 40 | + id: scalar(), |
| 41 | + name: scalar(), |
| 42 | + order: scalar(), |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | +}) |
| 47 | + |
| 48 | +const entityDefs = { |
| 49 | + Article: entityDef<Article>('Article'), |
| 50 | + Tag: entityDef<Tag>('Tag'), |
| 51 | +} as const |
| 52 | + |
| 53 | +function createMockData() { |
| 54 | + return { |
| 55 | + Article: { |
| 56 | + 'article-1': { |
| 57 | + id: 'article-1', |
| 58 | + title: 'Test Article', |
| 59 | + tags: [ |
| 60 | + { id: 'tag-1', name: 'JavaScript', order: 0 }, |
| 61 | + { id: 'tag-2', name: 'React', order: 1 }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function queryByTestId(container: Element, testId: string): Element | null { |
| 69 | + return container.querySelector(`[data-testid="${testId}"]`) |
| 70 | +} |
| 71 | + |
| 72 | +// The nested hasMany `tags` is selected with an `orderBy` argument. The adapter returns the two tags |
| 73 | +// (verifiable by logging the raw query result), but reading `list.items[0].tags.items` yields an empty |
| 74 | +// array — the relation's scalar siblings (title) load fine, only the ordered hasMany comes back empty. |
| 75 | +describe('useEntityList nested hasMany with orderBy', () => { |
| 76 | + test('should expose nested hasMany items when the relation is selected WITH orderBy', async () => { |
| 77 | + const adapter = new MockAdapter(createMockData(), { delay: 0 }) |
| 78 | + |
| 79 | + function TestComponent(): React.ReactElement { |
| 80 | + const list = useEntityList(entityDefs.Article, { filter: { id: { eq: 'article-1' } } }, a => |
| 81 | + a.id().title().tags({ orderBy: [{ order: 'asc' }] }, t => t.id().name()), |
| 82 | + ) |
| 83 | + if (list.$status !== 'ready') return <div data-testid="loading">Loading...</div> |
| 84 | + const article = list.items[0] |
| 85 | + return ( |
| 86 | + <div> |
| 87 | + <span data-testid="title">{article?.title.value}</span> |
| 88 | + <span data-testid="tag-count">{article ? article.tags.items.length : -1}</span> |
| 89 | + </div> |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + const { container } = render( |
| 94 | + <BindxProvider adapter={adapter} schema={schema}> |
| 95 | + <TestComponent /> |
| 96 | + </BindxProvider>, |
| 97 | + ) |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + expect(queryByTestId(container, 'title')).not.toBeNull() |
| 101 | + }) |
| 102 | + |
| 103 | + expect(container.querySelector('[data-testid="title"]')?.textContent).toBe('Test Article') |
| 104 | + // Bug: reads '0'. The two ordered tags are dropped from the accessor. |
| 105 | + expect(container.querySelector('[data-testid="tag-count"]')?.textContent).toBe('2') |
| 106 | + }) |
| 107 | + |
| 108 | + test('control: nested hasMany WITHOUT orderBy exposes items', async () => { |
| 109 | + const adapter = new MockAdapter(createMockData(), { delay: 0 }) |
| 110 | + |
| 111 | + function TestComponent(): React.ReactElement { |
| 112 | + const list = useEntityList(entityDefs.Article, { filter: { id: { eq: 'article-1' } } }, a => |
| 113 | + a.id().title().tags(t => t.id().name()), |
| 114 | + ) |
| 115 | + if (list.$status !== 'ready') return <div data-testid="loading">Loading...</div> |
| 116 | + const article = list.items[0] |
| 117 | + return <span data-testid="tag-count">{article ? article.tags.items.length : -1}</span> |
| 118 | + } |
| 119 | + |
| 120 | + const { container } = render( |
| 121 | + <BindxProvider adapter={adapter} schema={schema}> |
| 122 | + <TestComponent /> |
| 123 | + </BindxProvider>, |
| 124 | + ) |
| 125 | + |
| 126 | + await waitFor(() => { |
| 127 | + expect(queryByTestId(container, 'tag-count')).not.toBeNull() |
| 128 | + }) |
| 129 | + |
| 130 | + expect(container.querySelector('[data-testid="tag-count"]')?.textContent).toBe('2') |
| 131 | + }) |
| 132 | +}) |
0 commit comments