@@ -5,47 +5,55 @@ import $rdf from 'rdf-ext'
5
5
import { expect } from 'chai'
6
6
import * as sinon from 'sinon'
7
7
import { setObjectValue , setPropertyObjects } from '../../../../models/forms/reducers/updateObject'
8
- import { RecursivePartial , testFocusNodeState , testObjectState , testState , testStore } from '../util'
8
+ import { RecursivePartial , testObjectState , testStore } from '../util'
9
9
import { propertyShape } from '../../../util'
10
10
import { Store } from '../../../../state'
11
11
import { FormState } from '../../../../models/forms'
12
12
13
13
const ex = ns ( 'http://example.com/' )
14
14
15
15
describe ( 'core/models/forms/reducers/updateObject' , ( ) => {
16
+ let store : Store
17
+ let form : symbol
18
+ let formState : {
19
+ focusNodes : RecursivePartial < FormState [ 'focusNodes' ] >
20
+ focusStack : FormState [ 'focusStack' ]
21
+ }
22
+
23
+ beforeEach ( ( ) => {
24
+ ( { form, store } = testStore ( ) )
25
+ formState = store . getState ( ) . forms . get ( form ) !
26
+ } )
27
+
16
28
describe ( 'setPropertyObjects' , ( ) => {
17
- it ( 'removes all current triples and creates new' , ( ) => {
29
+ it ( 'removes all current objects and creates new' , ( ) => {
18
30
// given
19
31
const graph = cf ( { dataset : $rdf . dataset ( ) } )
20
32
const focusNode = graph . node ( ex . FocusNode )
21
33
. addOut ( ex . prop , [ 'foo1' , 'foo2' ] )
22
34
const property = propertyShape ( graph . blankNode ( ) , {
23
35
path : ex . prop ,
24
36
} )
25
- const { form, state } = testState ( {
26
- form : {
27
- focusNodes : {
28
- ...testFocusNodeState ( focusNode , {
29
- properties : [ {
30
- shape : property ,
31
- objects : [ ] ,
32
- name : 'prop' ,
33
- canRemove : true ,
34
- canAdd : true ,
35
- editors : [ ] ,
36
- selectedEditor : undefined ,
37
- } ] ,
38
- } ) ,
39
- } ,
37
+ formState . focusNodes = {
38
+ [ focusNode . value ] : {
39
+ properties : [ {
40
+ shape : property ,
41
+ objects : [ ] ,
42
+ name : 'prop' ,
43
+ canRemove : true ,
44
+ canAdd : true ,
45
+ editors : [ ] ,
46
+ selectedEditor : undefined ,
47
+ } ] ,
40
48
} ,
41
- } )
49
+ }
42
50
43
51
// when
44
- const after = setPropertyObjects ( state , {
52
+ const after = setPropertyObjects ( store . getState ( ) . forms , {
45
53
focusNode,
46
54
form,
47
55
property,
48
- editors : testStore ( ) . store . getState ( ) . editors ,
56
+ editors : store . getState ( ) . editors ,
49
57
objects : graph . node ( [ $rdf . literal ( 'bar1' ) , $rdf . literal ( 'bar2' ) , $rdf . literal ( 'bar3' ) ] ) ,
50
58
} )
51
59
@@ -55,21 +63,102 @@ describe('core/models/forms/reducers/updateObject', () => {
55
63
expect ( values ) . to . have . length ( 3 )
56
64
expect ( values ) . to . include . members ( [ 'bar1' , 'bar2' , 'bar3' ] )
57
65
} )
58
- } )
59
66
60
- describe ( 'setObjectValue' , ( ) => {
61
- let store : Store
62
- let form : symbol
63
- let formState : {
64
- focusNodes : RecursivePartial < FormState [ 'focusNodes' ] >
65
- focusStack : FormState [ 'focusStack' ]
66
- }
67
-
68
- beforeEach ( ( ) => {
69
- ( { form, store } = testStore ( ) )
70
- formState = store . getState ( ) . forms . get ( form ) !
67
+ it ( 'flips canAdd flag when max reached' , ( ) => {
68
+ // given
69
+ const graph = cf ( { dataset : $rdf . dataset ( ) } )
70
+ const focusNode = graph . node ( ex . FocusNode )
71
+ const property = propertyShape ( graph . blankNode ( ) , {
72
+ path : ex . prop ,
73
+ maxCount : 2 ,
74
+ } )
75
+ formState . focusNodes = {
76
+ [ focusNode . value ] : {
77
+ properties : [ {
78
+ shape : property ,
79
+ canAdd : true ,
80
+ } ] ,
81
+ } ,
82
+ }
83
+
84
+ // when
85
+ const after = setPropertyObjects ( store . getState ( ) . forms , {
86
+ focusNode,
87
+ form,
88
+ property,
89
+ editors : testStore ( ) . store . getState ( ) . editors ,
90
+ objects : graph . node ( [ $rdf . literal ( 'foo' ) , $rdf . literal ( 'bar' ) ] ) ,
91
+ } )
92
+
93
+ // then
94
+ expect ( after . get ( form ) ?. focusNodes [ focusNode . value ] . properties [ 0 ] . canAdd ) . to . be . false
95
+ } )
96
+
97
+ it ( 'sets canRemove flag when min reached' , ( ) => {
98
+ // given
99
+ const graph = cf ( { dataset : $rdf . dataset ( ) } )
100
+ const focusNode = graph . node ( ex . FocusNode )
101
+ const property = propertyShape ( graph . blankNode ( ) , {
102
+ path : ex . prop ,
103
+ minCount : 2 ,
104
+ } )
105
+ formState . focusNodes = {
106
+ [ focusNode . value ] : {
107
+ properties : [ {
108
+ shape : property ,
109
+ canRemove : true ,
110
+ } ] ,
111
+ } ,
112
+ }
113
+
114
+ // when
115
+ const after = setPropertyObjects ( store . getState ( ) . forms , {
116
+ focusNode,
117
+ form,
118
+ property,
119
+ editors : testStore ( ) . store . getState ( ) . editors ,
120
+ objects : graph . node ( [ $rdf . literal ( 'foo' ) ] ) ,
121
+ } )
122
+
123
+ // then
124
+ expect ( after . get ( form ) ?. focusNodes [ focusNode . value ] . properties [ 0 ] . canRemove ) . to . be . false
71
125
} )
72
126
127
+ it ( 'sets canAdd/canRemove flags to true' , ( ) => {
128
+ // given
129
+ const graph = cf ( { dataset : $rdf . dataset ( ) } )
130
+ const focusNode = graph . node ( ex . FocusNode )
131
+ const property = propertyShape ( graph . blankNode ( ) , {
132
+ path : ex . prop ,
133
+ minCount : 1 ,
134
+ maxCount : 4 ,
135
+ } )
136
+ formState . focusNodes = {
137
+ [ focusNode . value ] : {
138
+ properties : [ {
139
+ shape : property ,
140
+ canAdd : false ,
141
+ canRemove : false ,
142
+ } ] ,
143
+ } ,
144
+ }
145
+
146
+ // when
147
+ const after = setPropertyObjects ( store . getState ( ) . forms , {
148
+ focusNode,
149
+ form,
150
+ property,
151
+ editors : testStore ( ) . store . getState ( ) . editors ,
152
+ objects : graph . node ( [ $rdf . literal ( 'foo' ) , $rdf . literal ( 'bar' ) ] ) ,
153
+ } )
154
+
155
+ // then
156
+ expect ( after . get ( form ) ?. focusNodes [ focusNode . value ] . properties [ 0 ] . canAdd ) . to . be . true
157
+ expect ( after . get ( form ) ?. focusNodes [ focusNode . value ] . properties [ 0 ] . canRemove ) . to . be . true
158
+ } )
159
+ } )
160
+
161
+ describe ( 'setObjectValue' , ( ) => {
73
162
it ( 'recalculates editors' , ( ) => {
74
163
// given
75
164
const graph = cf ( { dataset : $rdf . dataset ( ) } )
0 commit comments