@@ -3,10 +3,12 @@ import { renderHook } from '@testing-library/react';
3
3
import { overrideAjax } from '../../../tests/ajax' ;
4
4
import { mockTime , requireContext } from '../../../tests/helpers' ;
5
5
import { overwriteReadOnly } from '../../../utils/types' ;
6
+ import type { SerializedResource } from '../helperTypes' ;
6
7
import { getResourceApiUrl } from '../resource' ;
7
8
import { useSaveBlockers } from '../saveBlockers' ;
8
9
import { schema } from '../schema' ;
9
10
import { tables } from '../tables' ;
11
+ import type { Taxon , TaxonTreeDefItem } from '../types' ;
10
12
11
13
mockTime ( ) ;
12
14
requireContext ( ) ;
@@ -180,3 +182,128 @@ describe('uniqueness rules', () => {
180
182
] ) ;
181
183
} ) ;
182
184
} ) ;
185
+
186
+ describe ( 'treeBusinessRules' , ( ) => {
187
+ const animaliaResponse : Partial < SerializedResource < Taxon > > = {
188
+ _tableName : 'Taxon' ,
189
+ id : 2 ,
190
+ fullName : 'Animalia' ,
191
+ parent : '/api/specify/taxon/1/' ,
192
+ definition : '/api/specify/taxontreedef/1/' ,
193
+ definitionItem : '/api/specify/taxontreedefitem/21/' ,
194
+ rankId : 10 ,
195
+ } ;
196
+ const acipenserResponse : Partial < SerializedResource < Taxon > > = {
197
+ _tableName : 'Taxon' ,
198
+ id : 3 ,
199
+ name : 'Acipenser' ,
200
+ rankId : 180 ,
201
+ definition : '/api/specify/taxontreedef/1/' ,
202
+ definitionItem : '/api/specify/taxontreedefitem/9/' ,
203
+ parent : '/api/specify/taxon/2/' ,
204
+ } ;
205
+
206
+ const oxyrinchusSpeciesResponse : Partial < SerializedResource < Taxon > > = {
207
+ _tableName : 'Taxon' ,
208
+ id : 4 ,
209
+ name : 'oxyrinchus' ,
210
+ rankId : 220 ,
211
+ definition : '/api/specify/taxontreedef/1/' ,
212
+ definitionItem : '/api/specify/taxontreedefitem/2/' ,
213
+ parent : '/api/specify/taxon/3/' ,
214
+ } ;
215
+
216
+ const oxyrinchusSubSpeciesResponse : Partial < SerializedResource < Taxon > > = {
217
+ _tableName : 'Taxon' ,
218
+ id : 5 ,
219
+ rankId : 230 ,
220
+ name : 'oxyrinchus' ,
221
+ definition : '/api/specify/taxontreedef/1/' ,
222
+ definitionItem : '/api/specify/taxontreedefitem/22/' ,
223
+ } ;
224
+
225
+ const genusResponse : Partial < SerializedResource < TaxonTreeDefItem > > = {
226
+ _tableName : 'TaxonTreeDefItem' ,
227
+ id : 9 ,
228
+ fullNameSeparator : ' ' ,
229
+ isEnforced : true ,
230
+ isInFullName : true ,
231
+ name : 'Genus' ,
232
+ rankId : 180 ,
233
+ title : 'Genus' ,
234
+ parent : '/api/specify/taxontreedefitem/8/' ,
235
+ treeDef : '/api/specify/taxontreedef/1/' ,
236
+ resource_uri : '/api/specify/taxontreedefitem/9/' ,
237
+ } ;
238
+
239
+ const speciesResponse : Partial < SerializedResource < TaxonTreeDefItem > > = {
240
+ id : 2 ,
241
+ fullNameSeparator : ' ' ,
242
+ isEnforced : false ,
243
+ isInFullName : true ,
244
+ name : 'Species' ,
245
+ rankId : 220 ,
246
+ title : null ,
247
+ version : 2 ,
248
+ parent : '/api/specify/taxontreedefitem/15/' ,
249
+ treeDef : '/api/specify/taxontreedef/1/' ,
250
+ resource_uri : '/api/specify/taxontreedefitem/2/' ,
251
+ } ;
252
+
253
+ const oxyrinchusFullNameResponse = 'Acipenser oxyrinchus' ;
254
+
255
+ overrideAjax ( '/api/specify/taxon/2/' , animaliaResponse ) ;
256
+ overrideAjax ( '/api/specify/taxon/3/' , acipenserResponse ) ;
257
+ overrideAjax ( '/api/specify/taxon/4/' , oxyrinchusSpeciesResponse ) ;
258
+ overrideAjax ( '/api/specify/taxon/5/' , oxyrinchusSubSpeciesResponse ) ;
259
+ overrideAjax ( '/api/specify/taxontreedefitem/9/' , genusResponse ) ;
260
+ overrideAjax ( '/api/specify/taxontreedefitem/2/' , speciesResponse ) ;
261
+ overrideAjax (
262
+ '/api/specify_tree/taxon/3/predict_fullname/?name=oxyrinchus&treedefitemid=2' ,
263
+ oxyrinchusFullNameResponse
264
+ ) ;
265
+
266
+ test ( 'fullName being set' , async ( ) => {
267
+ const oxyrinchus = new tables . Taxon . Resource ( {
268
+ _tableName : 'Taxon' ,
269
+ id : 4 ,
270
+ } ) ;
271
+ await oxyrinchus . fetch ( ) ;
272
+ await oxyrinchus . businessRuleManager ?. checkField ( 'parent' ) ;
273
+ expect ( oxyrinchus . get ( 'fullName' ) ) . toBe ( 'Acipenser oxyrinchus' ) ;
274
+ } ) ;
275
+
276
+ test ( 'parent blocking on invalid rank' , async ( ) => {
277
+ const taxon = new tables . Taxon . Resource ( {
278
+ name : 'Ameiurus' ,
279
+ parent : '/api/specify/taxon/3/' ,
280
+ rankid : 180 ,
281
+ definition : '/api/specify/taxontreedef/1/' ,
282
+ definitionitem : '/api/specify/taxontreedefitem/9/' ,
283
+ } ) ;
284
+
285
+ await taxon . businessRuleManager ?. checkField ( 'parent' ) ;
286
+
287
+ const { result } = renderHook ( ( ) =>
288
+ useSaveBlockers ( taxon , tables . Taxon . getField ( 'parent' ) )
289
+ ) ;
290
+ expect ( result . current [ 0 ] ) . toStrictEqual ( [ 'Bad tree structure.' ] ) ;
291
+ } ) ;
292
+
293
+ test ( 'parent blocking on invalid parent' , async ( ) => {
294
+ const taxon = new tables . Taxon . Resource ( {
295
+ name : 'Ameiurus' ,
296
+ parent : '/api/specify/taxon/5/' ,
297
+ rankid : 180 ,
298
+ definition : '/api/specify/taxontreedef/1/' ,
299
+ definitionitem : '/api/specify/taxontreedefitem/9/' ,
300
+ } ) ;
301
+
302
+ await taxon . businessRuleManager ?. checkField ( 'parent' ) ;
303
+
304
+ const { result } = renderHook ( ( ) =>
305
+ useSaveBlockers ( taxon , tables . Taxon . getField ( 'parent' ) )
306
+ ) ;
307
+ expect ( result . current [ 0 ] ) . toStrictEqual ( [ 'Bad tree structure.' ] ) ;
308
+ } ) ;
309
+ } ) ;
0 commit comments