From 5ff59b0c2342ddeed944cbe5dcca697190f9a2e7 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 14 Jun 2017 17:08:49 +0100 Subject: [PATCH] Tests: delete indexeddbs after running --- test/app-tests/loading.js | 11 +++++++++-- test/test-utils.js | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/test/app-tests/loading.js b/test/app-tests/loading.js index 0248986b..0132be3c 100644 --- a/test/app-tests/loading.js +++ b/test/app-tests/loading.js @@ -30,7 +30,7 @@ import sdk from 'matrix-react-sdk'; import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg'; import * as languageHandler from 'matrix-react-sdk/lib/languageHandler'; -import test_utils from '../test-utils'; +import * as test_utils from '../test-utils'; import MockHttpBackend from '../mock-request'; import {parseQs, parseQsFromFragment} from '../../src/vector/url_utils'; @@ -68,12 +68,19 @@ describe('loading:', function () { }); }); - afterEach(function() { + afterEach(async function() { if (parentDiv) { ReactDOM.unmountComponentAtNode(parentDiv); parentDiv.remove(); parentDiv = null; } + + // unmounting should have cleared the MatrixClientPeg + expect(MatrixClientPeg.get()).toBe(null); + + // clear the indexeddbs so we can start from a clean slate next time. + await test_utils.deleteIndexedDB('matrix-js-sdk:crypto'); + await test_utils.deleteIndexedDB('matrix-js-sdk:riot-web-sync'); }); /* simulate the load process done by index.js diff --git a/test/test-utils.js b/test/test-utils.js index 53528e5c..a1c1cd0a 100644 --- a/test/test-utils.js +++ b/test/test-utils.js @@ -7,7 +7,7 @@ var q = require('q'); * name to stdout. * @param {Mocha.Context} context The test context */ -module.exports.beforeEach = function(context) { +export function beforeEach(context) { var desc = context.currentTest.fullTitle(); console.log(); console.log(desc); @@ -16,13 +16,40 @@ module.exports.beforeEach = function(context) { // some tests store things in localstorage. Improve independence of tests // by making sure that they don't inherit any old state. window.localStorage.clear(); -}; +} /** * returns true if the current environment supports webrtc */ -module.exports.browserSupportsWebRTC = function() { +export function browserSupportsWebRTC() { var n = global.window.navigator; return n.getUserMedia || n.webkitGetUserMedia || n.mozGetUserMedia; -}; +} + +export function deleteIndexedDB(dbName) { + return new q.Promise((resolve, reject) => { + if (!window.indexedDB) { + resolve(); + return; + } + + console.log(`Removing indexeddb instance: ${dbName}`); + const req = window.indexedDB.deleteDatabase(dbName); + + req.onblocked = () => { + console.log(`can't yet delete indexeddb because it is open elsewhere`); + }; + + req.onerror = (ev) => { + reject(new Error( + "unable to delete indexeddb: " + ev.target.error, + )); + }; + + req.onsuccess = () => { + console.log(`Removed indexeddb instance: ${dbName}`); + resolve(); + }; + }); +}