From 063e13ef22dce93c7bbe3275d7cee0b89511007b Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Wed, 31 May 2017 16:44:35 +0100 Subject: [PATCH] Modernize sinon.stub() calls --- test/tests/dataDirectoryTest.js | 6 +++--- test/tests/integrationTest.js | 14 ++++++++++---- test/tests/server_connectorTest.js | 2 +- test/tests/storageLocalTest.js | 8 ++++---- test/tests/styleTest.js | 2 +- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/test/tests/dataDirectoryTest.js b/test/tests/dataDirectoryTest.js index e0604292d9..a7166e6c52 100644 --- a/test/tests/dataDirectoryTest.js +++ b/test/tests/dataDirectoryTest.js @@ -188,7 +188,7 @@ describe("Zotero.DataDirectory", function () { yield populateDataDirectory(oldDir, null, automatic); let origFunc = OS.File.move; - let fileMoveStub = sinon.stub(OS.File, "move", function () { + let fileMoveStub = sinon.stub(OS.File, "move").callsFake(function () { if (OS.Path.basename(arguments[0]) == storageFile1) { return Zotero.Promise.reject(new Error("Error")); } @@ -241,7 +241,7 @@ describe("Zotero.DataDirectory", function () { yield populateDataDirectory(oldDir, null, automatic); let origFunc = OS.File.move; - let stub1 = sinon.stub(OS.File, "move", function () { + let stub1 = sinon.stub(OS.File, "move").callsFake(function () { if (OS.Path.basename(arguments[0]) == dbFilename) { return Zotero.Promise.reject(new Error("Error")); } @@ -371,7 +371,7 @@ describe("Zotero.DataDirectory", function () { yield populateDataDirectory(oldDir); let origFunc = OS.File.move; - let stub1 = sinon.stub(OS.File, "move", function () { + let stub1 = sinon.stub(OS.File, "move").callsFake(function () { if (OS.Path.basename(arguments[0]) == storageFile1) { return Zotero.Promise.reject(new Error("Error")); } diff --git a/test/tests/integrationTest.js b/test/tests/integrationTest.js index 80e315b16c..106bbb63c7 100644 --- a/test/tests/integrationTest.js +++ b/test/tests/integrationTest.js @@ -300,14 +300,20 @@ describe("Zotero.Integration", function () { } setAddEditItems(testItems[0]); - sinon.stub(Zotero.Integration, 'getApplication', function(agent, command, docID) { + sinon.stub(Zotero.Integration, 'getApplication').callsFake(function(agent, command, docID) { if (!applications[docID]) { applications[docID] = new DocumentPluginDummy.Application(); } return applications[docID]; }); - displayDialogStub = sinon.stub(Zotero.Integration, 'displayDialog', function(doc, dialogName, prefs, io) { + displayDialogStub = sinon.stub(Zotero.Integration, 'displayDialog', + // @TODO: This sinon.stub syntax is deprecated! + // However when using callsFake tests won't work. This is due to a + // possible bug that reset() erases callsFake. + // @NOTE: https://github.com/sinonjs/sinon/issues/1341 + // displayDialogStub.callsFake(function(doc, dialogName, prefs, io) { + function(doc, dialogName, prefs, io) { var ioResult = dialogResults[dialogName.substring(dialogName.lastIndexOf('/')+1, dialogName.length-4)]; if (typeof ioResult == 'function') { ioResult = ioResult(doc, dialogName); @@ -389,7 +395,7 @@ describe("Zotero.Integration", function () { var styleInstallStub = sinon.stub(Zotero.Styles, "install").resolves(); var style = Zotero.Styles.get(styleID); var styleGetCalledOnce = false; - var styleGetStub = sinon.stub(Zotero.Styles, 'get', function() { + var styleGetStub = sinon.stub(Zotero.Styles, 'get').callsFake(function() { if (!styleGetCalledOnce) { styleGetCalledOnce = true; return false; @@ -422,7 +428,7 @@ describe("Zotero.Integration", function () { var styleInstallStub = sinon.stub(Zotero.Styles, "install").resolves(); var style = Zotero.Styles.get(styleID); var styleGetCalledOnce = false; - var styleGetStub = sinon.stub(Zotero.Styles, 'get', function() { + var styleGetStub = sinon.stub(Zotero.Styles, 'get').callsFake(function() { if (!styleGetCalledOnce) { styleGetCalledOnce = true; return false; diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index dd4c28756a..f469129364 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -464,7 +464,7 @@ describe("Connector Server", function () { }); it('should import a style with application/vnd.citationstyles.style+xml content-type', function* () { - sinon.stub(Zotero.Styles, 'install', function(style) { + sinon.stub(Zotero.Styles, 'install').callsFake(function(style) { var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] .createInstance(Components.interfaces.nsIDOMParser), doc = parser.parseFromString(style, "application/xml"); diff --git a/test/tests/storageLocalTest.js b/test/tests/storageLocalTest.js index 75dc3d4b5c..2f4143156a 100644 --- a/test/tests/storageLocalTest.js +++ b/test/tests/storageLocalTest.js @@ -286,7 +286,7 @@ describe("Zotero.Sync.Storage.Local", function () { // Stub functions to simulate OS.Path.basename() behavior on Windows var basenameOrigFunc = OS.Path.basename.bind(OS.Path); - var basenameStub = sinon.stub(OS.Path, "basename", (path) => { + var basenameStub = sinon.stub(OS.Path, "basename").callsFake((path) => { // Split on colon if (path.endsWith("a:b.txt")) { return "b.txt"; @@ -294,7 +294,7 @@ describe("Zotero.Sync.Storage.Local", function () { return basenameOrigFunc(path); }); var pathToFileOrigFunc = Zotero.File.pathToFile.bind(Zotero.File); - var pathToFileStub = sinon.stub(Zotero.File, "pathToFile", (path) => { + var pathToFileStub = sinon.stub(Zotero.File, "pathToFile").callsFake((path) => { if (path.includes(":")) { throw new Error("Path contains colon"); } @@ -476,7 +476,7 @@ describe("Zotero.Sync.Storage.Local", function () { // Stub functions to simulate OS.Path.basename() behavior on Windows var basenameOrigFunc = OS.Path.basename.bind(OS.Path); - var basenameStub = sinon.stub(OS.Path, "basename", (path) => { + var basenameStub = sinon.stub(OS.Path, "basename").callsFake((path) => { // Split on colon if (path.endsWith("a:b.html")) { return "b.html"; @@ -484,7 +484,7 @@ describe("Zotero.Sync.Storage.Local", function () { return basenameOrigFunc(path); }); var pathToFileOrigFunc = Zotero.File.pathToFile.bind(Zotero.File); - var pathToFileStub = sinon.stub(Zotero.File, "pathToFile", (path) => { + var pathToFileStub = sinon.stub(Zotero.File, "pathToFile").callsFake((path) => { if (path.includes(":")) { throw new Error("Path contains colon"); } diff --git a/test/tests/styleTest.js b/test/tests/styleTest.js index 198750380c..7c76716092 100644 --- a/test/tests/styleTest.js +++ b/test/tests/styleTest.js @@ -27,7 +27,7 @@ describe("Zotero.Styles", function() { it("should install the style from url", function* () { var getContentsFromURLAsync = Zotero.File.getContentsFromURLAsync; - sinon.stub(Zotero.File, 'getContentsFromURLAsync', function(style) { + sinon.stub(Zotero.File, 'getContentsFromURLAsync').callsFake(function(style) { if (style.url == styleID) { return Zotero.Promise.resolve(style); } else {