Skip to content

Commit 0c60796

Browse files
committed
Add a test
1 parent 2a0955f commit 0c60796

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

configs/eslint-config-mongosh/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module.exports = {
9393
...common.testRules,
9494
...extraJSRules,
9595
...extraTypescriptRules,
96+
'@typescript-eslint/no-non-null-assertion': 'off',
9697
},
9798
},
9899
],

packages/cli-repl/src/clr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type StyleDefinition =
1414
/** Optionally colorize a string, given a set of style definition(s). */
1515
export default function colorize(
1616
text: string,
17-
style: StyleDefinition,
17+
style: StyleDefinition | undefined,
1818
options: { colors: boolean }
1919
): string {
2020
if (options.colors) {

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ type MongoshRuntimeState = {
116116

117117
type GreetingDetails = {
118118
moreRecentMongoshVersion?: string | null;
119-
currentVersionCTA?: { text: string; style: StyleDefinition }[];
119+
currentVersionCTA?: { text: string; style?: StyleDefinition }[];
120120
};
121121

122122
/* Utility, inverse of Readonly<T> */

packages/cli-repl/src/update-notification-manager.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { AddressInfo } from 'net';
77
import os from 'os';
88
import path from 'path';
99
import { UpdateNotificationManager } from './update-notification-manager';
10+
import type { MongoshVersionsContents } from './update-notification-manager';
1011
import sinon from 'sinon';
1112

1213
describe('UpdateNotificationManager', function () {
@@ -122,4 +123,73 @@ describe('UpdateNotificationManager', function () {
122123
await manager.getLatestVersionIfMoreRecent('1.0.0-alpha.0')
123124
).to.equal(null);
124125
});
126+
127+
it('figures out the greeting CTA when set on a global level', async function () {
128+
const response: MongoshVersionsContents = {
129+
versions: [
130+
{ version: '1.0.0' },
131+
{
132+
version: '1.1.0',
133+
cta: { chunks: [{ text: "Don't use 1.1.0, downgrade!!" }] },
134+
},
135+
],
136+
cta: {
137+
chunks: [{ text: 'Vote for your favorite feature!', style: 'bold' }],
138+
},
139+
};
140+
reqHandler.callsFake((req, res) => {
141+
res.end(JSON.stringify(response));
142+
});
143+
144+
const manager = new UpdateNotificationManager();
145+
await manager.fetchUpdateMetadata(httpServerUrl, filename, '1.0.0');
146+
147+
const cta = await manager.getGreetingCTAForCurrentVersion();
148+
expect(cta).to.not.be.undefined;
149+
expect(cta?.length).to.equal(1);
150+
expect(cta![0]?.text).to.equal('Vote for your favorite feature!');
151+
expect(cta![0]?.style).to.equal('bold');
152+
});
153+
154+
it('figures out the greeting CTA when set on a per-version basis', async function () {
155+
const response: MongoshVersionsContents = {
156+
versions: [
157+
{
158+
version: '1.0.0',
159+
cta: {
160+
chunks: [
161+
{ text: "Don't use 1.0.0, upgrade!! " },
162+
{
163+
text: 'https://downloads.mongodb.com/mongosh/1.1.0/',
164+
style: 'mongosh:uri',
165+
},
166+
],
167+
},
168+
},
169+
{
170+
version: '1.1.0',
171+
cta: { chunks: [{ text: 'This version is very safe!' }] },
172+
},
173+
],
174+
cta: {
175+
chunks: [{ text: 'Vote for your favorite feature!', style: 'bold' }],
176+
},
177+
};
178+
reqHandler.callsFake((req, res) => {
179+
res.end(JSON.stringify(response));
180+
});
181+
182+
const manager = new UpdateNotificationManager();
183+
await manager.fetchUpdateMetadata(httpServerUrl, filename, '1.0.0');
184+
185+
const cta = await manager.getGreetingCTAForCurrentVersion();
186+
expect(cta).to.not.be.undefined;
187+
expect(cta?.length).to.equal(2);
188+
expect(cta![0]?.text).to.equal("Don't use 1.0.0, upgrade!! ");
189+
expect(cta![0]?.style).to.be.undefined;
190+
expect(cta![1]?.text).to.equal(
191+
'https://downloads.mongodb.com/mongosh/1.1.0/'
192+
);
193+
expect(cta![1]?.style).to.equal('mongosh:uri');
194+
});
125195
});

packages/cli-repl/src/update-notification-manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import type {
77
Response,
88
} from '@mongodb-js/devtools-proxy-support';
99
import { createFetch } from '@mongodb-js/devtools-proxy-support';
10-
import { StyleDefinition } from './clr';
10+
import type { StyleDefinition } from './clr';
1111

1212
interface GreetingCTADetails {
1313
chunks: {
1414
text: string;
15-
style: StyleDefinition;
15+
style?: StyleDefinition;
1616
}[];
1717
}
1818

19-
interface MongoshVersionsContents {
19+
export interface MongoshVersionsContents {
2020
versions: {
2121
version: string;
2222
cta?: GreetingCTADetails;
@@ -72,7 +72,7 @@ export class UpdateNotificationManager {
7272
async getGreetingCTAForCurrentVersion(): Promise<
7373
| {
7474
text: string;
75-
style: StyleDefinition;
75+
style?: StyleDefinition;
7676
}[]
7777
| undefined
7878
> {

0 commit comments

Comments
 (0)