From 7fd668b099b61c45916621ed5ead3dc2da8f64b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Bostr=C3=B6m?= Date: Wed, 4 Jan 2017 22:38:45 +0200 Subject: [PATCH] fix(copy) Copying collections now correctly sets route, fromServer and parent on the copy --- src/restangular.js | 4 +-- test/restangularSpec.js | 61 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/restangular.js b/src/restangular.js index 64a08df2..62fdd1d7 100644 --- a/src/restangular.js +++ b/src/restangular.js @@ -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) { diff --git a/test/restangularSpec.js b/test/restangularSpec.js index a94dff14..354c630e 100644 --- a/test/restangularSpec.js +++ b/test/restangularSpec.js @@ -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 @@ -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'); }); });