From 2f51c130cc92b94ba35a2ae43c222c5514974e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Fri, 24 Aug 2018 11:32:41 +0300 Subject: [PATCH] Add sinon-test for easier test stub cleanup --- package-lock.json | 6 ++ package.json | 1 + scripts/config.js | 7 ++ test/content/runtests.html | 1 + test/content/runtests.js | 2 + test/tests/server_connectorTest.js | 112 ++++++++++++++--------------- 6 files changed, 71 insertions(+), 58 deletions(-) diff --git a/package-lock.json b/package-lock.json index df2d0f98f8..cc1afccf2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4634,6 +4634,12 @@ } } }, + "sinon-test": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/sinon-test/-/sinon-test-2.2.1.tgz", + "integrity": "sha512-j2X8jRz6GG/xBxCss3P36Q8QVdwV7DnOXOe4cNCsI0pbKRc2sErGTsHDimqZ1tiOmxw8ANog68T0SQq8hVMQRw==", + "dev": true + }, "slash": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz", diff --git a/package.json b/package.json index e78efebf7b..5f07cbf83b 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "multimatch": "^2.1.0", "node-sass": "^4.9.0", "sinon": "^4.5.0", + "sinon-test": "^2.2.1", "universalify": "^0.1.1" } } diff --git a/scripts/config.js b/scripts/config.js index a00979e1eb..3f688dc8b2 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -45,6 +45,13 @@ const browserifyConfigs = [ config: { standalone: 'chaiAsPromised' } + }, + { + src: 'node_modules/sinon-test/dist/sinon-test.js', + dest: 'test/resource/sinon-test.js', + config: { + standalone: 'sinonTest' + } } ]; diff --git a/test/content/runtests.html b/test/content/runtests.html index 0df4ea6a0b..00d4dcf9db 100644 --- a/test/content/runtests.html +++ b/test/content/runtests.html @@ -11,6 +11,7 @@ + diff --git a/test/content/runtests.js b/test/content/runtests.js index f41a911273..a839c60cf8 100644 --- a/test/content/runtests.js +++ b/test/content/runtests.js @@ -171,6 +171,8 @@ mocha.setup({ coMocha(Mocha); +sinon.test = sinonTest(sinon); + before(function () { // Store all prefs set in runtests.sh Components.utils.import("resource://zotero/config.js"); diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index 9f64b540ae..6b35275eef 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -790,66 +790,62 @@ describe("Connector Server", function () { assert.equal(item.getField('title'), 'Title'); }); - it("should save a PDF to the current selected collection and retrieve metadata", async function () { - try { - var collection = await createDataObject('collection'); - await waitForItemsLoad(win); + it("should save a PDF to the current selected collection and retrieve metadata", sinon.test(async function () { + var collection = await createDataObject('collection'); + await waitForItemsLoad(win); + + var file = getTestDataDirectory(); + file.append('test.pdf'); + httpd.registerFile("/test.pdf", file); + + var promise = waitForItemEvent('add'); + var recognizerPromise = waitForRecognizer(); + + var origRequest = Zotero.HTTP.request.bind(Zotero.HTTP); + var called = 0; + var stub = sinon.stub(Zotero.HTTP, 'request').callsFake(function (method, url, options) { + // Forward saveSnapshot request + if (url.endsWith('saveSnapshot')) { + return origRequest(...arguments); + } - var file = getTestDataDirectory(); - file.append('test.pdf'); - httpd.registerFile("/test.pdf", file); - - var promise = waitForItemEvent('add'); - var recognizerPromise = waitForRecognizer(); - - var origRequest = Zotero.HTTP.request.bind(Zotero.HTTP); - var called = 0; - var stub = sinon.stub(Zotero.HTTP, 'request').callsFake(function (method, url, options) { - // Forward saveSnapshot request - if (url.endsWith('saveSnapshot')) { - return origRequest(...arguments); - } - - // Fake recognizer response - return Zotero.Promise.resolve({ - getResponseHeader: () => {}, - responseText: JSON.stringify({ - title: 'Test', - authors: [] - }) - }); + // Fake recognizer response + return Zotero.Promise.resolve({ + getResponseHeader: () => {}, + responseText: JSON.stringify({ + title: 'Test', + authors: [] + }) }); - - await Zotero.HTTP.request( - 'POST', - connectorServerPath + "/connector/saveSnapshot", - { - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - url: testServerPath + "/test.pdf", - pdf: true - }) - } - ); - - var ids = await promise; - - assert.lengthOf(ids, 1); - var item = Zotero.Items.get(ids[0]); - assert.isTrue(item.isImportedAttachment()); - assert.equal(item.attachmentContentType, 'application/pdf'); - assert.isTrue(collection.hasItem(item.id)); - - var progressWindow = await recognizerPromise; - progressWindow.close(); - Zotero.RecognizePDF.cancel(); - assert.isFalse(item.isTopLevelItem()); - } finally { - stub.restore(); - } - }); + }); + + await Zotero.HTTP.request( + 'POST', + connectorServerPath + "/connector/saveSnapshot", + { + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + url: testServerPath + "/test.pdf", + pdf: true + }) + } + ); + + var ids = await promise; + + assert.lengthOf(ids, 1); + var item = Zotero.Items.get(ids[0]); + assert.isTrue(item.isImportedAttachment()); + assert.equal(item.attachmentContentType, 'application/pdf'); + assert.isTrue(collection.hasItem(item.id)); + + var progressWindow = await recognizerPromise; + progressWindow.close(); + Zotero.RecognizePDF.cancel(); + assert.isFalse(item.isTopLevelItem()); + })); it("should switch to My Library if a read-only library is selected", function* () { var group = yield createGroup({