@contember/react-binding 2.2.0-alpha.20 → 2.2.0-alpha.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/src/accessorPropagation/useAccessorUpdateSubscription.cjs +4 -2
- package/dist/development/src/accessorPropagation/useAccessorUpdateSubscription.cjs.map +1 -1
- package/dist/development/src/accessorPropagation/useAccessorUpdateSubscription.js +4 -2
- package/dist/development/src/accessorPropagation/useAccessorUpdateSubscription.js.map +1 -1
- package/dist/development/src/accessorPropagation/useEntity.cjs +2 -4
- package/dist/development/src/accessorPropagation/useEntity.cjs.map +1 -1
- package/dist/development/src/accessorPropagation/useEntity.js +2 -4
- package/dist/development/src/accessorPropagation/useEntity.js.map +1 -1
- package/dist/development/src/accessorPropagation/useEntityList.cjs +2 -4
- package/dist/development/src/accessorPropagation/useEntityList.cjs.map +1 -1
- package/dist/development/src/accessorPropagation/useEntityList.js +2 -4
- package/dist/development/src/accessorPropagation/useEntityList.js.map +1 -1
- package/dist/development/src/accessorPropagation/useField.cjs +2 -4
- package/dist/development/src/accessorPropagation/useField.cjs.map +1 -1
- package/dist/development/src/accessorPropagation/useField.js +2 -4
- package/dist/development/src/accessorPropagation/useField.js.map +1 -1
- package/dist/development/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.cjs +33 -0
- package/dist/development/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.cjs.map +1 -0
- package/dist/development/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.js +33 -0
- package/dist/development/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.js.map +1 -0
- package/dist/production/src/accessorPropagation/useAccessorUpdateSubscription.cjs +4 -2
- package/dist/production/src/accessorPropagation/useAccessorUpdateSubscription.cjs.map +1 -1
- package/dist/production/src/accessorPropagation/useAccessorUpdateSubscription.js +4 -2
- package/dist/production/src/accessorPropagation/useAccessorUpdateSubscription.js.map +1 -1
- package/dist/production/src/accessorPropagation/useEntity.cjs +2 -4
- package/dist/production/src/accessorPropagation/useEntity.cjs.map +1 -1
- package/dist/production/src/accessorPropagation/useEntity.js +2 -4
- package/dist/production/src/accessorPropagation/useEntity.js.map +1 -1
- package/dist/production/src/accessorPropagation/useEntityList.cjs +2 -4
- package/dist/production/src/accessorPropagation/useEntityList.cjs.map +1 -1
- package/dist/production/src/accessorPropagation/useEntityList.js +2 -4
- package/dist/production/src/accessorPropagation/useEntityList.js.map +1 -1
- package/dist/production/src/accessorPropagation/useField.cjs +2 -4
- package/dist/production/src/accessorPropagation/useField.cjs.map +1 -1
- package/dist/production/src/accessorPropagation/useField.js +2 -4
- package/dist/production/src/accessorPropagation/useField.js.map +1 -1
- package/dist/production/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.cjs +33 -0
- package/dist/production/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.cjs.map +1 -0
- package/dist/production/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.js +33 -0
- package/dist/production/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.js.map +1 -0
- package/dist/types/accessorPropagation/useAccessorUpdateSubscription.d.ts +1 -0
- package/dist/types/accessorPropagation/useAccessorUpdateSubscription.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useEntity.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useEntityList.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useField.d.ts.map +1 -1
- package/dist/types/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.d.ts +7 -0
- package/dist/types/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/src/accessorPropagation/useAccessorUpdateSubscription.ts +8 -2
- package/src/accessorPropagation/useEntity.ts +2 -4
- package/src/accessorPropagation/useEntityList.ts +2 -4
- package/src/accessorPropagation/useField.ts +2 -4
- package/src/accessorPropagation/useOnHasOneRelationPathConnectionUpdate.ts +38 -0
- package/tests/cases/unit/core/nestedHasOneReconnect.test.tsx +280 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/react-binding",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
|
-
"version": "2.2.0-alpha.
|
|
4
|
+
"version": "2.2.0-alpha.22",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "./dist/production/index.js",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"ae:test": "api-extractor run"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@contember/binding": "2.2.0-alpha.
|
|
33
|
-
"@contember/client": "2.2.0-alpha.
|
|
34
|
-
"@contember/graphql-client": "2.2.0-alpha.
|
|
35
|
-
"@contember/react-client": "2.2.0-alpha.
|
|
36
|
-
"@contember/react-utils": "2.2.0-alpha.
|
|
37
|
-
"@contember/utilities": "2.2.0-alpha.
|
|
32
|
+
"@contember/binding": "2.2.0-alpha.22",
|
|
33
|
+
"@contember/client": "2.2.0-alpha.22",
|
|
34
|
+
"@contember/graphql-client": "2.2.0-alpha.22",
|
|
35
|
+
"@contember/react-client": "2.2.0-alpha.22",
|
|
36
|
+
"@contember/react-utils": "2.2.0-alpha.22",
|
|
37
|
+
"@contember/utilities": "2.2.0-alpha.22",
|
|
38
38
|
"@radix-ui/react-slot": "^1.2.4"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
@@ -6,6 +6,7 @@ import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
6
6
|
export const useAccessorUpdateSubscription = <
|
|
7
7
|
Accessor extends {
|
|
8
8
|
addEventListener: (event: { type: 'update' }, cb: (accessor: Accessor) => void) => () => void
|
|
9
|
+
getAccessor: () => Accessor
|
|
9
10
|
},
|
|
10
11
|
>(getAccessor: () => Accessor): [Accessor, { update: () => void }] => {
|
|
11
12
|
const [state, setState] = useState<Accessor>(() => getAccessor())
|
|
@@ -18,20 +19,25 @@ export const useAccessorUpdateSubscription = <
|
|
|
18
19
|
getterRef.current = getAccessor
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
// A state node's getter is stable, so this changes exactly when `getAccessor` resolves to another node.
|
|
23
|
+
const getSubscribedAccessor = accessor.getAccessor
|
|
24
|
+
|
|
21
25
|
useEffect(() => {
|
|
22
26
|
let isStillSubscribed = true
|
|
23
|
-
const unsubscribe =
|
|
27
|
+
const unsubscribe = getSubscribedAccessor().addEventListener({ type: 'update' }, newAccessor => {
|
|
24
28
|
if (!isStillSubscribed || getterRef.current !== getAccessor) {
|
|
25
29
|
return
|
|
26
30
|
}
|
|
27
31
|
setState(newAccessor)
|
|
28
32
|
})
|
|
33
|
+
// Catches up with updates that landed between the render and this subscription.
|
|
34
|
+
setState(getAccessor())
|
|
29
35
|
|
|
30
36
|
return () => {
|
|
31
37
|
isStillSubscribed = false
|
|
32
38
|
unsubscribe()
|
|
33
39
|
}
|
|
34
|
-
}, [getAccessor])
|
|
40
|
+
}, [getAccessor, getSubscribedAccessor])
|
|
35
41
|
|
|
36
42
|
const update = useCallback(() => {
|
|
37
43
|
setState(getAccessor())
|
|
@@ -2,9 +2,9 @@ import { useConstantLengthInvariant, useConstantValueInvariant } from '@contembe
|
|
|
2
2
|
import { useCallback } from 'react'
|
|
3
3
|
import { useEntityKey, useGetEntityByKey } from './index.js'
|
|
4
4
|
import type { EntityAccessor } from '@contember/binding'
|
|
5
|
-
import { useOnConnectionUpdate } from '../entityEvents/index.js'
|
|
6
5
|
import type { SugaredRelativeSingleEntity } from '@contember/binding'
|
|
7
6
|
import { useAccessorUpdateSubscription } from './useAccessorUpdateSubscription.js'
|
|
7
|
+
import { useOnHasOneRelationPathConnectionUpdate } from './useOnHasOneRelationPathConnectionUpdate.js'
|
|
8
8
|
import { useDesugaredRelativeSingleEntity } from './useDesugaredRelativeSingleEntity.js'
|
|
9
9
|
|
|
10
10
|
function useEntity(): EntityAccessor
|
|
@@ -37,9 +37,7 @@ function useEntity(...entity: [] | [string | SugaredRelativeSingleEntity | undef
|
|
|
37
37
|
)
|
|
38
38
|
const [entity, { update }] = useAccessorUpdateSubscription(getEntity)
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
useOnConnectionUpdate(relativeSingleEntity.hasOneRelationPath[0].field, update)
|
|
42
|
-
}
|
|
40
|
+
useOnHasOneRelationPathConnectionUpdate(relativeSingleEntity.hasOneRelationPath, update)
|
|
43
41
|
|
|
44
42
|
return entity
|
|
45
43
|
}
|
|
@@ -2,9 +2,9 @@ import { useConstantLengthInvariant, useConstantValueInvariant } from '@contembe
|
|
|
2
2
|
import { useCallback } from 'react'
|
|
3
3
|
import { useEntityKey, useGetEntityByKey } from './index.js'
|
|
4
4
|
import type { EntityListAccessor } from '@contember/binding'
|
|
5
|
-
import { useOnConnectionUpdate } from '../entityEvents/index.js'
|
|
6
5
|
import type { SugaredRelativeEntityList } from '@contember/binding'
|
|
7
6
|
import { useAccessorUpdateSubscription } from './useAccessorUpdateSubscription.js'
|
|
7
|
+
import { useOnHasOneRelationPathConnectionUpdate } from './useOnHasOneRelationPathConnectionUpdate.js'
|
|
8
8
|
import { useDesugaredRelativeEntityList } from './useDesugaredRelativeEntityList.js'
|
|
9
9
|
|
|
10
10
|
function useEntityList(sugaredRelativeEntityList: string | SugaredRelativeEntityList): EntityListAccessor
|
|
@@ -34,9 +34,7 @@ function useEntityList(
|
|
|
34
34
|
)
|
|
35
35
|
const [entityList, { update }] = useAccessorUpdateSubscription(getEntityList)
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
useOnConnectionUpdate(relativeEntityList.hasOneRelationPath[0].field, update)
|
|
39
|
-
}
|
|
37
|
+
useOnHasOneRelationPathConnectionUpdate(relativeEntityList.hasOneRelationPath, update)
|
|
40
38
|
|
|
41
39
|
return entityList
|
|
42
40
|
}
|
|
@@ -2,9 +2,9 @@ import { useConstantLengthInvariant, useConstantValueInvariant } from '@contembe
|
|
|
2
2
|
import { useCallback } from 'react'
|
|
3
3
|
import { useEntityKey, useGetEntityByKey } from './index.js'
|
|
4
4
|
import type { FieldAccessor } from '@contember/binding'
|
|
5
|
-
import { useOnConnectionUpdate } from '../entityEvents/index.js'
|
|
6
5
|
import type { FieldValue, SugaredRelativeSingleField } from '@contember/binding'
|
|
7
6
|
import { useAccessorUpdateSubscription } from './useAccessorUpdateSubscription.js'
|
|
7
|
+
import { useOnHasOneRelationPathConnectionUpdate } from './useOnHasOneRelationPathConnectionUpdate.js'
|
|
8
8
|
import { useDesugaredRelativeSingleField } from './useDesugaredRelativeSingleField.js'
|
|
9
9
|
|
|
10
10
|
function useField<Value extends FieldValue = FieldValue>(
|
|
@@ -36,9 +36,7 @@ function useField<Value extends FieldValue = FieldValue>(
|
|
|
36
36
|
)
|
|
37
37
|
const [field, { update }] = useAccessorUpdateSubscription(getField)
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
useOnConnectionUpdate(relativeSingleField.hasOneRelationPath[0].field, update)
|
|
41
|
-
}
|
|
39
|
+
useOnHasOneRelationPathConnectionUpdate(relativeSingleField.hasOneRelationPath, update)
|
|
42
40
|
|
|
43
41
|
return field
|
|
44
42
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { EntityAccessor, HasOneRelation } from '@contember/binding'
|
|
2
|
+
import { useObjectMemo } from '@contember/react-utils'
|
|
3
|
+
import { useEffect } from 'react'
|
|
4
|
+
import { useEntityKey } from './useEntityKey.js'
|
|
5
|
+
import { useGetEntityByKey } from './useGetEntityByKey.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Calls `update` when any has-one relation along the path is reconnected, not only the first one.
|
|
9
|
+
* Each hop is watched on the entity it currently hangs off, so the listeners move once an earlier hop is reconnected.
|
|
10
|
+
*/
|
|
11
|
+
export const useOnHasOneRelationPathConnectionUpdate = (hasOneRelationPath: HasOneRelation[], update: () => void): void => {
|
|
12
|
+
const entityKey = useEntityKey()
|
|
13
|
+
const getEntityByKey = useGetEntityByKey()
|
|
14
|
+
|
|
15
|
+
// A getter is stable per entity realm, so this array changes exactly when a hop resolves to another entity.
|
|
16
|
+
const hopParentGetters: EntityAccessor.GetEntityAccessor[] = []
|
|
17
|
+
if (hasOneRelationPath.length > 0) {
|
|
18
|
+
let hopParent = getEntityByKey(entityKey)
|
|
19
|
+
for (const [index, hop] of hasOneRelationPath.entries()) {
|
|
20
|
+
hopParentGetters.push(hopParent.getAccessor)
|
|
21
|
+
if (index < hasOneRelationPath.length - 1) {
|
|
22
|
+
hopParent = hopParent.getEntity({ hasOneRelationPath: [hop] })
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const stableHopParentGetters = useObjectMemo(hopParentGetters)
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
const unsubscribers = stableHopParentGetters.map((getHopParent, index) =>
|
|
30
|
+
getHopParent().addEventListener({ type: 'connectionUpdate', key: hasOneRelationPath[index].field }, update)
|
|
31
|
+
)
|
|
32
|
+
return () => {
|
|
33
|
+
for (const unsubscribe of unsubscribers) {
|
|
34
|
+
unsubscribe()
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}, [hasOneRelationPath, stableHopParentGetters, update])
|
|
38
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'bun:test'
|
|
2
|
+
import { screen } from '@testing-library/react'
|
|
3
|
+
import { c, createSchema } from '@contember/schema-definition'
|
|
4
|
+
import { ReactNode, useCallback } from 'react'
|
|
5
|
+
import {
|
|
6
|
+
Component,
|
|
7
|
+
EntitySubTree,
|
|
8
|
+
Field,
|
|
9
|
+
HasMany,
|
|
10
|
+
HasOne,
|
|
11
|
+
useAccessorUpdateSubscription,
|
|
12
|
+
useEntity,
|
|
13
|
+
useEntityKey,
|
|
14
|
+
useEntityList,
|
|
15
|
+
useField,
|
|
16
|
+
useGetEntityByKey,
|
|
17
|
+
} from '../../../../src/index.js'
|
|
18
|
+
import { convertModelToAdminSchema } from '../../../lib/convertModelToAdminSchema.js'
|
|
19
|
+
import { BindingHarness, createBindingHarness } from '../../../lib/harness/index.js'
|
|
20
|
+
|
|
21
|
+
namespace Model {
|
|
22
|
+
export class Item {
|
|
23
|
+
link = c.manyHasOne(Link)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class Link {
|
|
27
|
+
target = c.manyHasOne(Page)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class Page {
|
|
31
|
+
url = c.stringColumn()
|
|
32
|
+
blocks = c.oneHasMany(Block, 'page')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class Block {
|
|
36
|
+
label = c.stringColumn()
|
|
37
|
+
page = c.manyHasOne(Page, 'blocks')
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const schema = convertModelToAdminSchema(createSchema(Model).model)
|
|
42
|
+
const itemId = 'aaaaaaaa-0000-0000-0000-000000000001'
|
|
43
|
+
const linkAId = 'bbbbbbbb-0000-0000-0000-000000000001'
|
|
44
|
+
const linkBId = 'bbbbbbbb-0000-0000-0000-000000000002'
|
|
45
|
+
const pageAId = 'cccccccc-0000-0000-0000-000000000001'
|
|
46
|
+
const pageBId = 'cccccccc-0000-0000-0000-000000000002'
|
|
47
|
+
const pageCId = 'cccccccc-0000-0000-0000-000000000003'
|
|
48
|
+
const blockAId = 'dddddddd-0000-0000-0000-000000000001'
|
|
49
|
+
const blockBId = 'dddddddd-0000-0000-0000-000000000002'
|
|
50
|
+
const blockCId = 'dddddddd-0000-0000-0000-000000000003'
|
|
51
|
+
|
|
52
|
+
const pageFields = (
|
|
53
|
+
<>
|
|
54
|
+
<Field field="url" />
|
|
55
|
+
<HasMany field="blocks">
|
|
56
|
+
<Field field="label" />
|
|
57
|
+
</HasMany>
|
|
58
|
+
</>
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
// Every hook resolves a path whose second hop is `target`, starting from the item.
|
|
62
|
+
const FieldProbe = Component(
|
|
63
|
+
() => <span data-testid="field">{useField<string>('link.target.url').value ?? 'null'}</span>,
|
|
64
|
+
() => (
|
|
65
|
+
<HasOne field="link">
|
|
66
|
+
<HasOne field="target">
|
|
67
|
+
<Field field="url" />
|
|
68
|
+
</HasOne>
|
|
69
|
+
</HasOne>
|
|
70
|
+
),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
const EntityProbe = Component(
|
|
74
|
+
() => <span data-testid="entity">{useEntity('link.target').getField<string>('url').value ?? 'null'}</span>,
|
|
75
|
+
() => (
|
|
76
|
+
<HasOne field="link">
|
|
77
|
+
<HasOne field="target">
|
|
78
|
+
<Field field="url" />
|
|
79
|
+
</HasOne>
|
|
80
|
+
</HasOne>
|
|
81
|
+
),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
const EntityListProbe = Component(
|
|
85
|
+
() => {
|
|
86
|
+
const blocks = useEntityList('link.target.blocks')
|
|
87
|
+
return <span data-testid="list">{Array.from(blocks, it => it.getField<string>('label').value).join(',')}</span>
|
|
88
|
+
},
|
|
89
|
+
() => (
|
|
90
|
+
<HasOne field="link">
|
|
91
|
+
<HasOne field="target">
|
|
92
|
+
<HasMany field="blocks">
|
|
93
|
+
<Field field="label" />
|
|
94
|
+
</HasMany>
|
|
95
|
+
</HasOne>
|
|
96
|
+
</HasOne>
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
const page = (id: string, url: string, blockId: string, label: string) => ({ id, url, blocks: [{ id: blockId, label }] })
|
|
101
|
+
|
|
102
|
+
const probes = (
|
|
103
|
+
<>
|
|
104
|
+
<FieldProbe />
|
|
105
|
+
<EntityProbe />
|
|
106
|
+
<EntityListProbe />
|
|
107
|
+
</>
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
const mount = (itemChildren: ReactNode = probes) =>
|
|
111
|
+
createBindingHarness({
|
|
112
|
+
schema,
|
|
113
|
+
data: {
|
|
114
|
+
item: { id: itemId, link: { id: linkAId, target: page(pageAId, 'A', blockAId, 'a') } },
|
|
115
|
+
linkB: { id: linkBId, target: page(pageCId, 'C', blockCId, 'c') },
|
|
116
|
+
pageB: page(pageBId, 'B', blockBId, 'b'),
|
|
117
|
+
},
|
|
118
|
+
node: (
|
|
119
|
+
<>
|
|
120
|
+
<EntitySubTree entity={`Item(id = '${itemId}')`} alias="item">
|
|
121
|
+
{itemChildren}
|
|
122
|
+
</EntitySubTree>
|
|
123
|
+
<EntitySubTree entity={`Link(id = '${linkBId}')`} alias="linkB">
|
|
124
|
+
<HasOne field="target">{pageFields}</HasOne>
|
|
125
|
+
</EntitySubTree>
|
|
126
|
+
<EntitySubTree entity={`Page(id = '${pageBId}')`} alias="pageB">
|
|
127
|
+
{pageFields}
|
|
128
|
+
</EntitySubTree>
|
|
129
|
+
</>
|
|
130
|
+
),
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const rendered = () => ({
|
|
134
|
+
field: screen.getByTestId('field').textContent,
|
|
135
|
+
entity: screen.getByTestId('entity').textContent,
|
|
136
|
+
list: screen.getByTestId('list').textContent,
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
let harness: BindingHarness | undefined
|
|
140
|
+
afterEach(() => {
|
|
141
|
+
harness?.unmount()
|
|
142
|
+
harness = undefined
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
describe('hooks resolving a path through a reconnected has-one relation', () => {
|
|
146
|
+
it('follow a reconnect of the second relation in the path', async () => {
|
|
147
|
+
const h = (harness = await mount())
|
|
148
|
+
expect(rendered()).toEqual({ field: 'A', entity: 'A', list: 'a' })
|
|
149
|
+
|
|
150
|
+
await h.update(() => {
|
|
151
|
+
h.getEntity('item').getEntity('link').connectEntityAtField('target', h.getEntity('pageB'))
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
expect(rendered()).toEqual({ field: 'B', entity: 'B', list: 'b' })
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('follow edits of the newly connected entity', async () => {
|
|
158
|
+
const h = (harness = await mount())
|
|
159
|
+
await h.update(() => {
|
|
160
|
+
h.getEntity('item').getEntity('link').connectEntityAtField('target', h.getEntity('pageB'))
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
await h.update(() => {
|
|
164
|
+
const target = h.getEntity('item').getEntity('link').getEntity('target')
|
|
165
|
+
target.getField('url').updateValue('B2')
|
|
166
|
+
target.getEntityList('blocks').getChildEntityById(blockBId).getField('label').updateValue('b2')
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
expect(rendered()).toEqual({ field: 'B2', entity: 'B2', list: 'b2' })
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('stop following the disconnected entity', async () => {
|
|
173
|
+
const h = (harness = await mount())
|
|
174
|
+
await h.update(() => {
|
|
175
|
+
h.getEntity('item').getEntity('link').connectEntityAtField('target', h.getEntity('pageB'))
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
await h.update(() => {
|
|
179
|
+
h.getEntity('item').getEntity('link').disconnectEntityAtField('target')
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
expect(rendered()).toEqual({ field: 'null', entity: 'null', list: '' })
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
it('follow edits made in the same update as the reconnect', async () => {
|
|
186
|
+
const h = (harness = await mount())
|
|
187
|
+
await h.update(() => {
|
|
188
|
+
const link = h.getEntity('item').getEntity('link')
|
|
189
|
+
link.connectEntityAtField('target', h.getEntity('pageB'))
|
|
190
|
+
const target = link.getEntity('target')
|
|
191
|
+
target.getField('url').updateValue('B2')
|
|
192
|
+
target.getEntityList('blocks').getChildEntityById(blockBId).getField('label').updateValue('b2')
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
expect(rendered()).toEqual({ field: 'B2', entity: 'B2', list: 'b2' })
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('follow a reconnect of the first relation and then of the second one', async () => {
|
|
199
|
+
const h = (harness = await mount())
|
|
200
|
+
await h.update(() => {
|
|
201
|
+
h.getEntity('item').connectEntityAtField('link', h.getEntity('linkB'))
|
|
202
|
+
})
|
|
203
|
+
expect(rendered()).toEqual({ field: 'C', entity: 'C', list: 'c' })
|
|
204
|
+
|
|
205
|
+
await h.update(() => {
|
|
206
|
+
h.getEntity('item').getEntity('link').connectEntityAtField('target', h.getEntity('pageB'))
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
expect(rendered()).toEqual({ field: 'B', entity: 'B', list: 'b' })
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
describe('connectionUpdate of a reconnected has-one relation', () => {
|
|
214
|
+
it('fires once per reconnect, not on later updates through the same entity', async () => {
|
|
215
|
+
const h = (harness = await mount())
|
|
216
|
+
let connectionUpdates = 0
|
|
217
|
+
h.getEntity('item').getEntity('link').addEventListener({ type: 'connectionUpdate', key: 'target' }, () => {
|
|
218
|
+
connectionUpdates++
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
await h.update(() => {
|
|
222
|
+
h.getEntity('item').getEntity('link').connectEntityAtField('target', h.getEntity('pageB'))
|
|
223
|
+
})
|
|
224
|
+
expect(connectionUpdates).toBe(1)
|
|
225
|
+
|
|
226
|
+
await h.update(() => {
|
|
227
|
+
h.getEntity('item').getEntity('link').getEntity('target').getField('url').updateValue('B2')
|
|
228
|
+
})
|
|
229
|
+
expect(connectionUpdates).toBe(1)
|
|
230
|
+
|
|
231
|
+
await h.update(() => {
|
|
232
|
+
h.getEntity('item').getEntity('link').disconnectEntityAtField('target')
|
|
233
|
+
})
|
|
234
|
+
expect(connectionUpdates).toBe(2)
|
|
235
|
+
})
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
describe('useAccessorUpdateSubscription', () => {
|
|
239
|
+
// Kept outside React so the test can ask the probe to re-resolve its getter, as a connectionUpdate listener would.
|
|
240
|
+
let refreshProbe: (() => void) | undefined
|
|
241
|
+
afterEach(() => {
|
|
242
|
+
refreshProbe = undefined
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
const SubscriptionProbe = Component(
|
|
246
|
+
() => {
|
|
247
|
+
const entityKey = useEntityKey()
|
|
248
|
+
const getEntityByKey = useGetEntityByKey()
|
|
249
|
+
const getUrl = useCallback(() => getEntityByKey(entityKey).getField<string>('link.target.url'), [entityKey, getEntityByKey])
|
|
250
|
+
const [url, { update }] = useAccessorUpdateSubscription(getUrl)
|
|
251
|
+
refreshProbe = update
|
|
252
|
+
return <span data-testid="subscription">{url.value ?? 'null'}</span>
|
|
253
|
+
},
|
|
254
|
+
() => (
|
|
255
|
+
<HasOne field="link">
|
|
256
|
+
<HasOne field="target">
|
|
257
|
+
<Field field="url" />
|
|
258
|
+
</HasOne>
|
|
259
|
+
</HasOne>
|
|
260
|
+
),
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
it('follows updates of the accessor the getter resolves to now, not of the one from mount', async () => {
|
|
264
|
+
const h = (harness = await mount(<SubscriptionProbe />))
|
|
265
|
+
await h.update(() => {
|
|
266
|
+
h.getEntity('item').getEntity('link').connectEntityAtField('target', h.getEntity('pageB'))
|
|
267
|
+
if (refreshProbe === undefined) {
|
|
268
|
+
throw new Error('SubscriptionProbe has not rendered.')
|
|
269
|
+
}
|
|
270
|
+
refreshProbe()
|
|
271
|
+
})
|
|
272
|
+
expect(screen.getByTestId('subscription').textContent).toBe('B')
|
|
273
|
+
|
|
274
|
+
await h.update(() => {
|
|
275
|
+
h.getEntity('item').getEntity('link').getEntity('target').getField('url').updateValue('B2')
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
expect(screen.getByTestId('subscription').textContent).toBe('B2')
|
|
279
|
+
})
|
|
280
|
+
})
|