Skip to content

Commit 7fd668b

Browse files
committed
fix(copy) Copying collections now correctly sets route, fromServer and parent on the copy
1 parent 1a988cb commit 7fd668b

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

Diff for: src/restangular.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,8 @@
998998

999999
function copyRestangularizedElement(fromElement, toElement) {
10001000
var copiedElement = angular.copy(fromElement, toElement);
1001-
return restangularizeElem(copiedElement[config.restangularFields.parentResource],
1002-
copiedElement, copiedElement[config.restangularFields.route], copiedElement[config.restangularFields.fromServer]);
1001+
return restangularizeElem(fromElement[config.restangularFields.parentResource],
1002+
copiedElement, fromElement[config.restangularFields.route], fromElement[config.restangularFields.fromServer]);
10031003
}
10041004

10051005
function restangularizeElem(parent, element, route, fromServer, collection, reqParams) {

Diff for: test/restangularSpec.js

+54-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, beforeEach, inject, afterEach, it, expect, spyOn */
1+
/* global describe, beforeEach, inject, afterEach, it, expect, spyOn, jasmine */
22
/* jshint unused: false */
33
describe('Restangular', function() {
44
// API
@@ -952,18 +952,65 @@ describe('Restangular', function() {
952952
});
953953

954954
it('should copy an object and "fromServer" param should be the same with the copied object', function() {
955+
var responseHandler = jasmine.createSpy();
956+
955957
// with fromServer=true
956-
restangularAccount1.get().then(function(account) {
957-
var copiedAccount = Restangular.copy(account);
958-
expect(account.fromServer).toEqual(copiedAccount.fromServer);
959-
});
958+
restangularAccount1.get().then(responseHandler);
959+
$httpBackend.flush();
960+
var account = responseHandler.calls[0].args[0],
961+
copiedAccount = Restangular.copy(account);
962+
expect(account.fromServer).toEqual(true);
963+
expect(copiedAccount.fromServer).toEqual(true);
960964

961965
// with fromServer=false
962-
var account = Restangular.one('accounts', 123),
966+
account = Restangular.one('accounts', 123),
963967
copiedAccount = Restangular.copy(account);
964-
expect(account.fromServer).toEqual(copiedAccount.fromServer);
968+
expect(account.fromServer).toEqual(false);
969+
expect(copiedAccount.fromServer).toEqual(false);
970+
});
965971

972+
it('should copy a collection and "fromServer" param should stay the same', function () {
973+
var responseHandler = jasmine.createSpy();
974+
975+
// with collections, fromServer=false
976+
var accounts = Restangular.all('accounts'),
977+
copiedAccounts = Restangular.copy(accounts);
978+
expect(accounts.fromServer).toEqual(false);
979+
expect(copiedAccounts.fromServer).toEqual(false);
980+
981+
// with collections, fromServer = true;
982+
restangularAccounts.getList().then(responseHandler);
966983
$httpBackend.flush();
984+
accounts = responseHandler.calls[0].args[0],
985+
copiedAccounts = Restangular.copy(accounts);
986+
expect(accounts.fromServer).toEqual(true);
987+
expect(copiedAccounts.fromServer).toEqual(true);
988+
});
989+
990+
it('should copy an object and "route" param should be the same in the copied object', function () {
991+
// for element
992+
var account = Restangular.one('accounts', 123),
993+
copiedAccount = Restangular.copy(account);
994+
expect(account.route).toEqual(copiedAccount.route);
995+
996+
// for collection
997+
var accounts = Restangular.all('accounts'),
998+
copiedAccounts = Restangular.copy(accounts);
999+
expect(accounts.route).toEqual(copiedAccounts.route);
1000+
});
1001+
1002+
it('should copy an object and the parent property should stay the same', function () {
1003+
// element
1004+
var user = Restangular.one('account', 12).one('user', 14),
1005+
userCopy = Restangular.copy(user);
1006+
expect(user.parentResource.route).toEqual('account');
1007+
expect(userCopy.parentResource.route).toEqual('account');
1008+
1009+
// collection
1010+
var users = Restangular.one('account', 12).all('users'),
1011+
usersCopy = Restangular.copy(users);
1012+
expect(user.parentResource.route).toEqual('account');
1013+
expect(usersCopy.parentResource.route).toEqual('account');
9671014
});
9681015
});
9691016

0 commit comments

Comments
 (0)