Skip to content

Commit

Permalink
Allow a string argument when constructing an App
Browse files Browse the repository at this point in the history
  • Loading branch information
Kræn Hansen committed Jul 22, 2020
1 parent 1f3c13e commit c077045
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NOTE: Support for syncing with realm.cloud.io and/or Realm Object Server has bee
NOTE: This version bumps the Realm file format to version 11. It is not possible to downgrade to earlier versions. Older files will automatically be upgraded to the new file format. Files created by Realm JavaScript prior to v1.0.0, might not be upgradeable. Only [Realm Studio 10.0.0](https://github.com/realm/realm-studio/releases/tag/v10.0.0-beta.1) or later will be able to open the new file format.

### Enhancements
* None.
* Adding the possibility of passing a string argument when constructing an App. ([#3082](https://github.com/realm/realm-js/pull/3082))

### Fixed
* A crash when calling `user.apiKeys.fetchAll()`
Expand Down
4 changes: 2 additions & 2 deletions docs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@
/**
* Creates a new app and connects to a MongoDB Realm instance.
*
* @param {Realm.App~AppConfiguration} config - The configuration of the app.
* @param {(Realm.App~AppConfiguration|string)} configOrId - The configuration of the app or a string app id.
* @throws If no app id is provided.
*/
constructor(config) { }
constructor(configOrId) { }

/**
* Logs in a user.
Expand Down
4 changes: 4 additions & 0 deletions src/js_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ void AppClass<T>::constructor(ContextType ctx, ObjectType this_object, Arguments
config.local_app_version = util::Optional<std::string>(Value::validated_to_string(ctx, config_app_version_value, "version"));
}
}
} else if (Value::is_string(ctx, args[0])) {
config.app_id = Value::validated_to_string(ctx, args[0]);
} else {
throw std::runtime_error("Expected either a configuration object or an app id string.");
}

Protected<typename T::GlobalContext> protected_ctx(Context<T>::get_global_context(ctx));
Expand Down
22 changes: 22 additions & 0 deletions tests/js/app-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ module.exports = {
TestCase.assertInstanceOf(app, Realm.App);
},

testNewAppFromString() {
let app = new Realm.App(config.id);
TestCase.assertInstanceOf(app, Realm.App);
TestCase.assertEqual(app.id, config.id);
},

testNewAppFromUndefined() {
const error = TestCase.assertThrows(() => new Realm.App());
TestCase.assertEqual(
error.message,
'Invalid arguments: 1 expected, but 0 supplied.',
);
},

testNewAppFromOther() {
const error = TestCase.assertThrows(() => new Realm.App(1234));
TestCase.assertEqual(
error.message,
'Expected either a configuration object or an app id string.',
);
},

async testInvalidServer() {
const conf = {
id: 'smurf',
Expand Down

0 comments on commit c077045

Please sign in to comment.