Skip to content

Commit

Permalink
feat: show survey VSCODE-562 (#780)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Rhys <Anemy@users.noreply.github.com>
Co-authored-by: Rhys Howell <rhys.howell@mongodb.com>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent a29b4e2 commit d97770b
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export default class MDBExtensionController implements vscode.Disposable {

this.registerCommands();
this.showOverviewPageIfRecentlyInstalled();
void this.showSurveyForEstablishedUsers();
}

registerCommands = (): void => {
Expand Down Expand Up @@ -815,6 +816,44 @@ export default class MDBExtensionController implements vscode.Disposable {
}
}

async showSurveyForEstablishedUsers(): Promise<void> {
const surveyId = '9viN9wcbsC3zvHyg7';

const hasBeenShownSurveyAlready =
this._storageController.get(StorageVariables.GLOBAL_SURVEY_SHOWN) ===
surveyId;

// Show the overview page when it hasn't been show to the
// user yet, and they have saved connections
// -> they haven't just started using this extension
if (
hasBeenShownSurveyAlready ||
!this._connectionStorage.hasSavedConnections()
) {
return;
}

const action = 'Share your thoughts';
const text = 'How can we make the MongoDB extension better for you?';
const link = 'https://forms.gle/9viN9wcbsC3zvHyg7';
const result = await vscode.window.showInformationMessage(
text,
{},
{
title: action,
}
);
if (result?.title === action) {
void vscode.env.openExternal(vscode.Uri.parse(link));
}

// whether action was taken or the prompt dismissed, we won't show this again
void this._storageController.update(
StorageVariables.GLOBAL_SURVEY_SHOWN,
surveyId
);
}

async dispose(): Promise<void> {
await this.deactivate();
}
Expand Down
2 changes: 2 additions & 0 deletions src/storage/storageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { StoreConnectionInfo } from './connectionStorage';
export enum StorageVariables {
// Only exists on globalState.
GLOBAL_HAS_BEEN_SHOWN_INITIAL_VIEW = 'GLOBAL_HAS_BEEN_SHOWN_INITIAL_VIEW',
GLOBAL_SURVEY_SHOWN = 'GLOBAL_SURVEY_SHOWN',
GLOBAL_SAVED_CONNECTIONS = 'GLOBAL_SAVED_CONNECTIONS',
// Analytics user identify.
GLOBAL_USER_ID = 'GLOBAL_USER_ID',
Expand Down Expand Up @@ -50,6 +51,7 @@ interface StorageVariableContents {
[StorageVariables.GLOBAL_USER_ID]: string;
[StorageVariables.GLOBAL_ANONYMOUS_ID]: string;
[StorageVariables.GLOBAL_HAS_BEEN_SHOWN_INITIAL_VIEW]: boolean;
[StorageVariables.GLOBAL_SURVEY_SHOWN]: string;
[StorageVariables.GLOBAL_SAVED_CONNECTIONS]: ConnectionsFromStorage;
[StorageVariables.WORKSPACE_SAVED_CONNECTIONS]: ConnectionsFromStorage;
}
Expand Down
135 changes: 135 additions & 0 deletions src/test/suite/mdbExtensionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ suite('MDBExtensionController Test Suite', function () {
let fakeActiveConnectionId: SinonSpy;
let showErrorMessageStub: SinonStub;
let fakeCreatePlaygroundFileWithContent: SinonSpy;
let openExternalStub: SinonStub;

const sandbox = sinon.createSandbox();

Expand All @@ -183,6 +184,7 @@ suite('MDBExtensionController Test Suite', function () {
vscode.window,
'showInformationMessage'
);
openExternalStub = sandbox.stub(vscode.env, 'openExternal');
openTextDocumentStub = sandbox.stub(vscode.workspace, 'openTextDocument');
fakeActiveConnectionId = sandbox.fake.returns('tasty_sandwhich');
sandbox.replace(
Expand Down Expand Up @@ -1717,5 +1719,138 @@ suite('MDBExtensionController Test Suite', function () {
});
});
});

suite('survey prompt', function () {
suite(
"when a user hasn't been shown the survey prompt yet, and they have connections saved",
() => {
[
{
description: 'clicked the button',
value: { title: 'Share your thoughts' },
},
{ description: 'dismissed', value: undefined },
].forEach((reaction) => {
suite(`user ${reaction.description}`, () => {
let connectionsUpdateStub: SinonStub;
let uriParseStub: SinonStub;
beforeEach(async () => {
showInformationMessageStub.resolves(reaction.value);
openExternalStub.resolves(undefined);
sandbox.replace(
mdbTestExtension.testExtensionController._storageController,
'get',
sandbox.fake.returns(undefined)
);
sandbox.replace(
mdbTestExtension.testExtensionController._connectionStorage,
'hasSavedConnections',
sandbox.fake.returns(true)
);
connectionsUpdateStub = sandbox.stub(
mdbTestExtension.testExtensionController._storageController,
'update'
);
uriParseStub = sandbox.stub(vscode.Uri, 'parse');
connectionsUpdateStub.resolves(undefined);
await mdbTestExtension.testExtensionController.showSurveyForEstablishedUsers();
});

afterEach(() => {
sandbox.restore();
});

test('they are shown the survey prompt', () => {
assert(showInformationMessageStub.called);
assert.strictEqual(
showInformationMessageStub.firstCall.args[0],
'How can we make the MongoDB extension better for you?'
);
});

test('the link was open if and only if they click the button', () => {
if (reaction.value === undefined) {
assert(openExternalStub.notCalled);
}
if (reaction.value) {
assert(openExternalStub.called);
assert(uriParseStub.called);
assert.strictEqual(
uriParseStub.firstCall.args[0],
'https://forms.gle/9viN9wcbsC3zvHyg7'
);
}
});

test("it sets that they've been shown the survey", () => {
assert(connectionsUpdateStub.called);
assert.strictEqual(
connectionsUpdateStub.firstCall.args[0],
StorageVariables.GLOBAL_SURVEY_SHOWN
);
assert.strictEqual(
connectionsUpdateStub.firstCall.args[1],
'9viN9wcbsC3zvHyg7'
);
});
});
});
}
);

suite('when a user has been shown the survey prompt already', () => {
let connectionsUpdateStub: SinonStub;
beforeEach(() => {
sandbox.replace(
mdbTestExtension.testExtensionController._storageController,
'get',
sandbox.fake.returns('9viN9wcbsC3zvHyg7') // survey has been shown
);
sandbox.replace(
mdbTestExtension.testExtensionController._connectionStorage,
'hasSavedConnections',
sandbox.fake.returns(true)
);
connectionsUpdateStub = sandbox.stub(
mdbTestExtension.testExtensionController._storageController,
'update'
);
connectionsUpdateStub.resolves(undefined);

void mdbTestExtension.testExtensionController.showSurveyForEstablishedUsers();
});

test('they are not shown the survey prompt', () => {
assert(showInformationMessageStub.notCalled);
});
});

suite('when a has no connections saved', () => {
let connectionsUpdateStub: SinonStub;
beforeEach(() => {
sandbox.replace(
mdbTestExtension.testExtensionController._storageController,
'get',
sandbox.fake.returns(undefined)
);
sandbox.replace(
mdbTestExtension.testExtensionController._connectionStorage,
'hasSavedConnections',
sandbox.fake.returns(false) // no connections yet - this might be the first install
);
connectionsUpdateStub = sandbox.stub(
mdbTestExtension.testExtensionController._storageController,
'update'
);
connectionsUpdateStub.resolves(undefined);

void mdbTestExtension.testExtensionController.showSurveyForEstablishedUsers();
});

test('they are not shown the survey prompt', () => {
assert(showInformationMessageStub.notCalled);
});
});
});
});
});

0 comments on commit d97770b

Please sign in to comment.