-
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 21, 2018
1 parent
9965742
commit 7e7c359
Showing
10 changed files
with
346 additions
and
8 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
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,129 @@ | ||
// tslint:disable:no-any no-magic-numbers | ||
import { window } from 'vscode'; | ||
|
||
import { insert } from '../insert'; | ||
|
||
jest.mock('fs'); | ||
jest.mock('path'); | ||
|
||
const showQuickPickSpy = jest.spyOn(window, 'showQuickPick'); | ||
|
||
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() | ||
}, | ||
{ | ||
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() | ||
} | ||
]); | ||
|
||
const updateGistMock = jest.fn(); | ||
const getGistMock = jest.fn((id: string) => ({ | ||
createdAt: new Date(), | ||
description: 'some markdown file', | ||
fileCount: 1, | ||
files: { | ||
'file-one.md': { content: 'test' }, | ||
'file-two.md': { content: 'test' } | ||
}, | ||
id, | ||
name: 'test', | ||
public: true, | ||
updatedAt: new Date() | ||
})); | ||
const utilsMock = jest.genMockFromModule<Utils>('../../../utils'); | ||
const errorMock = jest.fn(); | ||
|
||
describe('open gist', () => { | ||
let insertFn: CommandFn; | ||
beforeEach(() => { | ||
const gists = { | ||
getGist: getGistMock, | ||
getGists: getGistsMock, | ||
updateGist: updateGistMock | ||
}; | ||
const insights = { exception: jest.fn() }; | ||
const logger = { debug: jest.fn(), error: errorMock, info: jest.fn() }; | ||
insertFn = insert({ gists, insights, logger } as any, utilsMock as any)[1]; | ||
window.activeTextEditor = <any>{ | ||
document: { getText: jest.fn() }, | ||
selection: { isEmpty: true } | ||
}; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('should log error when no editor', async () => { | ||
expect.assertions(1); | ||
|
||
(<any>window.activeTextEditor) = undefined; | ||
|
||
await insertFn(); | ||
expect(errorMock.mock.calls.length).toBe(1); | ||
}); | ||
test('what happens when errors occur', async () => { | ||
expect.assertions(1); | ||
|
||
(<any>utilsMock.input.quickPick).mockRejectedValueOnce(false); | ||
|
||
await insertFn(); | ||
expect(errorMock.mock.calls.length).toBe(1); | ||
}); | ||
test('it should prompt the user to select a gist', async () => { | ||
expect.assertions(1); | ||
|
||
await insertFn(); | ||
|
||
expect(utilsMock.input.quickPick).toHaveBeenCalledWith([ | ||
expect.any(Object), | ||
expect.any(Object) | ||
]); | ||
}); | ||
test('it should query for the users selected gist', async () => { | ||
expect.assertions(1); | ||
|
||
(<any>utilsMock.input.quickPick).mockResolvedValueOnce({ | ||
block: { id: '123' } | ||
}); | ||
|
||
await insertFn(); | ||
|
||
expect(getGistMock).toHaveBeenCalledWith('123'); | ||
}); | ||
test('it should prompt the user to select a file if more than one is available', async () => { | ||
expect.assertions(1); | ||
|
||
(<any>utilsMock.input.quickPick).mockResolvedValueOnce({ | ||
block: { id: '123' } | ||
}); | ||
|
||
await insertFn(); | ||
|
||
expect(showQuickPickSpy).toHaveBeenCalledWith([ | ||
{ | ||
description: '', | ||
'file-one.md': expect.any(Object), | ||
label: 'file-one.md' | ||
}, | ||
{ | ||
description: '', | ||
'file-two.md': expect.any(Object), | ||
label: 'file-two.md' | ||
} | ||
]); | ||
}); | ||
}); |
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,77 @@ | ||
// tslint:disable:no-any no-magic-numbers | ||
import { window, workspace } from 'vscode'; | ||
|
||
import { openGist, selectFile } from '../utils'; | ||
|
||
const showQuickPickSpy = jest.spyOn(window, 'showQuickPick'); | ||
const showTextDocumentSpy = jest.spyOn(window, 'showTextDocument'); | ||
const openTextDocumentMock = jest.spyOn(workspace, 'openTextDocument'); | ||
|
||
jest.mock('path'); | ||
jest.mock('fs'); | ||
|
||
describe('Gist Command Utils Tests', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
describe('#openGist', () => { | ||
test('it should open a gist in an editor', async () => { | ||
expect.assertions(3); | ||
|
||
const mockDoc = { | ||
getText(): string { | ||
return 'test-file-one'; | ||
} | ||
}; | ||
|
||
openTextDocumentMock.mockImplementationOnce((_filePath: string) => | ||
Promise.resolve(mockDoc) | ||
); | ||
|
||
await openGist({ | ||
fileCount: 1, | ||
files: { 'file-one.md': { content: 'test-file-one' } }, | ||
id: '123test' | ||
} as any); | ||
|
||
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1); | ||
expect(openTextDocumentMock).toHaveBeenCalledWith( | ||
expect.stringContaining('file-one.md') | ||
); | ||
expect(showTextDocumentSpy).toHaveBeenCalledWith(mockDoc); | ||
}); | ||
}); | ||
describe('#selectFile', () => { | ||
test('it accepts a list of gist files', async () => { | ||
expect.assertions(1); | ||
|
||
await selectFile({ | ||
files: { | ||
'file-one.md': { content: 'test-content' }, | ||
'file-two.md': { content: 'test-content' } | ||
} | ||
} as any); | ||
|
||
expect(showQuickPickSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
test('it returns a single file when the user selects one', async () => { | ||
expect.assertions(2); | ||
showQuickPickSpy.mockImplementationOnce((items: any[]) => | ||
Promise.resolve(items[0]) | ||
); | ||
|
||
const file = await selectFile({ | ||
files: { | ||
'file-one.md': { content: 'test-content-one' }, | ||
'file-two.md': { content: 'test-content-two' } | ||
} | ||
} as any); | ||
|
||
expect(showQuickPickSpy).toHaveBeenCalledTimes(1); | ||
expect(file).toStrictEqual({ | ||
content: 'test-content-one', | ||
filename: 'file-one.md' | ||
}); | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import { window } from 'vscode'; | ||
|
||
import { GistCommands } from '../extension-commands'; | ||
|
||
import { insertText, selectFile } from './utils'; | ||
|
||
const insert: CommandInitializer = ( | ||
services: Services, | ||
utils: Utils | ||
): [Command, CommandFn] => { | ||
const { gists, insights, logger } = services; | ||
|
||
const command = GistCommands.Insert; | ||
|
||
const commandFn = async (): Promise<void> => { | ||
try { | ||
const editor = window.activeTextEditor; | ||
if (!editor) { | ||
throw new Error('Open a file before inserting'); | ||
} | ||
|
||
const list = await gists.getGists(); | ||
const selected = await utils.input.quickPick(list); | ||
|
||
if (!selected) { | ||
services.logger.debug('No gist selected'); | ||
|
||
return; | ||
} | ||
|
||
const gist = await gists.getGist(selected.block.id); | ||
// selected file to insert | ||
const file = await selectFile(gist); | ||
if (!file) { | ||
services.logger.debug('No file selected'); | ||
|
||
return; | ||
} | ||
await insertText(editor, file.content); | ||
services.insights.track(command); | ||
} catch (err) { | ||
const error: Error = err as Error; | ||
logger.error(`${command} > ${error && error.message}`); | ||
insights.exception(command, { messsage: error.message }); | ||
utils.notify.error('Could Not Insert', `Reason: ${error.message}`); | ||
} | ||
}; | ||
|
||
return [command, commandFn]; | ||
}; | ||
|
||
export { insert }; |
Oops, something went wrong.