refactor session tests

This commit is contained in:
Yavor Georgiev 2017-04-04 14:00:00 +02:00 committed by blagoev
parent daad5a43cd
commit 95da4d81c3
1 changed files with 16 additions and 24 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,12 +51,11 @@ 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); TestCase.assertEqual(error, accessTokenRefreshed);
TestCase.assertEqual(session.url, `realm://localhost:9080/${user.identity}/myrealm`); TestCase.assertEqual(sender.url, `realm://localhost:9080/${user.identity}/myrealm`);
resolve(); resolve();
}; };
@ -69,7 +64,7 @@ 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 +78,19 @@ 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) => {
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]) TestCase.assertEqual(sender.config.url, config.sync.url);
} }; TestCase.assertEqual(error.message, 'simulated error');
let realm = new Realm(config); TestCase.assertEqual(error.code, 123);
let session = realm.syncSession; resolve();
};
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);
}); });
}); });
} }