From bbcd9a152a37a77dda203198ce5bf5501ee19b58 Mon Sep 17 00:00:00 2001 From: Lucas Dohmen Date: Mon, 16 Sep 2013 14:39:55 +0200 Subject: [PATCH] Foxx Repository: byId (Test: Ugly) --- js/common/tests/shell-foxx-repository.js | 34 +++++++++++++++++++ .../modules/org/arangodb/foxx/repository.js | 4 ++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/js/common/tests/shell-foxx-repository.js b/js/common/tests/shell-foxx-repository.js index ed3d8aa1a2..283df1d51b 100644 --- a/js/common/tests/shell-foxx-repository.js +++ b/js/common/tests/shell-foxx-repository.js @@ -91,6 +91,7 @@ function RepositoryMethodsSpec() { ModelPrototype, model, modelData, + data, example, id, id_and_rev; @@ -102,6 +103,7 @@ function RepositoryMethodsSpec() { modelData = stub(); example = stub(); model = stub(); + data = stub(); collection = stub(); id = stub(); @@ -146,6 +148,38 @@ function RepositoryMethodsSpec() { instance.removeByExample(example); collection.assertIsSatisfied(); + }, + + testById: function () { + expect(collection) + .toReceive("document") + .withArguments(id); + + instance.byId(id); + + collection.assertIsSatisfied(); + }, + + testByIdReturnValue: function () { + var calledWith; + + allow(collection) + .toReceive("document") + .andReturn(data); + + ModelPrototype = function (arg) { + if (this.constructor === ModelPrototype) { + calledWith = arg; + } + }; + + instance = new FoxxRepository(collection, { model: ModelPrototype }); + + model = instance.byId(id); + + // This checks if it was called with the correct arguments + // and using new + assertEqual(calledWith, data); } }; } diff --git a/js/server/modules/org/arangodb/foxx/repository.js b/js/server/modules/org/arangodb/foxx/repository.js index 274324dbb7..e2327fcf15 100644 --- a/js/server/modules/org/arangodb/foxx/repository.js +++ b/js/server/modules/org/arangodb/foxx/repository.js @@ -184,8 +184,10 @@ _.extend(Repository.prototype, { /// /// See the documentation of collection. //////////////////////////////////////////////////////////////////////////////// - byId: function () { + byId: function (id) { 'use strict'; + var data = this.collection.document(id); + return (new this.modelPrototype(data)); }, ////////////////////////////////////////////////////////////////////////////////