1
+ import { render , screen } from '@mongodb-js/testing-library-compass' ;
1
2
import React from 'react' ;
2
- import { render , screen , cleanup } from '@mongodb-js/testing-library-compass' ;
3
3
import { expect } from 'chai' ;
4
4
import { AIOptInModal } from './ai-optin-modal' ;
5
5
import type { PreferencesAccess } from 'compass-preferences-model' ;
6
6
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model' ;
7
7
import { PreferencesProvider } from 'compass-preferences-model/provider' ;
8
+ import Sinon from 'sinon' ;
8
9
9
10
let mockPreferences : PreferencesAccess ;
10
11
11
12
describe ( 'AIOptInModal Component' , function ( ) {
13
+ const sandbox = Sinon . createSandbox ( ) ;
14
+ const onOptInClickStub = sandbox . stub ( ) ;
15
+
16
+ const baseProps = {
17
+ projectId : 'ab123' ,
18
+ isCloudOptIn : true ,
19
+ isOptInModalVisible : true ,
20
+ isOptInInProgress : false ,
21
+ onOptInModalClose : ( ) => { } ,
22
+ onOptInClick : onOptInClickStub ,
23
+ } ;
24
+
12
25
beforeEach ( async function ( ) {
13
26
mockPreferences = await createSandboxFromDefaultPreferences ( ) ;
14
27
} ) ;
15
28
16
29
afterEach ( function ( ) {
17
- cleanup ( ) ;
30
+ sandbox . restore ( ) ;
18
31
} ) ;
19
32
20
- it ( 'should show the modal title' , function ( ) {
33
+ it ( 'should show the correct modal title when in a cloud opt-in environment ' , function ( ) {
21
34
render (
22
35
< PreferencesProvider value = { mockPreferences } >
23
- < AIOptInModal
24
- projectId = "ab123"
25
- isOptInModalVisible = { true }
26
- isOptInInProgress = { false }
27
- onOptInModalClose = { ( ) => { } }
28
- onOptInClick = { ( ) => { } }
29
- > </ AIOptInModal >
36
+ < AIOptInModal { ...baseProps } />
30
37
</ PreferencesProvider >
31
38
) ;
32
39
expect (
@@ -35,38 +42,61 @@ describe('AIOptInModal Component', function () {
35
42
} )
36
43
) . to . exist ;
37
44
} ) ;
45
+
46
+ it ( 'should show the correct modal title when not in a cloud opt-in environment' , function ( ) {
47
+ render (
48
+ < PreferencesProvider value = { mockPreferences } >
49
+ < AIOptInModal { ...baseProps } isCloudOptIn = { false } />
50
+ </ PreferencesProvider >
51
+ ) ;
52
+ expect (
53
+ screen . getByRole ( 'heading' , {
54
+ name : 'Use AI Features in Compass' ,
55
+ } )
56
+ ) . to . exist ;
57
+ } ) ;
58
+
38
59
it ( 'should show the Not now link' , function ( ) {
39
60
render (
40
61
< PreferencesProvider value = { mockPreferences } >
41
- < AIOptInModal
42
- projectId = "ab123"
43
- isOptInModalVisible = { true }
44
- isOptInInProgress = { false }
45
- onOptInModalClose = { ( ) => { } }
46
- onOptInClick = { ( ) => { } }
47
- >
48
- { ' ' }
49
- </ AIOptInModal >
62
+ < AIOptInModal { ...baseProps } />
50
63
</ PreferencesProvider >
51
64
) ;
52
65
expect ( screen . getByText ( 'Not now' ) ) . to . exist ;
53
66
} ) ;
54
67
68
+ it ( 'should show an info banner when in a cloud opt-in environment' , async function ( ) {
69
+ await mockPreferences . savePreferences ( {
70
+ enableGenAIFeaturesAtlasProject : true ,
71
+ } ) ;
72
+
73
+ render (
74
+ < PreferencesProvider value = { mockPreferences } >
75
+ < AIOptInModal { ...baseProps } />
76
+ </ PreferencesProvider >
77
+ ) ;
78
+
79
+ const banner = screen . getByTestId ( 'ai-optin-cloud-banner' ) ;
80
+ expect ( banner ) . to . exist ;
81
+ } ) ;
82
+
83
+ it ( 'should not show a banner when in non-cloud environment' , function ( ) {
84
+ render (
85
+ < PreferencesProvider value = { mockPreferences } >
86
+ < AIOptInModal { ...baseProps } isCloudOptIn = { false } />
87
+ </ PreferencesProvider >
88
+ ) ;
89
+ const banner = screen . queryByTestId ( 'ai-optin-cloud-banner' ) ;
90
+ expect ( banner ) . to . not . exist ;
91
+ } ) ;
92
+
55
93
it ( 'should show the opt in button enabled when project AI setting is enabled' , async function ( ) {
56
94
await mockPreferences . savePreferences ( {
57
95
enableGenAIFeaturesAtlasProject : true ,
58
96
} ) ;
59
97
render (
60
98
< PreferencesProvider value = { mockPreferences } >
61
- < AIOptInModal
62
- projectId = "ab123"
63
- isOptInModalVisible = { true }
64
- isOptInInProgress = { false }
65
- onOptInModalClose = { ( ) => { } }
66
- onOptInClick = { ( ) => { } }
67
- >
68
- { ' ' }
69
- </ AIOptInModal >
99
+ < AIOptInModal { ...baseProps } />
70
100
</ PreferencesProvider >
71
101
) ;
72
102
const button = screen . getByText ( 'Use AI Features' ) . closest ( 'button' ) ;
@@ -81,13 +111,7 @@ describe('AIOptInModal Component', function () {
81
111
} ) ;
82
112
render (
83
113
< PreferencesProvider value = { mockPreferences } >
84
- < AIOptInModal
85
- projectId = "ab123"
86
- isOptInModalVisible = { true }
87
- isOptInInProgress = { false }
88
- onOptInModalClose = { ( ) => { } }
89
- onOptInClick = { ( ) => { } }
90
- />
114
+ < AIOptInModal { ...baseProps } />
91
115
</ PreferencesProvider >
92
116
) ;
93
117
expect (
@@ -107,13 +131,7 @@ describe('AIOptInModal Component', function () {
107
131
} ) ;
108
132
render (
109
133
< PreferencesProvider value = { mockPreferences } >
110
- < AIOptInModal
111
- projectId = "ab123"
112
- isOptInModalVisible = { true }
113
- isOptInInProgress = { false }
114
- onOptInModalClose = { ( ) => { } }
115
- onOptInClick = { ( ) => { } }
116
- />
134
+ < AIOptInModal { ...baseProps } />
117
135
</ PreferencesProvider >
118
136
) ;
119
137
expect (
@@ -135,13 +153,7 @@ describe('AIOptInModal Component', function () {
135
153
} ) ;
136
154
render (
137
155
< PreferencesProvider value = { mockPreferences } >
138
- < AIOptInModal
139
- projectId = "ab123"
140
- isOptInModalVisible = { true }
141
- isOptInInProgress = { false }
142
- onOptInModalClose = { ( ) => { } }
143
- onOptInClick = { ( ) => { } }
144
- />
156
+ < AIOptInModal { ...baseProps } />
145
157
</ PreferencesProvider >
146
158
) ;
147
159
expect (
@@ -157,49 +169,31 @@ describe('AIOptInModal Component', function () {
157
169
158
170
describe ( 'button click behavior' , function ( ) {
159
171
it ( 'should not call onOptInClick when main AI features are disabled' , async function ( ) {
160
- let onOptInClickCalled = false ;
161
172
await mockPreferences . savePreferences ( {
162
173
enableGenAIFeaturesAtlasProject : false ,
163
174
} ) ;
164
175
render (
165
176
< PreferencesProvider value = { mockPreferences } >
166
- < AIOptInModal
167
- projectId = "ab123"
168
- isOptInModalVisible = { true }
169
- isOptInInProgress = { false }
170
- onOptInModalClose = { ( ) => { } }
171
- onOptInClick = { ( ) => {
172
- onOptInClickCalled = true ;
173
- } }
174
- />
177
+ < AIOptInModal { ...baseProps } />
175
178
</ PreferencesProvider >
176
179
) ;
177
180
const button = screen . getByText ( 'Use AI Features' ) ;
178
181
button . click ( ) ;
179
- expect ( onOptInClickCalled ) . to . be . false ;
182
+ expect ( onOptInClickStub ) . not . to . have . been . called ;
180
183
} ) ;
181
184
182
185
it ( 'should call onOptInClick when main AI features are enabled' , async function ( ) {
183
- let onOptInClickCalled = false ;
184
186
await mockPreferences . savePreferences ( {
185
187
enableGenAIFeaturesAtlasProject : true ,
186
188
} ) ;
187
189
render (
188
190
< PreferencesProvider value = { mockPreferences } >
189
- < AIOptInModal
190
- projectId = "ab123"
191
- isOptInModalVisible = { true }
192
- isOptInInProgress = { false }
193
- onOptInModalClose = { ( ) => { } }
194
- onOptInClick = { ( ) => {
195
- onOptInClickCalled = true ;
196
- } }
197
- />
191
+ < AIOptInModal { ...baseProps } />
198
192
</ PreferencesProvider >
199
193
) ;
200
194
const button = screen . getByText ( 'Use AI Features' ) ;
201
195
button . click ( ) ;
202
- expect ( onOptInClickCalled ) . to . be . true ;
196
+ expect ( onOptInClickStub ) . to . have . been . calledOnce ;
203
197
} ) ;
204
198
} ) ;
205
199
} ) ;
0 commit comments