From 6d485866f633e61d5026313456600154b15b7aef Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 10 Nov 2016 13:55:22 -0800 Subject: [PATCH] all users should return a dictionary --- src/js_sync.hpp | 7 +++---- tests/js/user-tests.js | 12 ++++++------ tests/spec/sync_tests.js | 5 +++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 21e0967f..ab279673 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -126,14 +126,13 @@ void UserClass::create_user(ContextType ctx, ObjectType this_object, size_t a template void UserClass::all_users(ContextType ctx, ObjectType object, ReturnValue &return_value) { - std::vector user_vector; - // TODO: This method should return a dictionary of shape {userid->user} + auto users = Object::create_empty(ctx); for (auto user : SyncManager::shared().all_users()) { if (user->state() == SyncUser::State::Active) { - user_vector.push_back(create_object>(ctx, new SharedUser(user))); + Object::set_property(ctx, users, user->identity(), create_object>(ctx, new SharedUser(user)), ReadOnly | DontDelete); } } - return_value.set(Object::create_array(ctx, user_vector)); + return_value.set(users); } template diff --git a/tests/js/user-tests.js b/tests/js/user-tests.js index def89e43..fc0236fb 100644 --- a/tests/js/user-tests.js +++ b/tests/js/user-tests.js @@ -199,19 +199,19 @@ module.exports = { return new Promise((resolve, reject) => { let all; all = Realm.Sync.User.all; - TestCase.assertArrayLength(all, 0); + TestCase.assertArrayLength(Object.keys(all), 0); Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user1) => { all = Realm.Sync.User.all; - TestCase.assertArrayLength(all, 1); - assertIsSameUser(all[0], user1); + TestCase.assertArrayLength(Object.keys(all), 1); + assertIsSameUser(all[user1.identity], user1); Realm.Sync.User.register('http://localhost:9080', uuid(), 'password', (error, user2) => { all = Realm.Sync.User.all; - TestCase.assertArrayLength(all, 2); + TestCase.assertArrayLength(Object.keys(all), 2); // NOTE: the list of users is in latest-first order. - assertIsSameUser(all[0], user2); - assertIsSameUser(all[1], user1); + assertIsSameUser(all[user2.identity], user2); + assertIsSameUser(all[user1.identity], user1); resolve(); }); }); diff --git a/tests/spec/sync_tests.js b/tests/spec/sync_tests.js index bad62e00..2f4fdd2a 100644 --- a/tests/spec/sync_tests.js +++ b/tests/spec/sync_tests.js @@ -29,8 +29,9 @@ describe('SyncTests', () => { beforeEach(() => Realm.clearTestState()); afterEach(() => { Realm.clearTestState(); - Realm.Sync.User.all.forEach((user) => { - user.logout(); + let users = Realm.Sync.User.all; + Object.keys(users).forEach((identity) => { + users[identity].logout(); }); });