11import { palette } from '@leafygreen-ui/palette' ;
22import { ComponentProps } from 'react' ;
3+ import { userEvent } from '@testing-library/user-event' ;
34
45import { render , screen } from '@/mocks/testing-utils' ;
56import { Field as FieldComponent } from '@/components/field/field' ;
67import { 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 ' ;
89
910const Field = ( props : React . ComponentProps < typeof FieldComponent > ) => (
10- < FieldSelectionProvider >
11+ < EditableDiagramInteractionsProvider >
1112 < FieldComponent { ...props } />
12- </ FieldSelectionProvider >
13+ </ EditableDiagramInteractionsProvider >
1314) ;
1415
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+
1529describe ( 'field' , ( ) => {
1630 const DEFAULT_PROPS : ComponentProps < typeof Field > = {
1731 nodeType : 'collection' ,
@@ -30,6 +44,52 @@ describe('field', () => {
3044 expect ( screen . getByRole ( 'img' , { name : 'Key Icon' } ) ) . toBeInTheDocument ( ) ;
3145 expect ( screen . getByRole ( 'img' , { name : 'Link Icon' } ) ) . toBeInTheDocument ( ) ;
3246 } ) ;
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+
3393 describe ( 'With glyphs' , ( ) => {
3494 it ( 'With disabled' , ( ) => {
3595 render ( < Field { ...DEFAULT_PROPS } variant = { 'disabled' } /> ) ;
0 commit comments