1
1
import { configure , fireEvent , render , screen , waitFor } from '@testing-library/react' ;
2
2
import { componentCRMocks } from '../../Components/__data__/mock-data' ;
3
3
import { ComponentRelationModal } from '../ComponentRelationModal' ;
4
+ import { ComponentRelationNudgeType , ComponentRelationValue } from '../type' ;
5
+ import { useNudgeData } from '../useNudgeData' ;
6
+ import { updateNudgeDependencies } from '../utils' ;
4
7
5
8
configure ( { testIdAttribute : 'id' } ) ;
6
9
@@ -20,7 +23,11 @@ jest.mock('../../../utils/analytics', () => ({
20
23
21
24
jest . mock ( '../utils' , ( ) => ( {
22
25
...jest . requireActual ( '../utils' ) ,
23
- updateNudgeDependencies : jest . fn ( ( ) => Promise . resolve ( [ ] ) ) ,
26
+ updateNudgeDependencies : jest . fn ( ) ,
27
+ } ) ) ;
28
+
29
+ jest . mock ( '../useNudgeData' , ( ) => ( {
30
+ useNudgeData : jest . fn ( ) ,
24
31
} ) ) ;
25
32
26
33
class MockResizeObserver {
@@ -37,9 +44,29 @@ class MockResizeObserver {
37
44
}
38
45
}
39
46
47
+ const mockComponentRelations : ComponentRelationValue [ ] = [
48
+ {
49
+ source : 'a' ,
50
+ nudgeType : ComponentRelationNudgeType . NUDGES ,
51
+ target : [ 'b' ] ,
52
+ } ,
53
+ {
54
+ source : 'c' ,
55
+ nudgeType : ComponentRelationNudgeType . NUDGES ,
56
+ target : [ 'd' ] ,
57
+ } ,
58
+ ] ;
59
+
40
60
window . ResizeObserver = MockResizeObserver ;
61
+ const useNudgeDataMock = useNudgeData as jest . Mock ;
62
+ const updateNudgeDependenciesMock = updateNudgeDependencies as jest . Mock ;
41
63
42
64
describe ( 'ComponentRelationModal' , ( ) => {
65
+ beforeEach ( ( ) => {
66
+ useNudgeDataMock . mockReturnValue ( [ [ ] , true , null ] ) ;
67
+ updateNudgeDependenciesMock . mockResolvedValue ( componentCRMocks ) ;
68
+ } ) ;
69
+
43
70
it ( 'should render modal' , ( ) => {
44
71
render ( < ComponentRelationModal modalProps = { { isOpen : true } } application = "apps" /> ) ;
45
72
screen . getByText ( 'Component relationships' ) ;
@@ -50,6 +77,15 @@ describe('ComponentRelationModal', () => {
50
77
expect ( screen . getAllByTestId ( 'toggle-component-menu' ) ) . toHaveLength ( 2 ) ;
51
78
} ) ;
52
79
80
+ it ( 'should remove a relation' , ( ) => {
81
+ render ( < ComponentRelationModal modalProps = { { isOpen : true } } application = "apps" /> ) ;
82
+ expect ( screen . queryAllByTestId ( / r e m o v e - r e l a t i o n - \d + / ) ) . toHaveLength ( 1 ) ;
83
+ fireEvent . click ( screen . getByText ( `Add another component relationship` ) ) ;
84
+ expect ( screen . getAllByTestId ( / r e m o v e - r e l a t i o n - \d + / ) ) . toHaveLength ( 2 ) ;
85
+ fireEvent . click ( screen . getByTestId ( 'remove-relation-0' ) ) ;
86
+ expect ( screen . queryAllByTestId ( / r e m o v e - r e l a t i o n - \d + / ) ) . toHaveLength ( 1 ) ;
87
+ } ) ;
88
+
53
89
it ( 'should show cancelation modal when clicked on cancel' , ( ) => {
54
90
let isOpen = true ;
55
91
const onClose = ( ) => {
@@ -79,17 +115,47 @@ describe('ComponentRelationModal', () => {
79
115
} ) ;
80
116
81
117
it ( 'should show confirmation modal on relationship save' , async ( ) => {
118
+ useNudgeDataMock . mockReturnValue ( [ mockComponentRelations , true , null ] ) ;
119
+ updateNudgeDependenciesMock . mockResolvedValue ( componentCRMocks ) ;
82
120
let isOpen = true ;
83
121
const onClose = ( ) => {
84
122
isOpen = false ;
85
123
} ;
86
- render ( < ComponentRelationModal modalProps = { { isOpen, onClose } } application = "apps" /> ) ;
124
+ const { rerender } = render (
125
+ < ComponentRelationModal modalProps = { { isOpen, onClose } } application = "apps" /> ,
126
+ ) ;
87
127
expect ( screen . queryByText ( 'Component relationships' ) ) . toBeInTheDocument ( ) ;
88
128
fireEvent . click ( screen . getByTestId ( 'nudged-by-0' ) ) ;
89
129
const saveButton = screen . getByText ( 'Save relationships' ) ;
90
130
expect ( saveButton . getAttribute ( 'class' ) ) . not . toContain ( 'pf-m-disabled' ) ;
91
131
fireEvent . click ( saveButton ) ;
92
132
expect ( saveButton . getAttribute ( 'class' ) ) . toContain ( 'pf-m-in-progress' ) ;
93
- await waitFor ( ( ) => expect ( saveButton . getAttribute ( 'class' ) ) . not . toContain ( 'pf-m-in-progress' ) ) ;
133
+ await waitFor ( ( ) => {
134
+ expect ( saveButton . getAttribute ( 'class' ) ) . toContain ( 'pf-m-in-progress' ) ;
135
+ } ) ;
136
+
137
+ rerender ( < ComponentRelationModal modalProps = { { isOpen, onClose } } application = "apps" /> ) ;
138
+ await waitFor ( ( ) => {
139
+ expect ( screen . queryByText ( 'Relationships updated!' ) ) . toBeInTheDocument ( ) ;
140
+ } ) ;
141
+
142
+ const doneButton = screen . getByText ( 'Done' ) ;
143
+ fireEvent . click ( doneButton ) ;
144
+ } ) ;
145
+
146
+ it ( 'should display an error on failure' , async ( ) => {
147
+ useNudgeDataMock . mockReturnValue ( [ mockComponentRelations , true , null ] ) ;
148
+ updateNudgeDependenciesMock . mockRejectedValue ( new Error ( 'error' ) ) ;
149
+ let isOpen = true ;
150
+ const onClose = ( ) => {
151
+ isOpen = false ;
152
+ } ;
153
+ render ( < ComponentRelationModal modalProps = { { isOpen, onClose } } application = "apps" /> ) ;
154
+ expect ( screen . queryByText ( 'Component relationships' ) ) . toBeInTheDocument ( ) ;
155
+ fireEvent . click ( screen . getByTestId ( 'nudged-by-0' ) ) ;
156
+ const saveButton = screen . getByText ( 'Save relationships' ) ;
157
+ expect ( saveButton . getAttribute ( 'class' ) ) . not . toContain ( 'pf-m-disabled' ) ;
158
+ fireEvent . click ( saveButton ) ;
159
+ await waitFor ( ( ) => expect ( screen . queryByText ( 'Danger alert:' ) ) . toBeInTheDocument ( ) ) ;
94
160
} ) ;
95
161
} ) ;
0 commit comments