1- import { createForm } from '@formily/core'
1+ import { createForm , isField } from '@formily/core'
22import { Schema } from '@formily/json-schema'
3- import { render } from '@testing-library/vue'
3+ import { render , waitFor } from '@testing-library/vue'
44import Vue , { FunctionalComponentOptions } from 'vue'
55import { FormProvider , createSchemaField } from '../vue2-components'
66
@@ -21,6 +21,21 @@ const Input: FunctionalComponentOptions = {
2121 } ,
2222}
2323
24+ const Input2 : FunctionalComponentOptions = {
25+ functional : true ,
26+ render ( h , context ) {
27+ return h ( 'input' , {
28+ attrs : {
29+ value : context . props . value ,
30+ 'data-testid' : 'input2' ,
31+ } ,
32+ on : {
33+ input : context . listeners . change ,
34+ } ,
35+ } )
36+ } ,
37+ }
38+
2439const Previewer : FunctionalComponentOptions = {
2540 functional : true ,
2641 render ( h , context ) {
@@ -296,3 +311,103 @@ describe('x-content', () => {
296311 expect ( queryByTestId ( 'previewer2' ) . textContent ) . toEqual ( '123' )
297312 } )
298313} )
314+
315+ describe ( 'scope' , ( ) => {
316+ test ( 'scope in <SchemaField> prop' , async ( ) => {
317+ const form = createForm ( )
318+ const { SchemaField } = createSchemaField ( {
319+ components : {
320+ Input,
321+ Input2,
322+ Previewer,
323+ } ,
324+ } )
325+ const { queryByTestId } = render ( {
326+ components : { SchemaField } ,
327+ data ( ) {
328+ return {
329+ form,
330+ schema : {
331+ type : 'object' ,
332+ properties : {
333+ input1 : {
334+ type : 'string' ,
335+ 'x-component' : 'Input' ,
336+ 'x-reactions' : {
337+ target : 'input2' ,
338+ fulfill : {
339+ state : {
340+ value : '{{ test }}' ,
341+ } ,
342+ } ,
343+ } ,
344+ } ,
345+ input2 : {
346+ type : 'string' ,
347+ 'x-component' : 'Input2' ,
348+ } ,
349+ } ,
350+ } ,
351+ }
352+ } ,
353+ template : `<FormProvider :form="form">
354+ <SchemaField
355+ :schema="schema"
356+ :scope="{
357+ test: '123'
358+ }"
359+ />
360+ </FormProvider>` ,
361+ } )
362+ expect ( queryByTestId ( 'input2' ) . getAttribute ( 'value' ) ) . toEqual ( '123' )
363+ } )
364+
365+ test ( 'scope in options of createSchemaField' , async ( ) => {
366+ const form = createForm ( )
367+ const { SchemaField } = createSchemaField ( {
368+ components : {
369+ Input,
370+ Input2,
371+ Previewer,
372+ } ,
373+ scope : {
374+ test : '123' ,
375+ } ,
376+ } )
377+ const { queryByTestId } = render ( {
378+ components : { SchemaField } ,
379+ data ( ) {
380+ return {
381+ form,
382+ schema : {
383+ type : 'object' ,
384+ properties : {
385+ input1 : {
386+ type : 'string' ,
387+ 'x-component' : 'Input' ,
388+ 'x-reactions' : {
389+ target : 'input2' ,
390+ fulfill : {
391+ state : {
392+ value : '{{ test }}' ,
393+ } ,
394+ } ,
395+ } ,
396+ } ,
397+ input2 : {
398+ type : 'string' ,
399+ 'x-component' : 'Input2' ,
400+ } ,
401+ } ,
402+ } ,
403+ }
404+ } ,
405+ template : `<FormProvider :form="form">
406+ <SchemaField
407+ :schema="schema"
408+ />
409+ </FormProvider>` ,
410+ } )
411+ expect ( queryByTestId ( 'input2' ) . getAttribute ( 'value' ) ) . toEqual ( '123' )
412+ } )
413+ } )
0 commit comments