Skip to content

Commit

Permalink
Added tests for opening link in the browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduckted committed Jan 27, 2021
1 parent 244e940 commit b614a65
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
46 changes: 32 additions & 14 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Command {
private readonly handlerSelector: LinkHandlerSelector,
private readonly linkType: LinkType | undefined,
private readonly includeSelection: boolean
) { }
) {}

/**
* Executes the commands.
Expand Down Expand Up @@ -79,10 +79,16 @@ export class Command {

log('Web link created: %s', link);
await env.clipboard.writeText(link);
let clickedItem = await window.showInformationMessage(STRINGS.command.linkCopied(file.handler.name), STRINGS.command.openInWeb);
if (clickedItem === STRINGS.command.openInWeb) {
void env.openExternal(Uri.parse(link));
}

void window
.showInformationMessage<ActionMessageItem>(
STRINGS.command.linkCopied(file.handler.name),
{
title: STRINGS.command.openInBrowser,
action: 'open'
}
)
.then((x) => this.onNotificationItemClick(x, link));
} catch (ex) {
log('Error while generating a link: %o', ex);
void window.showErrorMessage(STRINGS.command.error);
Expand Down Expand Up @@ -123,14 +129,7 @@ export class Command {
title: 'Open Settings',
action: 'settings'
})
.then((item) => {
if (item?.action === 'settings') {
void commands.executeCommand(
'workbench.action.openSettings',
'gitweblinks'
);
}
});
.then((x) => this.onNotificationItemClick(x));
return undefined;
}

Expand All @@ -153,6 +152,25 @@ export class Command {
endColumn: editor.selection.end.character + 1
};
}

/**
* Handles a button on a notification being clicked.
*
* @param item The item that was clicked on.
* @param link The link that has been created.
*/
private onNotificationItemClick(item: ActionMessageItem | undefined, link?: string): void {
switch (item?.action) {
case 'settings':
void commands.executeCommand('workbench.action.openSettings', 'gitweblinks');
break;

case 'open':
if (link) {
void env.openExternal(Uri.parse(link));
}
}
}
}

/**
Expand Down Expand Up @@ -279,5 +297,5 @@ interface ActionMessageItem extends MessageItem {
/**
* The action to perform.
*/
action: 'settings';
action: 'settings' | 'open';
}
2 changes: 1 addition & 1 deletion src/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const STRINGS = {
},
command: {
noFileSelected: 'Cannot copy a link because no file is selected.',
openInWeb: 'Open in Web',
openInBrowser: 'Open in Browser',
linkCopied: (handlerName: string): string =>
format('%s link copied to the clipboard.', handlerName),
error: 'An error occurred while creating the link.',
Expand Down
22 changes: 20 additions & 2 deletions test/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as chai from 'chai';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import { env, Position, Selection, TextDocument, Uri, window } from 'vscode';
import { env, MessageItem, Position, Selection, TextDocument, Uri, window } from 'vscode';

import { Command } from '../src/command';
import { LinkHandler } from '../src/link-handler';
Expand Down Expand Up @@ -177,10 +177,28 @@ describe('Command', () => {

expect(showInformationMessage).to.have.been.calledWithExactly(
STRINGS.command.linkCopied(handler?.name ?? ''),
STRINGS.command.openInWeb
{ title: STRINGS.command.openInBrowser, action: 'open' }
);
});

it('should open the link in the browser when clicking on the success notification.', async () => {
let openItem: MessageItem & Record<string, unknown>;
let openExternal: sinon.SinonStub;

useTextEditor(undefined);
createUrl.resolves('http://example.com/foo/bar');

openExternal = sinon.stub(env, 'openExternal').resolves();

openItem = { title: STRINGS.command.openInBrowser, action: 'open' };
showInformationMessage.resolves(openItem);

command = new Command(finder, selector, 'commit', true);
await command.execute(file);

expect(openExternal).to.have.been.calledWith(Uri.parse('http://example.com/foo/bar'));
});

it('should use the active text editor to get the file when no resource was specified.', async () => {
useTextEditor(file);

Expand Down

0 comments on commit b614a65

Please sign in to comment.