Skip to content

Commit 4ca513b

Browse files
committed
conditionally render ai optin modal content using isCloudOptIn prop
1 parent 5c1d63e commit 4ca513b

File tree

11 files changed

+198
-160
lines changed

11 files changed

+198
-160
lines changed

package-lock.json

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-collection/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@mongodb-js/compass-app-stores": "^7.57.0",
5353
"@mongodb-js/compass-components": "^1.49.0",
5454
"@mongodb-js/compass-connections": "^1.71.0",
55-
"@mongodb-js/compass-generative-ai": "^0.50.0",
55+
"@mongodb-js/compass-generative-ai": "^0.51.0",
5656
"@mongodb-js/compass-logging": "^1.7.12",
5757
"@mongodb-js/compass-telemetry": "^1.14.0",
5858
"@mongodb-js/compass-workspaces": "^0.52.0",

packages/compass-generative-ai/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write ."
5353
},
5454
"dependencies": {
55-
"@leafygreen-ui/marketing-modal": "^8.0.0",
5655
"@mongodb-js/atlas-service": "^0.56.0",
5756
"@mongodb-js/compass-app-registry": "^9.4.20",
5857
"@mongodb-js/compass-components": "^1.49.0",

packages/compass-generative-ai/src/atlas-ai-service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,18 @@ export class AtlasAiService {
278278

279279
async ensureAiFeatureAccess({ signal }: { signal?: AbortSignal } = {}) {
280280
if (this.preferences.getPreferences().enableUnauthenticatedGenAI) {
281-
return getStore().dispatch(optIntoGenAIWithModalPrompt({ signal }));
281+
return getStore().dispatch(
282+
optIntoGenAIWithModalPrompt({ signal, isCloudOptIn: false })
283+
);
282284
}
283285

284286
// When the ai feature is attempted to be opened we make sure
285287
// the user is signed into Atlas and opted in.
286288

287289
if (this.apiURLPreset === 'cloud') {
288-
return getStore().dispatch(optIntoGenAIWithModalPrompt({ signal }));
290+
return getStore().dispatch(
291+
optIntoGenAIWithModalPrompt({ signal, isCloudOptIn: true })
292+
);
289293
}
290294
return getStore().dispatch(signIntoAtlasWithModalPrompt({ signal }));
291295
}
Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1+
import { render, screen } from '@mongodb-js/testing-library-compass';
12
import React from 'react';
2-
import { render, screen, cleanup } from '@mongodb-js/testing-library-compass';
33
import { expect } from 'chai';
44
import { AIOptInModal } from './ai-optin-modal';
55
import type { PreferencesAccess } from 'compass-preferences-model';
66
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
77
import { PreferencesProvider } from 'compass-preferences-model/provider';
8+
import Sinon from 'sinon';
89

910
let mockPreferences: PreferencesAccess;
1011

1112
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+
1225
beforeEach(async function () {
1326
mockPreferences = await createSandboxFromDefaultPreferences();
1427
});
1528

1629
afterEach(function () {
17-
cleanup();
30+
sandbox.restore();
1831
});
1932

20-
it('should show the modal title', function () {
33+
it('should show the correct modal title when in a cloud opt-in environment', function () {
2134
render(
2235
<PreferencesProvider value={mockPreferences}>
23-
<AIOptInModal
24-
projectId="ab123"
25-
isOptInModalVisible={true}
26-
isOptInInProgress={false}
27-
onOptInModalClose={() => {}}
28-
onOptInClick={() => {}}
29-
></AIOptInModal>
36+
<AIOptInModal {...baseProps} />
3037
</PreferencesProvider>
3138
);
3239
expect(
@@ -35,38 +42,61 @@ describe('AIOptInModal Component', function () {
3542
})
3643
).to.exist;
3744
});
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+
3859
it('should show the Not now link', function () {
3960
render(
4061
<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} />
5063
</PreferencesProvider>
5164
);
5265
expect(screen.getByText('Not now')).to.exist;
5366
});
5467

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+
5593
it('should show the opt in button enabled when project AI setting is enabled', async function () {
5694
await mockPreferences.savePreferences({
5795
enableGenAIFeaturesAtlasProject: true,
5896
});
5997
render(
6098
<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} />
70100
</PreferencesProvider>
71101
);
72102
const button = screen.getByText('Use AI Features').closest('button');
@@ -81,13 +111,7 @@ describe('AIOptInModal Component', function () {
81111
});
82112
render(
83113
<PreferencesProvider value={mockPreferences}>
84-
<AIOptInModal
85-
projectId="ab123"
86-
isOptInModalVisible={true}
87-
isOptInInProgress={false}
88-
onOptInModalClose={() => {}}
89-
onOptInClick={() => {}}
90-
/>
114+
<AIOptInModal {...baseProps} />
91115
</PreferencesProvider>
92116
);
93117
expect(
@@ -107,13 +131,7 @@ describe('AIOptInModal Component', function () {
107131
});
108132
render(
109133
<PreferencesProvider value={mockPreferences}>
110-
<AIOptInModal
111-
projectId="ab123"
112-
isOptInModalVisible={true}
113-
isOptInInProgress={false}
114-
onOptInModalClose={() => {}}
115-
onOptInClick={() => {}}
116-
/>
134+
<AIOptInModal {...baseProps} />
117135
</PreferencesProvider>
118136
);
119137
expect(
@@ -135,13 +153,7 @@ describe('AIOptInModal Component', function () {
135153
});
136154
render(
137155
<PreferencesProvider value={mockPreferences}>
138-
<AIOptInModal
139-
projectId="ab123"
140-
isOptInModalVisible={true}
141-
isOptInInProgress={false}
142-
onOptInModalClose={() => {}}
143-
onOptInClick={() => {}}
144-
/>
156+
<AIOptInModal {...baseProps} />
145157
</PreferencesProvider>
146158
);
147159
expect(
@@ -157,49 +169,31 @@ describe('AIOptInModal Component', function () {
157169

158170
describe('button click behavior', function () {
159171
it('should not call onOptInClick when main AI features are disabled', async function () {
160-
let onOptInClickCalled = false;
161172
await mockPreferences.savePreferences({
162173
enableGenAIFeaturesAtlasProject: false,
163174
});
164175
render(
165176
<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} />
175178
</PreferencesProvider>
176179
);
177180
const button = screen.getByText('Use AI Features');
178181
button.click();
179-
expect(onOptInClickCalled).to.be.false;
182+
expect(onOptInClickStub).not.to.have.been.called;
180183
});
181184

182185
it('should call onOptInClick when main AI features are enabled', async function () {
183-
let onOptInClickCalled = false;
184186
await mockPreferences.savePreferences({
185187
enableGenAIFeaturesAtlasProject: true,
186188
});
187189
render(
188190
<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} />
198192
</PreferencesProvider>
199193
);
200194
const button = screen.getByText('Use AI Features');
201195
button.click();
202-
expect(onOptInClickCalled).to.be.true;
196+
expect(onOptInClickStub).to.have.been.calledOnce;
203197
});
204198
});
205199
});

0 commit comments

Comments
 (0)