Merge pull request #974 from realm/fix-sessions-tests

Fix sessions tests
This commit is contained in:
blagoev 2017-04-20 19:25:05 +03:00 committed by GitHub
commit 922a526fe9
2 changed files with 29 additions and 27 deletions

View File

@ -42,10 +42,6 @@ function promisifiedRegister(server, username, password) {
}); });
} }
function wait(delay) {
return new Promise((resolve, reject) => setTimeout(resolve, delay));
}
module.exports = { module.exports = {
testLocalRealmHasNoSession() { testLocalRealmHasNoSession() {
let realm = new Realm(); let realm = new Realm();
@ -55,13 +51,17 @@ module.exports = {
testProperties() { testProperties() {
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => { return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
return new Promise((resolve, _reject) => { return new Promise((resolve, _reject) => {
const accessTokenRefreshed = Symbol(); const accessTokenRefreshed = this;
let session;
let postTokenRefreshChecks = (sender, error) => { function postTokenRefreshChecks(sender, error) {
TestCase.assertEqual(error, accessTokenRefreshed); try {
TestCase.assertEqual(session.url, `realm://localhost:9080/${user.identity}/myrealm`); TestCase.assertEqual(error, accessTokenRefreshed);
resolve(); TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
resolve();
}
catch (e) {
_reject(e)
}
}; };
// Let the error handler trigger our checks when the access token was refreshed. // Let the error handler trigger our checks when the access token was refreshed.
@ -69,7 +69,8 @@ module.exports = {
const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } }; const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm', error: postTokenRefreshChecks } };
const realm = new Realm(config); const realm = new Realm(config);
session = realm.syncSession; const session = realm.syncSession;
TestCase.assertInstanceOf(session, Realm.Sync.Session); TestCase.assertInstanceOf(session, Realm.Sync.Session);
TestCase.assertEqual(session.user.identity, user.identity); TestCase.assertEqual(session.user.identity, user.identity);
@ -83,22 +84,23 @@ module.exports = {
testErrorHandling() { testErrorHandling() {
return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => { return promisifiedRegister('http://localhost:9080', uuid(), 'password').then(user => {
let errors = []; return new Promise((resolve, _reject) => {
let config = { sync: { user, const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } };
url: 'realm://localhost:9080/~/myrealm', config.sync.error = (sender, error) => {
error: (sender, error) => errors.push([sender, error]) try {
} }; TestCase.assertEqual(error.message, 'simulated error');
let realm = new Realm(config); TestCase.assertEqual(error.code, 123);
let session = realm.syncSession; resolve();
}
catch (e) {
_reject(e);
}
};
const realm = new Realm(config);
const session = realm.syncSession;
TestCase.assertEqual(session.config.error, config.sync.error); TestCase.assertEqual(session.config.error, config.sync.error);
session._simulateError(123, 'simulated error'); session._simulateError(123, 'simulated error');
return wait(2000).then(() => {
TestCase.assertArrayLength(errors, 1);
TestCase.assertEqual(errors[0][0].config.url, session.config.url);
TestCase.assertEqual(errors[0][1].message, 'simulated error');
TestCase.assertEqual(errors[0][1].code, 123);
}); });
}); });
} }

View File

@ -27,7 +27,7 @@ const path = require('path');
const Realm = require('realm'); const Realm = require('realm');
const RealmTests = require('../js'); const RealmTests = require('../js');
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
// Create this method with appropriate implementation for Node testing. // Create this method with appropriate implementation for Node testing.
Realm.copyBundledRealmFiles = function() { Realm.copyBundledRealmFiles = function() {