1
1
import { palette } from '@leafygreen-ui/palette' ;
2
2
import { ComponentProps } from 'react' ;
3
+ import { userEvent } from '@testing-library/user-event' ;
3
4
4
5
import { render , screen } from '@/mocks/testing-utils' ;
5
6
import { Field as FieldComponent } from '@/components/field/field' ;
6
7
import { DEFAULT_PREVIEW_GROUP_AREA } from '@/utilities/get-preview-group-area' ;
7
- import { FieldSelectionProvider } from '@/hooks/use-field-selection ' ;
8
+ import { EditableDiagramInteractionsProvider } from '@/hooks/use-editable-diagram-interactions ' ;
8
9
9
10
const Field = ( props : React . ComponentProps < typeof FieldComponent > ) => (
10
- < FieldSelectionProvider >
11
+ < EditableDiagramInteractionsProvider >
11
12
< FieldComponent { ...props } />
12
- </ FieldSelectionProvider >
13
+ </ EditableDiagramInteractionsProvider >
13
14
) ;
14
15
16
+ const FieldWithEditableInteractions = ( {
17
+ onAddFieldToObjectFieldClick,
18
+ ...fieldProps
19
+ } : React . ComponentProps < typeof FieldComponent > & {
20
+ onAddFieldToObjectFieldClick ?: ( ) => void ;
21
+ } ) => {
22
+ return (
23
+ < EditableDiagramInteractionsProvider onAddFieldToObjectFieldClick = { onAddFieldToObjectFieldClick } >
24
+ < FieldComponent { ...fieldProps } />
25
+ </ EditableDiagramInteractionsProvider >
26
+ ) ;
27
+ } ;
28
+
15
29
describe ( 'field' , ( ) => {
16
30
const DEFAULT_PROPS : ComponentProps < typeof Field > = {
17
31
nodeType : 'collection' ,
@@ -30,6 +44,52 @@ describe('field', () => {
30
44
expect ( screen . getByRole ( 'img' , { name : 'Key Icon' } ) ) . toBeInTheDocument ( ) ;
31
45
expect ( screen . getByRole ( 'img' , { name : 'Link Icon' } ) ) . toBeInTheDocument ( ) ;
32
46
} ) ;
47
+ it ( 'Should not have a button to add a field on an object type' , ( ) => {
48
+ render ( < Field { ...DEFAULT_PROPS } type = { 'object' } id = { [ 'ordersId' ] } /> ) ;
49
+ expect ( screen . getByText ( 'ordersId' ) ) . toBeInTheDocument ( ) ;
50
+ expect ( screen . getByText ( '{}' ) ) . toBeInTheDocument ( ) ;
51
+ const button = screen . queryByRole ( 'button' ) ;
52
+ expect ( button ) . not . toBeInTheDocument ( ) ;
53
+ } ) ;
54
+ describe ( 'With editable interactions supplied' , ( ) => {
55
+ it ( 'Should have a button to add a field on an object type' , async ( ) => {
56
+ const onAddFieldToObjectFieldClickMock = vi . fn ( ) ;
57
+
58
+ render (
59
+ < FieldWithEditableInteractions
60
+ { ...DEFAULT_PROPS }
61
+ type = { 'object' }
62
+ id = { [ 'ordersId' ] }
63
+ onAddFieldToObjectFieldClick = { onAddFieldToObjectFieldClickMock }
64
+ /> ,
65
+ ) ;
66
+ expect ( screen . getByText ( 'ordersId' ) ) . toBeInTheDocument ( ) ;
67
+ expect ( screen . getByText ( '{}' ) ) . toBeInTheDocument ( ) ;
68
+ const button = screen . getByRole ( 'button' ) ;
69
+ expect ( button ) . toBeInTheDocument ( ) ;
70
+ expect ( button ) . toHaveAttribute ( 'data-testid' , 'object-field-type-pineapple-ordersId' ) ;
71
+ expect ( button ) . toHaveAttribute ( 'title' , 'Add Field' ) ;
72
+ expect ( onAddFieldToObjectFieldClickMock ) . not . toHaveBeenCalled ( ) ;
73
+ await userEvent . click ( button ) ;
74
+ expect ( onAddFieldToObjectFieldClickMock ) . toHaveBeenCalled ( ) ;
75
+ } ) ;
76
+
77
+ it ( 'Should not have a button to add a field with non-object types' , ( ) => {
78
+ render ( < FieldWithEditableInteractions { ...DEFAULT_PROPS } id = { [ 'ordersId' ] } /> ) ;
79
+ expect ( screen . getByText ( 'ordersId' ) ) . toBeInTheDocument ( ) ;
80
+ expect ( screen . getByText ( 'objectId' ) ) . toBeInTheDocument ( ) ;
81
+ const button = screen . queryByRole ( 'button' ) ;
82
+ expect ( button ) . not . toBeInTheDocument ( ) ;
83
+ } ) ;
84
+ } ) ;
85
+ describe ( 'With specific types' , ( ) => {
86
+ it ( 'shows [] with "array"' , ( ) => {
87
+ render ( < Field { ...DEFAULT_PROPS } type = "array" /> ) ;
88
+ expect ( screen . getByText ( '[]' ) ) . toBeInTheDocument ( ) ;
89
+ expect ( screen . queryByText ( 'array' ) ) . not . toBeInTheDocument ( ) ;
90
+ } ) ;
91
+ } ) ;
92
+
33
93
describe ( 'With glyphs' , ( ) => {
34
94
it ( 'With disabled' , ( ) => {
35
95
render ( < Field { ...DEFAULT_PROPS } variant = { 'disabled' } /> ) ;
0 commit comments