|
| 1 | +// |
| 2 | +// Copyright © 2026 Hardcore Engineering Inc. |
| 3 | +// |
| 4 | +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. You may |
| 6 | +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +import type { AttributeModel } from '@hcengineering/view' |
| 17 | +import type { IntlString } from '@hcengineering/platform' |
| 18 | +import type { Class, Client, Doc, Hierarchy, Ref } from '@hcengineering/core' |
| 19 | +import { rebuildRelationshipTableViewModel } from '../data/relationshipBuilder' |
| 20 | + |
| 21 | +jest.mock('@hcengineering/view-resources', () => ({ |
| 22 | + buildConfigAssociation: jest.fn(() => []), |
| 23 | + buildConfigLookup: jest.fn(() => ({})) |
| 24 | +})) |
| 25 | + |
| 26 | +type DocOverrides = Partial<Doc> & { |
| 27 | + $associations?: Record<string, Doc[] | Doc> |
| 28 | + title?: string |
| 29 | +} |
| 30 | + |
| 31 | +function doc (id: string, overrides: DocOverrides = {}): Doc { |
| 32 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- test helper with spread overrides |
| 33 | + const result = { |
| 34 | + _id: id as Ref<Doc>, |
| 35 | + _class: 'test:class:Doc' as Ref<Class<Doc>>, |
| 36 | + space: 'test:space' as any, |
| 37 | + modifiedOn: 0, |
| 38 | + modifiedBy: '' as any, |
| 39 | + createdOn: 0, |
| 40 | + createdBy: '' as any, |
| 41 | + ...overrides |
| 42 | + } as Doc |
| 43 | + return result |
| 44 | +} |
| 45 | + |
| 46 | +function attr (key: string, label: string = key): AttributeModel { |
| 47 | + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- minimal AttributeModel for tests |
| 48 | + const result = { |
| 49 | + key, |
| 50 | + label: label as IntlString, |
| 51 | + _class: '' as Ref<Class<Doc>>, |
| 52 | + sortingKey: '', |
| 53 | + collectionAttr: false, |
| 54 | + isLookup: false |
| 55 | + } as AttributeModel |
| 56 | + return result |
| 57 | +} |
| 58 | + |
| 59 | +describe('relationshipBuilder', () => { |
| 60 | + const hierarchy: Hierarchy = {} as any |
| 61 | + const client: Client = {} as any |
| 62 | + const cardClass = 'test:class:Card' as Ref<Class<Doc>> |
| 63 | + |
| 64 | + describe('rebuildRelationshipTableViewModel', () => { |
| 65 | + it('returns one row when doc has no associations (single expanded row with undefined children)', async () => { |
| 66 | + const root = doc('root-1') |
| 67 | + const model: AttributeModel[] = [ |
| 68 | + attr(''), |
| 69 | + attr('$associations.assoc1_b', 'Level1'), |
| 70 | + attr('$associations.assoc1_b.$associations.assoc2_b', 'Level2') |
| 71 | + ] |
| 72 | + const viewModel = await rebuildRelationshipTableViewModel([root], model, cardClass, hierarchy, client) |
| 73 | + expect(viewModel).toHaveLength(1) |
| 74 | + expect(viewModel[0].cells).toHaveLength(3) |
| 75 | + expect(viewModel[0].cells[0].object).toBe(root) |
| 76 | + expect(viewModel[0].cells[0].rowSpan).toBe(1) |
| 77 | + expect(viewModel[0].cells[1].object).toBeUndefined() |
| 78 | + expect(viewModel[0].cells[1].parentObject).toBe(root) |
| 79 | + expect(viewModel[0].cells[2].object).toBeUndefined() |
| 80 | + expect(viewModel[0].cells[2].parentObject).toBeUndefined() |
| 81 | + }) |
| 82 | + |
| 83 | + it('builds one row per first-level child for single-level association', async () => { |
| 84 | + const b1 = doc('b1', { title: 'B1' }) |
| 85 | + const b2 = doc('b2', { title: 'B2' }) |
| 86 | + const root = doc('root-1', { |
| 87 | + $associations: { assoc1_b: [b1, b2] } |
| 88 | + }) |
| 89 | + const model: AttributeModel[] = [attr(''), attr('$associations.assoc1_b', 'Level1')] |
| 90 | + const viewModel = await rebuildRelationshipTableViewModel([root], model, cardClass, hierarchy, client) |
| 91 | + expect(viewModel).toHaveLength(2) |
| 92 | + expect(viewModel[0].cells[0].object).toBe(root) |
| 93 | + expect(viewModel[0].cells[0].rowSpan).toBe(2) |
| 94 | + expect(viewModel[0].cells[1].object).toBe(b1) |
| 95 | + expect(viewModel[0].cells[1].parentObject).toBe(root) |
| 96 | + expect(viewModel[0].cells[1].rowSpan).toBe(1) |
| 97 | + expect(viewModel[1].cells[0].rowSpan).toBe(0) |
| 98 | + expect(viewModel[1].cells[0].object).toBeUndefined() |
| 99 | + expect(viewModel[1].cells[1].object).toBe(b2) |
| 100 | + expect(viewModel[1].cells[1].parentObject).toBe(root) |
| 101 | + }) |
| 102 | + |
| 103 | + it('builds rows with correct nested objects for two-level association (A -> B -> C)', async () => { |
| 104 | + const c1 = doc('c1', { title: 'C1' }) |
| 105 | + const c2 = doc('c2', { title: 'C2' }) |
| 106 | + const c3 = doc('c3', { title: 'C3' }) |
| 107 | + const b1 = doc('b1', { title: 'B1', $associations: { assoc2_b: [c1, c2] } }) |
| 108 | + const b2 = doc('b2', { title: 'B2', $associations: { assoc2_b: [c3] } }) |
| 109 | + const root = doc('root-1', { |
| 110 | + $associations: { assoc1_b: [b1, b2] } |
| 111 | + }) |
| 112 | + const model: AttributeModel[] = [ |
| 113 | + attr(''), |
| 114 | + attr('$associations.assoc1_b', 'Level1'), |
| 115 | + attr('$associations.assoc1_b.$associations.assoc2_b', 'Level2') |
| 116 | + ] |
| 117 | + const viewModel = await rebuildRelationshipTableViewModel([root], model, cardClass, hierarchy, client) |
| 118 | + expect(viewModel).toHaveLength(3) |
| 119 | + expect(viewModel[0].cells[0].object).toBe(root) |
| 120 | + expect(viewModel[0].cells[0].rowSpan).toBe(3) |
| 121 | + expect(viewModel[0].cells[1].object).toBe(b1) |
| 122 | + expect(viewModel[0].cells[1].parentObject).toBe(root) |
| 123 | + expect(viewModel[0].cells[1].rowSpan).toBe(2) |
| 124 | + expect(viewModel[0].cells[2].object).toBe(c1) |
| 125 | + expect(viewModel[0].cells[2].parentObject).toBe(b1) |
| 126 | + expect(viewModel[0].cells[2].rowSpan).toBe(1) |
| 127 | + expect(viewModel[1].cells[2].object).toBe(c2) |
| 128 | + expect(viewModel[1].cells[2].parentObject).toBe(b1) |
| 129 | + expect(viewModel[2].cells[1].object).toBe(b2) |
| 130 | + expect(viewModel[2].cells[1].parentObject).toBe(root) |
| 131 | + expect(viewModel[2].cells[1].rowSpan).toBe(1) |
| 132 | + expect(viewModel[2].cells[2].object).toBe(c3) |
| 133 | + expect(viewModel[2].cells[2].parentObject).toBe(b2) |
| 134 | + }) |
| 135 | + |
| 136 | + it('emits one row when first-level child has no nested children', async () => { |
| 137 | + const b1 = doc('b1', { title: 'B1' }) |
| 138 | + const root = doc('root-1', { $associations: { assoc1_b: [b1] } }) |
| 139 | + const model: AttributeModel[] = [ |
| 140 | + attr(''), |
| 141 | + attr('$associations.assoc1_b', 'Level1'), |
| 142 | + attr('$associations.assoc1_b.$associations.assoc2_b', 'Level2') |
| 143 | + ] |
| 144 | + const viewModel = await rebuildRelationshipTableViewModel([root], model, cardClass, hierarchy, client) |
| 145 | + expect(viewModel).toHaveLength(1) |
| 146 | + expect(viewModel[0].cells[0].object).toBe(root) |
| 147 | + expect(viewModel[0].cells[1].object).toBe(b1) |
| 148 | + expect(viewModel[0].cells[1].parentObject).toBe(root) |
| 149 | + expect(viewModel[0].cells[2].object).toBeUndefined() |
| 150 | + expect(viewModel[0].cells[2].parentObject).toBe(b1) |
| 151 | + }) |
| 152 | + |
| 153 | + it('handles multiple root docs each with their own expanded rows', async () => { |
| 154 | + const b1 = doc('b1') |
| 155 | + const b2 = doc('b2') |
| 156 | + const root1 = doc('root-1', { $associations: { assoc1_b: [b1] } }) |
| 157 | + const root2 = doc('root-2', { $associations: { assoc1_b: [b2] } }) |
| 158 | + const model: AttributeModel[] = [attr(''), attr('$associations.assoc1_b', 'Level1')] |
| 159 | + const viewModel = await rebuildRelationshipTableViewModel([root1, root2], model, cardClass, hierarchy, client) |
| 160 | + expect(viewModel).toHaveLength(2) |
| 161 | + expect(viewModel[0].cells[0].object).toBe(root1) |
| 162 | + expect(viewModel[0].cells[0].rowSpan).toBe(1) |
| 163 | + expect(viewModel[0].cells[1].object).toBe(b1) |
| 164 | + expect(viewModel[1].cells[0].object).toBe(root2) |
| 165 | + expect(viewModel[1].cells[1].object).toBe(b2) |
| 166 | + }) |
| 167 | + |
| 168 | + it('preserves model order for non-association attributes', async () => { |
| 169 | + const b1 = doc('b1') |
| 170 | + const root = doc('root-1', { $associations: { assoc1_b: [b1] } }) |
| 171 | + const model: AttributeModel[] = [attr(''), attr('$associations.assoc1_b', 'Assoc'), attr('title', 'Title')] |
| 172 | + const viewModel = await rebuildRelationshipTableViewModel([root], model, cardClass, hierarchy, client) |
| 173 | + expect(viewModel).toHaveLength(1) |
| 174 | + expect(viewModel[0].cells).toHaveLength(3) |
| 175 | + expect(viewModel[0].cells[2].attribute.key).toBe('title') |
| 176 | + expect(viewModel[0].cells[2].object).toBe(root) |
| 177 | + expect(viewModel[0].cells[2].rowSpan).toBe(1) |
| 178 | + }) |
| 179 | + }) |
| 180 | +}) |
0 commit comments