From 5f3b82a767537cc07e136f588c1015e5548ebc1d Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 28 Mar 2016 22:59:34 +0100 Subject: [PATCH] Stub out the matrix client --- karma.conf.js | 33 ++++++++++++----- test/components/structures/MatrixChat-test.js | 9 ++++- test/test-utils.js | 36 +++++++++++++++++++ 3 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 test/test-utils.js diff --git a/karma.conf.js b/karma.conf.js index abd53f6afb..7a866d902d 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -7,7 +7,6 @@ var path = require('path'); * the easiest way to load our dependencies from node_modules. * * TODO: - * - how do we stub out the js-sdk * - can we run one test at a time * - can we can we run under phantomjs/jsdom? * - write junit out @@ -20,7 +19,14 @@ module.exports = function (config) { // list of files / patterns to load in the browser files: [ - 'test/tests.js' + 'test/tests.js', + ], + + // list of files to exclude + // (this doesn't work, and I don't know why - we still rerun the tests + // when lockfiles are created) + exclude: [ + '**/.#*' ], // preprocess matching files before serving them to the browser @@ -70,13 +76,21 @@ module.exports = function (config) { module: { loaders: [ { test: /\.json$/, loader: "json" }, - { test: /\.js$/, loader: "babel", - include: [path.resolve('./src'), - path.resolve('./test'), - ], - query: { - presets: ['react', 'es2015'] - }, + { + // disable 'require' and 'define' for sinon, per + // https://github.com/webpack/webpack/issues/304#issuecomment-170883329 + test: /sinon\/pkg\/sinon\.js/, + // TODO: use 'query'? + loader: 'imports?define=>false,require=>false', + }, + { + test: /\.js$/, loader: "babel", + include: [path.resolve('./src'), + path.resolve('./test'), + ], + query: { + presets: ['react', 'es2015'] + }, }, ], noParse: [ @@ -91,6 +105,7 @@ module.exports = function (config) { resolve: { alias: { 'matrix-react-sdk': path.resolve('src/index.js'), + 'sinon': 'sinon/pkg/sinon.js', }, }, devtool: 'inline-source-map', diff --git a/test/components/structures/MatrixChat-test.js b/test/components/structures/MatrixChat-test.js index 4d5c368873..46c76bb007 100644 --- a/test/components/structures/MatrixChat-test.js +++ b/test/components/structures/MatrixChat-test.js @@ -3,14 +3,21 @@ var TestUtils = require('react-addons-test-utils'); var expect = require('expect'); var sdk = require('matrix-react-sdk'); -var MatrixChat; + +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'); }); it('gives a login panel by default', function () { + peg.get().loginFlows.returns(q({})); + var res = TestUtils.renderIntoDocument( ); diff --git a/test/test-utils.js b/test/test-utils.js new file mode 100644 index 0000000000..7c0f38693a --- /dev/null +++ b/test/test-utils.js @@ -0,0 +1,36 @@ +"use strict"; + +var peg = require('../src/MatrixClientPeg.js'); +var jssdk = require('matrix-js-sdk'); +var sinon = require('sinon'); + +/** + * Stub out the MatrixClient, and configure the MatrixClientPeg object to + * return it when get() is called. + */ +module.exports.stubClient = function() { + var pegstub = sinon.stub(peg); + + var matrixClientStub = sinon.createStubInstance(jssdk.MatrixClient); + pegstub.get.returns(matrixClientStub); +} + + +/** + * make the test fail, with the given exception + * + *

This is useful for use with integration tests which use asyncronous + * methods: it can be added as a 'catch' handler in a promise chain. + * + * @param {Error} error exception to be reported + * + * @example + * it("should not throw", function(done) { + * asynchronousMethod().then(function() { + * // some tests + * }).catch(utils.failTest).done(done); + * }); + */ +module.exports.failTest = function(error) { + expect(error.stack).toBe(null); +};