Skip to content

Commit

Permalink
fix(copy) Copying collections now correctly sets route, fromServer an…
Browse files Browse the repository at this point in the history
…d parent on the copy
  • Loading branch information
bostrom committed Jan 4, 2017
1 parent 1a988cb commit 7fd668b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,8 @@

function copyRestangularizedElement(fromElement, toElement) {
var copiedElement = angular.copy(fromElement, toElement);
return restangularizeElem(copiedElement[config.restangularFields.parentResource],
copiedElement, copiedElement[config.restangularFields.route], copiedElement[config.restangularFields.fromServer]);
return restangularizeElem(fromElement[config.restangularFields.parentResource],
copiedElement, fromElement[config.restangularFields.route], fromElement[config.restangularFields.fromServer]);
}

function restangularizeElem(parent, element, route, fromServer, collection, reqParams) {
Expand Down
61 changes: 54 additions & 7 deletions test/restangularSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, beforeEach, inject, afterEach, it, expect, spyOn */
/* global describe, beforeEach, inject, afterEach, it, expect, spyOn, jasmine */
/* jshint unused: false */
describe('Restangular', function() {
// API
Expand Down Expand Up @@ -952,18 +952,65 @@ describe('Restangular', function() {
});

it('should copy an object and "fromServer" param should be the same with the copied object', function() {
var responseHandler = jasmine.createSpy();

// with fromServer=true
restangularAccount1.get().then(function(account) {
var copiedAccount = Restangular.copy(account);
expect(account.fromServer).toEqual(copiedAccount.fromServer);
});
restangularAccount1.get().then(responseHandler);
$httpBackend.flush();
var account = responseHandler.calls[0].args[0],
copiedAccount = Restangular.copy(account);
expect(account.fromServer).toEqual(true);
expect(copiedAccount.fromServer).toEqual(true);

// with fromServer=false
var account = Restangular.one('accounts', 123),
account = Restangular.one('accounts', 123),
copiedAccount = Restangular.copy(account);
expect(account.fromServer).toEqual(copiedAccount.fromServer);
expect(account.fromServer).toEqual(false);
expect(copiedAccount.fromServer).toEqual(false);
});

it('should copy a collection and "fromServer" param should stay the same', function () {
var responseHandler = jasmine.createSpy();

// with collections, fromServer=false
var accounts = Restangular.all('accounts'),
copiedAccounts = Restangular.copy(accounts);
expect(accounts.fromServer).toEqual(false);
expect(copiedAccounts.fromServer).toEqual(false);

// with collections, fromServer = true;
restangularAccounts.getList().then(responseHandler);
$httpBackend.flush();
accounts = responseHandler.calls[0].args[0],
copiedAccounts = Restangular.copy(accounts);
expect(accounts.fromServer).toEqual(true);
expect(copiedAccounts.fromServer).toEqual(true);
});

it('should copy an object and "route" param should be the same in the copied object', function () {
// for element
var account = Restangular.one('accounts', 123),
copiedAccount = Restangular.copy(account);
expect(account.route).toEqual(copiedAccount.route);

// for collection
var accounts = Restangular.all('accounts'),
copiedAccounts = Restangular.copy(accounts);
expect(accounts.route).toEqual(copiedAccounts.route);
});

it('should copy an object and the parent property should stay the same', function () {
// element
var user = Restangular.one('account', 12).one('user', 14),
userCopy = Restangular.copy(user);
expect(user.parentResource.route).toEqual('account');
expect(userCopy.parentResource.route).toEqual('account');

// collection
var users = Restangular.one('account', 12).all('users'),
usersCopy = Restangular.copy(users);
expect(user.parentResource.route).toEqual('account');
expect(usersCopy.parentResource.route).toEqual('account');
});
});

Expand Down

0 comments on commit 7fd668b

Please sign in to comment.