-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ken Howard
committed
Dec 13, 2018
1 parent
ae0ac6f
commit d3666e8
Showing
10 changed files
with
157 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// tslint:disable:no-any no-magic-numbers | ||
import { commands, window } from 'vscode'; | ||
|
||
import { TMP_DIRECTORY_PREFIX } from '../../../constants'; | ||
import { openInBrowser } from '../open-in-browser'; | ||
|
||
jest.mock('fs'); | ||
jest.mock('path'); | ||
|
||
const getGistsMock = jest.fn(() => [ | ||
{ | ||
createdAt: new Date(), | ||
description: 'some markdown file', | ||
fileCount: 1, | ||
files: { 'file-one.md': { content: 'test' } }, | ||
id: '123', | ||
name: 'gist one', | ||
public: true, | ||
updatedAt: new Date(), | ||
url: 'gist-one-url' | ||
}, | ||
{ | ||
createdAt: new Date(), | ||
description: 'some markdown file', | ||
fileCount: 1, | ||
files: { 'file-two.md': { content: 'test' } }, | ||
id: '123', | ||
name: 'gist two', | ||
public: true, | ||
updatedAt: new Date(), | ||
url: 'gist-two-url' | ||
} | ||
]); | ||
const getGistMock = jest.fn((id: string) => ({ | ||
createdAt: new Date(), | ||
description: 'some markdown file', | ||
fileCount: 1, | ||
files: { 'file-one.md': { content: 'test' } }, | ||
id, | ||
name: 'test', | ||
public: true, | ||
updatedAt: new Date(), | ||
url: 'test-url' | ||
})); | ||
const utilsMock = jest.genMockFromModule<Utils>('../../../utils'); | ||
const errorMock = jest.fn(); | ||
|
||
const executeCommandSpy = jest.spyOn(commands, 'executeCommand'); | ||
|
||
describe('open favorite gist', () => { | ||
let openInBrowserFn: CommandFn; | ||
beforeEach(() => { | ||
const gists = { getGists: getGistsMock, getGist: getGistMock }; | ||
const insights = { exception: jest.fn() }; | ||
const logger = { error: errorMock, info: jest.fn() }; | ||
openInBrowserFn = openInBrowser( | ||
{ gists, insights, logger } as any, | ||
utilsMock as any | ||
)[1]; | ||
(<any>window).activeTextEditor = undefined; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('that a notification is shown when no open documents', async () => { | ||
expect.assertions(1); | ||
|
||
const infoSpy = jest.spyOn(utilsMock.notify, 'info'); | ||
|
||
await openInBrowserFn(); | ||
expect(infoSpy.mock.calls.length).toBe(1); | ||
}); | ||
test('it opens a browser', async () => { | ||
expect.assertions(2); | ||
|
||
(<any>utilsMock.files.extractTextDocumentDetails).mockImplementation( | ||
() => ({ id: '123456789abcdefg', url: 'test-url' }) | ||
); | ||
|
||
const codeBlock = { | ||
fileName: `${TMP_DIRECTORY_PREFIX}_123456789abcdefg_random_string/test-file-name.md`, | ||
getText: jest.fn(() => 'test-file-content') | ||
}; | ||
|
||
const editor = { | ||
document: codeBlock, | ||
selection: { isEmpty: true } | ||
}; | ||
|
||
(<any>window).activeTextEditor = editor; | ||
|
||
await openInBrowserFn(); | ||
|
||
expect(getGistMock).toHaveBeenCalledWith('123456789abcdefg'); | ||
expect(executeCommandSpy).toHaveBeenCalledWith('vscode.open', 'test-url'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './create'; | ||
export * from './open'; | ||
export * from './open-favorite'; | ||
export * from './open-in-browser'; | ||
export * from './update-access-key'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { commands, Uri, window } from 'vscode'; | ||
|
||
import { GistCommands } from '../extension-commands'; | ||
|
||
const openInBrowser: CommandInitializer = ( | ||
services: Services, | ||
utils: Utils | ||
): [Command, CommandFn] => { | ||
const { gists, insights, logger } = services; | ||
|
||
const command = GistCommands.OpenInBrowser; | ||
|
||
const commandFn = async (): Promise<void> => { | ||
const gistName = ''; | ||
try { | ||
logger.info(`User Activated ${command}`); | ||
|
||
const editor = window.activeTextEditor; | ||
|
||
if (!editor) { | ||
utils.notify.info('No open documents'); | ||
|
||
return; | ||
} | ||
|
||
const gistId = utils.files.extractTextDocumentDetails(editor.document).id; | ||
|
||
const gist = await gists.getGist(gistId); | ||
|
||
commands.executeCommand('vscode.open', Uri.parse(gist.url)); | ||
|
||
insights.track(command); | ||
} catch (err) { | ||
const error: Error = err as Error; | ||
logger.error(`${command} > ${error && error.message}`); | ||
insights.exception(command, { messsage: error.message }); | ||
if (error && error.message === 'Not Found') { | ||
utils.notify.error( | ||
`Could Not Open Gist ${gistName}`, | ||
`Reason: ${error.message}` | ||
); | ||
} else { | ||
utils.notify.error('Unable To Open Gists', error.message); | ||
} | ||
} | ||
}; | ||
|
||
return [command, commandFn]; | ||
}; | ||
|
||
export { openInBrowser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters