-
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 17, 2018
1 parent
22ede07
commit 9d6afea
Showing
11 changed files
with
152 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// tslint:disable:no-any no-magic-numbers | ||
import { window } from 'vscode'; | ||
|
||
import { deleteFile } from '../delete-file'; | ||
|
||
const deleteFileMock = jest.fn(); | ||
const errorMock = jest.fn(); | ||
const utilsMock = jest.genMockFromModule<Utils>('../../../utils'); | ||
|
||
describe('open gist', () => { | ||
let deleteFileFn: CommandFn; | ||
beforeEach(() => { | ||
const gists = { deleteFile: deleteFileMock }; | ||
const insights = { exception: jest.fn() }; | ||
const logger = { error: errorMock, info: jest.fn() }; | ||
deleteFileFn = deleteFile( | ||
{ gists, insights, logger } as any, | ||
utilsMock as any | ||
)[1]; | ||
(<any>window).activeTextEditor = undefined; | ||
(<any>window).visibleTextEditors = []; | ||
(<any>utilsMock.files.extractTextDocumentDetails).mockReturnValue({ | ||
filename: 'foo', | ||
id: '123' | ||
}); | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('what happens when errors occur', async () => { | ||
expect.assertions(1); | ||
|
||
(<any>window).activeTextEditor = { document: {} }; | ||
|
||
(<any>utilsMock.input.prompt).mockResolvedValueOnce('DELETE'); | ||
deleteFileMock.mockRejectedValueOnce(false); | ||
|
||
await deleteFileFn(); | ||
expect(errorMock.mock.calls.length).toBe(1); | ||
}); | ||
test('it deletes the open file', async () => { | ||
expect.assertions(1); | ||
|
||
(<any>window).activeTextEditor = { document: { gist: { id: '123' } } }; | ||
(<any>utilsMock.input.prompt).mockResolvedValueOnce('DELETE'); | ||
|
||
await deleteFileFn(); | ||
|
||
expect(deleteFileMock).toHaveBeenCalledWith('123', 'foo'); | ||
}); | ||
}); |
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,50 @@ | ||
import { commands, window } from 'vscode'; | ||
|
||
import { GistCommands } from '../extension-commands'; | ||
|
||
const deleteFile: CommandInitializer = ( | ||
services: Services, | ||
utils: Utils | ||
): [Command, CommandFn] => { | ||
const { gists, insights, logger } = services; | ||
|
||
const command = GistCommands.DeleteFile; | ||
|
||
const commandFn = async (): Promise<void> => { | ||
try { | ||
const editor = window.activeTextEditor; | ||
const doc: undefined | GistTextDocument = editor && editor.document; | ||
if (!doc) { | ||
throw new Error('Document Missing'); | ||
} | ||
const { id, filename } = utils.files.extractTextDocumentDetails(doc); | ||
if (id) { | ||
const canDelete = | ||
(await utils.input.prompt('Enter "DELETE" to confirm')) === 'DELETE'; | ||
if (!canDelete) { | ||
logger.info('User Aborted Deletion'); | ||
|
||
return; | ||
} | ||
logger.info(`Deleting File "${id}"`); | ||
// tslint:disable-next-line:no-null-keyword | ||
await gists.deleteFile(id, filename); | ||
commands.executeCommand('workbench.action.closeActiveEditor'); | ||
utils.notify.info('Deleted File'); | ||
insights.track(command); | ||
} else { | ||
logger.info(`"${doc.fileName}" Not a Gist`); | ||
utils.notify.info('Document Is Not a Gist'); | ||
} | ||
} 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 Delete File', `Reason: ${error.message}`); | ||
} | ||
}; | ||
|
||
return [command, commandFn]; | ||
}; | ||
|
||
export { deleteFile }; |
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