From a2168efcdafa424dca45c2bd1e6322a4776cfcdd Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 7 Apr 2016 16:47:17 +0100 Subject: [PATCH] Correctly restore MatrixClientPeg after each test case --- test/components/structures/MatrixChat-test.js | 15 ++++++++++----- test/components/structures/MessagePanel-test.js | 7 +++++++ test/test-utils.js | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/test/components/structures/MatrixChat-test.js b/test/components/structures/MatrixChat-test.js index 7d96b3a77a..68bc85dd27 100644 --- a/test/components/structures/MatrixChat-test.js +++ b/test/components/structures/MatrixChat-test.js @@ -3,16 +3,21 @@ var TestUtils = require('react-addons-test-utils'); var expect = require('expect'); var sdk = require('matrix-react-sdk'); +var MatrixChat = sdk.getComponent('structures.MatrixChat'); +var peg = require('../../../src/MatrixClientPeg'); var test_utils = require('../../test-utils'); -var peg = require('../../../src/MatrixClientPeg.js'); var q = require('q'); describe('MatrixChat', function () { - var MatrixChat; - before(function() { - test_utils.stubClient(); - MatrixChat = sdk.getComponent('structures.MatrixChat'); + var sandbox; + + beforeEach(function() { + sandbox = test_utils.stubClient(); + }); + + afterEach(function() { + sandbox.restore(); }); it('gives a login panel by default', function () { diff --git a/test/components/structures/MessagePanel-test.js b/test/components/structures/MessagePanel-test.js index 9a4996b5da..134cc4c34b 100644 --- a/test/components/structures/MessagePanel-test.js +++ b/test/components/structures/MessagePanel-test.js @@ -27,12 +27,19 @@ var test_utils = require('test-utils'); var mockclock = require('mock-clock'); describe('MessagePanel', function () { + var sandbox; var clock = mockclock.clock(); var realSetTimeout = window.setTimeout; var events = mkEvents(); + beforeEach(function() { + test_utils.beforeEach(this); + sandbox = test_utils.stubClient(sandbox); + }); + afterEach(function () { clock.uninstall(); + sandbox.restore(); }); function mkEvents() { diff --git a/test/test-utils.js b/test/test-utils.js index ed14306fbe..956c3c15b0 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -21,12 +21,23 @@ module.exports.beforeEach = function(context) { /** * Stub out the MatrixClient, and configure the MatrixClientPeg object to * return it when get() is called. + * + * @returns {sinon.Sandbox}; remember to call sandbox.restore afterwards. */ module.exports.stubClient = function() { - var pegstub = sinon.stub(peg); + var sandbox = sinon.sandbox.create(); + + // 'sandbox.restore()' doesn't work correctly on inherited methods, + // so we do this for each method + var methods = ['get', 'unset', 'replaceUsingUrls', + 'replaceUsingAccessToken']; + for (var i = 0; i < methods.length; i++) { + sandbox.stub(peg, methods[i]); + } var matrixClientStub = sinon.createStubInstance(jssdk.MatrixClient); - pegstub.get.returns(matrixClientStub); + peg.get.returns(matrixClientStub); + return sandbox; }