Skip to content

Commit

Permalink
Refactored so reload on save handles the save event as well
Browse files Browse the repository at this point in the history
Since this has a one time setup this works better to include the vscode onDidSaveTextDocument with the reload callback.
  • Loading branch information
rkotze committed Jan 14, 2019
1 parent d0796a8 commit c005d37
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions __mocks__/vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const window = {

const workspace = {
getConfiguration: jest.fn(),
workspaceFolders: []
workspaceFolders: [],
onDidSaveTextDocument: jest.fn()
};

const OverviewRulerLane = {
Expand Down Expand Up @@ -53,4 +54,4 @@ const vscode = {
commands
};

exports = vscode;
module.exports = vscode;
3 changes: 1 addition & 2 deletions src/git-mob-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ function setupGitMob(context) {

reloadCommand({ coAuthorProvider });
openGitCoAuthor({ coAuthorProvider });
const { onDidSaveTextDocument } = vscode.workspace;
onDidSaveTextDocument(reloadOnSave(coAuthorProvider));
reloadOnSave(coAuthorProvider);

coAuthorProvider.loaded = function() {
mobList.onDidChangeVisibility(function({ visible }) {
Expand Down
7 changes: 5 additions & 2 deletions src/reload-on-save.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const vscode = require("vscode");
const { CONSTANTS } = require("./constants");

function reloadOnSave(coAuthorProvider) {
return function(textDocument) {
const { onDidSaveTextDocument } = vscode.workspace;

onDidSaveTextDocument(function(textDocument) {
if (textDocument.fileName.includes(CONSTANTS.GIT_COAUTHORS_FILE)) {
coAuthorProvider.mobAuthors.reset();
coAuthorProvider.reloadData();
}
};
});
}

exports.reloadOnSave = reloadOnSave;
21 changes: 15 additions & 6 deletions src/reload-on-save.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { reloadOnSave } = require("./reload-on-save");
const { CONSTANTS } = require("./constants");
let vscode = require("../__mocks__/vscode");

const coAuthorProviderStub = {
reloadData: jest.fn(),
Expand All @@ -14,23 +15,31 @@ const otherTextDocStub = {
fileName: `file/Path/to/other.js`
};

let saveTrigger = null;
beforeEach(() => {
vscode.workspace.onDidSaveTextDocument = jest.fn(cb => {
saveTrigger = cb;
});
});

afterEach(() => {
coAuthorProviderStub.reloadData.mockReset();
coAuthorProviderStub.mobAuthors.reset.mockReset();
saveTrigger = null;
});

test("Reload co-author list when git-coauthors file saved", () => {
const saveAction = reloadOnSave(coAuthorProviderStub);
expect(saveAction).toEqual(expect.any(Function));
saveAction(coAuthorTextDocStub);
reloadOnSave(coAuthorProviderStub);
expect(saveTrigger).toEqual(expect.any(Function));
saveTrigger(coAuthorTextDocStub);
expect(coAuthorProviderStub.reloadData).toHaveBeenCalledTimes(1);
expect(coAuthorProviderStub.mobAuthors.reset).toHaveBeenCalledTimes(1);
});

test("Do NOT reload co-author list when other files are saved", () => {
const saveAction = reloadOnSave(coAuthorProviderStub);
expect(saveAction).toEqual(expect.any(Function));
saveAction(otherTextDocStub);
reloadOnSave(coAuthorProviderStub);
expect(saveTrigger).toEqual(expect.any(Function));
saveTrigger(otherTextDocStub);
expect(coAuthorProviderStub.reloadData).not.toHaveBeenCalled();
expect(coAuthorProviderStub.mobAuthors.reset).not.toHaveBeenCalled();
});

0 comments on commit c005d37

Please sign in to comment.