Skip to content

Commit

Permalink
feat: adds notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Howard committed Nov 21, 2018
1 parent 0865240 commit 3c9c0d7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
2 changes: 2 additions & 0 deletions __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = {
language: 'en-US'
},
window: {
showErrorMessage: jest.fn(),
showInformationMessage: jest.fn(),
showQuickPick: jest.fn()
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-gist",
"displayName": "Gist Extension",
"displayName": "Gists",
"description": "Create, open and edit Gists",
"version": "1.2.1",
"publisher": "kenhowardpdx",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/__tests__/gists.commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ describe('Gists Commands Tests', () => {
});
describe('#updateCodeBlock', () => {
test('should save', async () => {
expect.assertions(2);
expect.assertions(3);

await updateCodeBlock({
fileName: `${TMP_DIRECTORY_PREFIX}_123456789abcdefg_random_string/test-file-name.md`,
getText: jest.fn(() => 'test-file-content')
});

expect(editSpy.mock.calls.length).toBe(1);
expect(editSpy.mock.calls[0][0].gist_id).toBe('123456789abcdefg');
expect(editSpy.mock.calls[0][0].files).toStrictEqual({
'test-file-name.md': 'test-file-content'
Expand Down
15 changes: 7 additions & 8 deletions src/commands/gists.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { commands, window, workspace } from 'vscode';

import { getGist, getGists, updateGist } from '../gists';
import { logger } from '../logger';
import { extractTextDocumentDetails, filesSync } from '../utils';
import { extractTextDocumentDetails, filesSync, notify } from '../utils';

const _formatGistsForQuickPick = (gists: Gist[]): QuickPickGist[] =>
gists.map((item, i, j) => ({
Expand Down Expand Up @@ -33,11 +33,6 @@ const _openDocument = (file: string): Thenable<void> =>
.then(window.showTextDocument)
.then(() => commands.executeCommand('workbench.action.keepEditor'));

// TODO: Move _notify to utils
const _notify = (friendly: string, message: string): void => {
window.showInformationMessage(`GIST: ${friendly}> ${message}"`);
};

const openCodeBlock = async (): Promise<void> => {
let gistName = '';
try {
Expand Down Expand Up @@ -71,7 +66,10 @@ const openCodeBlock = async (): Promise<void> => {
const error: Error = err as Error;
logger.error(`openCodeBlock > ${error && error.message}`);
if (error && error.message === 'Not Found') {
_notify(`Could Not Open Gist ${gistName}>`, error.message);
notify.error(
`Could Not Open Gist ${gistName}`,
`Reason: ${error.message}`
);
}
}
};
Expand All @@ -84,14 +82,15 @@ const updateCodeBlock = async (doc: GistTextDocument): Promise<void> => {
if (id) {
logger.info(`Saving "${filename}"`);
await updateGist(id, filename, content);
notify.info(`Saved "${filename}"`);
} else {
logger.info(`"${filename}" Not a Gist`);
}
} catch (err) {
const error: Error = err as Error;
logger.error(`updateCodeBlock > ${error && error.message}`);
if (error && error.message === 'Not Found') {
_notify(`Could Not Save ${file}`, error.message);
notify.error(`Could Not Save ${file}`, `Reason: ${error.message}`);
}
}
};
Expand Down
36 changes: 36 additions & 0 deletions src/utils/__tests__/notify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { window } from 'vscode';

import { notify } from '../notify';

let showErrorSpy: jest.SpyInstance<typeof window.showErrorMessage>;
let showInfoSpy: jest.SpyInstance<
typeof window.showInformationMessage
> = jest.spyOn(window, 'showInformationMessage');

describe('Notify Tests', () => {
beforeEach(() => {
showErrorSpy = jest.spyOn(window, 'showErrorMessage');
showInfoSpy = jest.spyOn(window, 'showInformationMessage');
});
afterEach(() => {
jest.resetAllMocks();
});
describe('#error', () => {
test('should notify user of error', () => {
expect.assertions(1);

notify.error('Foo', 'Bar');

expect(showErrorSpy.mock.calls[0][0]).toBe('GIST ERROR: Foo > Bar');
});
});
describe('#info', () => {
test('should notify user of info', () => {
expect.assertions(1);

notify.info('Foo', 'Bar');

expect(showInfoSpy.mock.calls[0][0]).toBe('GIST: Foo > Bar');
});
});
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './file';
export * from './notify';
32 changes: 32 additions & 0 deletions src/utils/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { window } from 'vscode';

import { logger } from '../logger';

const _notify = (type: 'error' | 'info', ...messages: string[]): void => {
const formattedMessage = [...messages]
.filter((m?: string) => typeof m !== 'undefined')
.join(' > ');
switch (type) {
case 'error':
window.showErrorMessage(`GIST ERROR: ${formattedMessage}`);
break;
case 'info':
window.showInformationMessage(`GIST: ${formattedMessage}`);
break;
default:
logger.error(`Invalid Notify Type "${type}"`);
}
};

const error = (...messages: string[]): void => {
_notify('error', ...messages);
};

const info = (...messages: string[]): void => {
_notify('info', ...messages);
};

export const notify = {
error,
info
};

0 comments on commit 3c9c0d7

Please sign in to comment.