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' ;
@@ -12,20 +13,19 @@ const Field = (props: React.ComponentProps<typeof FieldComponent>) => (
12
13
</ EditableDiagramInteractionsProvider >
13
14
) ;
14
15
15
- const noop = ( ) => {
16
- /* no operation */
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
+ ) ;
17
27
} ;
18
28
19
- const FieldWithEditableInteractions = ( props : React . ComponentProps < typeof FieldComponent > ) => (
20
- < EditableDiagramInteractionsProvider
21
- onFieldClick = { noop }
22
- onAddFieldToNodeClick = { noop }
23
- onAddFieldToObjectFieldClick = { noop }
24
- >
25
- < FieldComponent { ...props } />
26
- </ EditableDiagramInteractionsProvider >
27
- ) ;
28
-
29
29
describe ( 'field' , ( ) => {
30
30
const DEFAULT_PROPS : ComponentProps < typeof Field > = {
31
31
nodeType : 'collection' ,
@@ -52,14 +52,34 @@ describe('field', () => {
52
52
expect ( button ) . not . toBeInTheDocument ( ) ;
53
53
} ) ;
54
54
describe ( 'With editable interactions supplied' , ( ) => {
55
- it ( 'Should have a button to add a field on an object type' , ( ) => {
56
- render ( < FieldWithEditableInteractions { ...DEFAULT_PROPS } type = { 'object' } id = { [ 'ordersId' ] } /> ) ;
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
+ ) ;
57
66
expect ( screen . getByText ( 'ordersId' ) ) . toBeInTheDocument ( ) ;
58
67
expect ( screen . getByText ( '{}' ) ) . toBeInTheDocument ( ) ;
59
68
const button = screen . getByRole ( 'button' ) ;
60
69
expect ( button ) . toBeInTheDocument ( ) ;
61
70
expect ( button ) . toHaveAttribute ( 'data-testid' , 'object-field-type-pineapple-ordersId' ) ;
62
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 ( ) ;
63
83
} ) ;
64
84
} ) ;
65
85
describe ( 'With glyphs' , ( ) => {
0 commit comments