fix(copy) Copying collections now correctly sets route, fromServer an… · mgonto/restangular@7fd668b
1-/* global describe, beforeEach, inject, afterEach, it, expect, spyOn */
1+/* global describe, beforeEach, inject, afterEach, it, expect, spyOn, jasmine */
22/* jshint unused: false */
33describe('Restangular', function() {
44// API
@@ -952,18 +952,65 @@ describe('Restangular', function() {
952952});
953953954954it('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);
960964961965// with fromServer=false
962-var account = Restangular.one('accounts', 123),
966+account = Restangular.one('accounts', 123),
963967copiedAccount = Restangular.copy(account);
964-expect(account.fromServer).toEqual(copiedAccount.fromServer);
968+expect(account.fromServer).toEqual(false);
969+expect(copiedAccount.fromServer).toEqual(false);
970+});
965971972+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