test/unit/angularjs/rails/resourceSpec.js in angularjs-rails-resource-0.1.7 vs test/unit/angularjs/rails/resourceSpec.js in angularjs-rails-resource-0.2.0

- old
+ new

@@ -1,22 +1,34 @@ -describe("railsResourceFactory", function () { +describe('railsResourceFactory', function () { 'use strict'; - beforeEach(module('rails')); + beforeEach(function() { + module('rails'); + angular.module('rails').factory('railsTestInterceptor', function () { + return function (promise) { + return promise.then(function (response) { + response.data.interceptorAdded = 'x'; + return response; + }); + } + }); + }); + describe('singular', function() { - var $httpBackend, $rootScope, factory, Test, + var $httpBackend, $rootScope, factory, Test, testInterceptor, config = { url: '/test', name: 'test' }; - beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory) { + beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestInterceptor) { $httpBackend = _$httpBackend_; $rootScope = _$rootScope_; factory = railsResourceFactory; Test = railsResourceFactory(config); + testInterceptor = railsTestInterceptor; })); afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); @@ -151,52 +163,10 @@ expect(promise = resource.get(123)).toBeDefined(); $httpBackend.flush(); })); - it('should be able to turn off root mapping and field renaming', inject(function($httpBackend) { - var promise, result, resource; - - $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'}); - - resource = factory(config); - resource.responseInterceptors = []; - resource.requestTransformers = []; - expect(promise = resource.get(123)).toBeDefined(); - - promise.then(function (response) { - result = response; - }); - - $httpBackend.flush(); - - expect(result).toBeInstanceOf(resource); - expect(result).toEqualData({id: 123, abc_def: 'xyz'}); - })); - - it('should be able to turn off root mapping but keep field renaming', inject(function($httpBackend) { - var promise, result, resource, testConfig = {}; - - $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'}); - - angular.copy(config, testConfig); - testConfig.requestTransformers = []; - testConfig.responseInterceptors = ['railsFieldRenamingInterceptor']; - resource = factory(testConfig); - - expect(promise = resource.get(123)).toBeDefined(); - - promise.then(function (response) { - result = response; - }); - - $httpBackend.flush(); - - expect(result).toBeInstanceOf(resource); - expect(result).toEqualData({id: 123, abcDef: 'xyz'}); - })); - it('should be able to create new instance and save it', inject(function($httpBackend) { var data = new Test({abcDef: 'xyz'}); $httpBackend.expectPOST('/test', {test: {abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz'}}); data.create(); @@ -231,11 +201,11 @@ data.create(); $httpBackend.flush(1); expect(data).toEqualData({id: 123, abcDef: 'xyz'}); - $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: 'abc', abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}}); + $httpBackend.expectPUT('/test/123', {test: {abc_def: 'xyz', id: 123, xyz: 'abc'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}}); data.xyz = 'abc'; data.update(); $httpBackend.flush(); expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'}); @@ -248,10 +218,10 @@ data.save(); $httpBackend.flush(1); expect(data).toEqualData({id: 123, abcDef: 'xyz'}); - $httpBackend.expectPUT('/test/123', {test: {id: 123, xyz: 'abc', abc_def: 'xyz'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}}); + $httpBackend.expectPUT('/test/123', {test: {abc_def: 'xyz', id: 123, xyz: 'abc'}}).respond(200, {test: {id: 123, abc_def: 'xyz', xyz: 'abc', extra: 'test'}}); data.xyz = 'abc'; data.save(); $httpBackend.flush(); expect(data).toEqualData({id: 123, abcDef: 'xyz', xyz: 'abc', extra: 'test'});