Skip to content

Add a test for nested resources from a addRestangularMethod result #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,24 +1242,28 @@ module.provider('Restangular', function() {
}

function addRestangularMethodFunction(name, operation, path, defaultParams, defaultHeaders, defaultElem) {
var bindedFunction;
if (operation === 'getList') {
bindedFunction = _.bind(fetchFunction, this, path);
} else {
bindedFunction = _.bind(customFunction, this, operation, path);
}

var createdFunction = function(params, headers, elem) {
var callParams = _.defaults({
params: params,
headers: headers,
elem: elem
}, {
params: defaultParams,
headers: defaultHeaders,
elem: defaultElem
});
return bindedFunction(callParams.params, callParams.headers, callParams.elem);
var newRestangularObject,
createdFunction,
urlBuilder = operation === 'getList' ? 'all' : 'one';

newRestangularObject = this[urlBuilder](path);

createdFunction = function(params, headers, elem) {
var params, headers, elem;

params = _.defaults(params, defaultParams);
headers = _.defaults(headers, defaultHeaders);
elem = _.defaults(elem, defaultElem);

return newRestangularObject.customOperation(
// umm, so the documentation specifies delete is one of the operations
// that can be pased to customOperation, but it totally means remove?
operation === 'delete' ? 'remove' : operation,
'',
params,
headers,
elem
);
};

if (config.isSafe(operation)) {
Expand Down
109 changes: 87 additions & 22 deletions test/restangularSpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe("Restangular", function() {
// API
var Restangular, $httpBackend;
var accountsModel, restangularAccounts, restangularAccount0, restangularAccount1;
var accountsModel, restangularAccounts, restangularAccount0, restangularAccount1, fooModel;
var accountsHalModel;
var messages, newAccount;

Expand All @@ -27,7 +27,11 @@ describe("Restangular", function() {

infoModel = {
id: 0, text: "Some additional account information"
}
};

fooModel = {
foo: 'bar'
};

newAccount = {id: 44, user: "First User", amount: 45, transactions: []};

Expand All @@ -43,6 +47,14 @@ describe("Restangular", function() {

$httpBackend.whenGET("/accounts").respond(accountsModel);
$httpBackend.whenGET("/accounts/do-something").respond(accountsDoSomethingModel);
$httpBackend.whenGET("/accounts/do-something/foo").respond(fooModel);
$httpBackend.whenPOST("/accounts/post-something").respond(fooModel);
$httpBackend.whenGET("/accounts/post-something/foo").respond(fooModel);
$httpBackend.whenPUT("/accounts/put-something").respond(fooModel);
$httpBackend.whenGET("/accounts/put-something/foo").respond(fooModel);
$httpBackend.whenPUT("/accounts/put-something/foo").respond(fooModel);
$httpBackend.whenDELETE("/accounts/delete-something").respond(fooModel);
$httpBackend.whenGET("/accounts/delete-something/foo").respond(fooModel);
$httpBackend.whenJSONP("/accounts").respond(accountsModel);
$httpBackend.whenGET("/accounts/0,1").respond(accountsModel);
$httpBackend.whenGET("/accounts/messages").respond(messages);
Expand Down Expand Up @@ -560,7 +572,6 @@ describe("Restangular", function() {
});

describe("Scoped Service", function() {

it("should correctly work", function() {
var Accounts = Restangular.service('accounts');
Accounts.post(newAccount);
Expand All @@ -585,29 +596,83 @@ describe("Restangular", function() {
$httpBackend.flush();
});

it("should add custom collection method added with withConfig", function() {
var Accounts = Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.addElementTransformer('accounts', true, function(worker) {
worker.addRestangularMethod('doSomething', 'get', 'do-something');
return worker;
describe("addRestangularMethod", function() {
var Accounts;
beforeEach(function() {
Accounts = Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.addElementTransformer('accounts', true, function(worker) {
worker.addRestangularMethod('doSomething', 'get', 'do-something');
worker.addRestangularMethod('postSomething', 'post', 'post-something');
worker.addRestangularMethod('getListSomething', 'getList', 'getList-something');
worker.addRestangularMethod('putSomething', 'put', 'put-something');
worker.addRestangularMethod('deleteSomething', 'delete', 'delete-something');
return worker;
});
}).service('accounts');
});

it("should add custom collection method added with withConfig", function() {
expect(Accounts.doSomething).toBeDefined();
expect(_.isFunction(Accounts.doSomething)).toBeTruthy();

Accounts.post(newAccount);
Accounts.one(0).get();
Accounts.getList();
Accounts.doSomething();

$httpBackend.expectPOST('/accounts');
$httpBackend.expectGET('/accounts/0');
$httpBackend.expectGET('/accounts');
$httpBackend.expectGET('/accounts/do-something');
$httpBackend.flush();
});

it("should have the correct url when using addRestangularMethod", function() {
expect(Accounts.doSomething).toBeDefined();
expect(_.isFunction(Accounts.doSomething)).toBeTruthy();

Accounts.doSomething().then(function(doSomething) {
doSomething.one('foo').get();
});
}).service('accounts');

expect(Accounts.doSomething).toBeDefined();
expect(_.isFunction(Accounts.doSomething)).toBeTruthy();
$httpBackend.expectGET('/accounts/do-something');
$httpBackend.expectGET('/accounts/do-something/foo');
$httpBackend.flush();
});

Accounts.post(newAccount);
Accounts.one(0).get();
Accounts.getList();
Accounts.doSomething();
it("should support post", function() {
Accounts.postSomething({foo: 'bar'}).then(function(postSomething) {
postSomething.one('foo').get();
});
$httpBackend.expectPOST('/accounts/post-something', {foo: 'bar'});
$httpBackend.expectGET('/accounts/post-something/foo');
$httpBackend.flush();
});

$httpBackend.expectPOST('/accounts');
$httpBackend.expectGET('/accounts/0');
$httpBackend.expectGET('/accounts');
$httpBackend.expectGET('/accounts/do-something');
$httpBackend.flush();
});
});
it("should support put", function() {
Accounts.putSomething({foo: 'bar'}).then(function(putSomething) {
putSomething.one('foo').get();
var a = putSomething.one('foo');
a.foo = 'bar';
a.put();
});
$httpBackend.expectPUT('/accounts/put-something', {foo: 'bar'});
$httpBackend.expectGET('/accounts/put-something/foo');
$httpBackend.expectPUT('/accounts/put-something/foo', {foo: 'bar'});
$httpBackend.flush();
});

it("should support delete", function() {
Accounts.deleteSomething().then(function(deleteSomething) {
deleteSomething.one('foo').get();
});

$httpBackend.expectDELETE('/accounts/delete-something');
$httpBackend.expectGET('/accounts/delete-something/foo');
$httpBackend.flush();
});
});
});

describe("ONE", function() {
it("get() should return a JSON item", function() {
Expand Down