test/unit/angularjs/rails/interceptorsSpec.js in angularjs-rails-resource-0.2.0 vs test/unit/angularjs/rails/interceptorsSpec.js in angularjs-rails-resource-0.2.1
- old
+ new
@@ -1,105 +1,235 @@
describe('transformers', function () {
'use strict';
- var $httpBackend, $rootScope, factory, Test, testInterceptor,
+ var $httpBackend, $rootScope, factory, Test, testInterceptor, testAfterInterceptor,
config = {
url: '/test',
name: 'test'
};
- beforeEach(function() {
+ beforeEach(function () {
module('rails');
angular.module('rails').factory('railsTestInterceptor', function () {
return function (promise) {
return promise.then(function (response) {
response.data.interceptorAdded = 'x';
return response;
});
}
});
+
+ angular.module('rails').factory('railsTestAfterInterceptor', function () {
+ return function (promise) {
+ return promise.then(function (resource) {
+ resource.interceptorAdded = 'x';
+ return resource;
+ });
+ }
+ });
});
- beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestInterceptor) {
+ beforeEach(inject(function (_$httpBackend_, _$rootScope_, railsResourceFactory, railsTestInterceptor, railsTestAfterInterceptor) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
factory = railsResourceFactory;
Test = railsResourceFactory(config);
testInterceptor = railsTestInterceptor;
+ testAfterInterceptor = railsTestAfterInterceptor;
}));
- afterEach(function() {
+ afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
+ describe('before response', function () {
+ it('should be able to reference interceptor using name', function () {
+ var promise, result, Resource, testConfig = {};
- it('should be able to reference interceptor using name', function() {
- var promise, result, Resource, testConfig = {};
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
- $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
+ angular.copy(config, testConfig);
+ testConfig.responseInterceptors = ['railsTestInterceptor'];
+ Resource = factory(testConfig);
- angular.copy(config, testConfig);
- testConfig.responseInterceptors = ['railsTestInterceptor'];
- Resource = factory(testConfig);
+ expect(promise = Resource.get(123)).toBeDefined();
- expect(promise = Resource.get(123)).toBeDefined();
+ promise.then(function (response) {
+ result = response;
+ });
- promise.then(function (response) {
- result = response;
+ $httpBackend.flush();
+
+ expect(result).toBeInstanceOf(Resource);
+ expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
});
- $httpBackend.flush();
+ it('should be able to add interceptor using reference', function () {
+ var promise, result, Resource, testConfig = {};
- expect(result).toBeInstanceOf(Resource);
- expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
+
+
+ angular.copy(config, testConfig);
+ testConfig.responseInterceptors = [testInterceptor];
+ Resource = factory(testConfig);
+
+ expect(promise = Resource.get(123)).toBeDefined();
+
+ promise.then(function (response) {
+ result = response;
+ });
+
+ $httpBackend.flush();
+
+ expect(result).toBeInstanceOf(Resource);
+ expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
+ });
+
+ it('should be able to add interceptor using beforeResponse', function () {
+ var promise, result, Resource, interceptorCalled = false;
+
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
+
+ Resource = factory(config);
+
+ Resource.beforeResponse(function (resource, constructor, context) {
+ expect(resource).toEqualData({id: 123, abcDef: 'xyz'});
+ expect(constructor).toEqual(Resource);
+ expect(context).toBeUndefined();
+ interceptorCalled = true;
+ });
+
+ 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'});
+ expect(interceptorCalled).toBeTruthy();
+ });
+
+ it('should set context to resource instance', function () {
+ var instance, Resource, interceptorCalled = false;
+
+ $httpBackend.expectPOST('/test').respond(200, {id: 123, abc_def: 'xyz'});
+
+ Resource = factory(config);
+
+ instance = new Resource({abcDef: 'xyz'});
+
+ Resource.beforeResponse(function (resource, constructor, context) {
+ expect(resource).toEqualData({id: 123, abcDef: 'xyz'});
+ expect(constructor).toEqual(Resource);
+ expect(context).toBeInstanceOf(Resource);
+ expect(context).toEqualData(instance);
+ interceptorCalled = true;
+ });
+
+ instance.save();
+
+ $httpBackend.flush();
+
+ expect(interceptorCalled).toBeTruthy();
+ });
});
- it('should be able to add interceptor using reference', function() {
- var promise, result, Resource, testConfig = {};
+ describe('after response', function () {
+ it('should be able to reference interceptor using name', function () {
+ var promise, result, Resource, testConfig = {};
- $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
+ angular.copy(config, testConfig);
+ testConfig.afterResponseInterceptors = ['railsTestAfterInterceptor'];
+ Resource = factory(testConfig);
- angular.copy(config, testConfig);
- testConfig.responseInterceptors = [testInterceptor];
- Resource = factory(testConfig);
+ expect(promise = Resource.get(123)).toBeDefined();
- expect(promise = Resource.get(123)).toBeDefined();
+ promise.then(function (response) {
+ result = response;
+ });
- promise.then(function (response) {
- result = response;
+ $httpBackend.flush();
+
+ expect(result).toBeInstanceOf(Resource);
+ expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
});
- $httpBackend.flush();
+ it('should be able to add interceptor using reference', function () {
+ var promise, result, Resource, testConfig = {};
- expect(result).toBeInstanceOf(Resource);
- expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
- });
-
- it('should be able to add interceptor using beforeResponse', function() {
- var promise, result, Resource, interceptorCalled = false;
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
- $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
- Resource = factory(config);
+ angular.copy(config, testConfig);
+ testConfig.afterResponseInterceptors = [testAfterInterceptor];
+ Resource = factory(testConfig);
- Resource.beforeResponse(function (resource, constructor) {
- expect(resource).toEqualData({id: 123, abcDef: 'xyz'});
- expect(constructor).toEqual(Resource);
- interceptorCalled = true;
+ expect(promise = Resource.get(123)).toBeDefined();
+
+ promise.then(function (response) {
+ result = response;
+ });
+
+ $httpBackend.flush();
+
+ expect(result).toBeInstanceOf(Resource);
+ expect(result).toEqualData({interceptorAdded: 'x', id: 123, abcDef: 'xyz'});
});
- expect(promise = Resource.get(123)).toBeDefined();
+ it('should be able to add interceptor using afterResponse', function () {
+ var promise, result, Resource, interceptorCalled = false;
- promise.then(function (response) {
- result = response;
+ $httpBackend.expectGET('/test/123').respond(200, {id: 123, abc_def: 'xyz'});
+
+ Resource = factory(config);
+
+ Resource.afterResponse(function (resource, constructor, context) {
+ expect(resource).toEqualData({id: 123, abcDef: 'xyz'});
+ expect(constructor).toEqual(Resource);
+ interceptorCalled = true;
+ });
+
+ 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'});
+ expect(interceptorCalled).toBeTruthy();
});
- $httpBackend.flush();
+ it('should set context to resource instance', function () {
+ var instance, Resource, interceptorCalled = false;
- expect(result).toBeInstanceOf(Resource);
- expect(result).toEqualData({id: 123, abcDef: 'xyz'});
- expect(interceptorCalled).toBeTruthy();
+ $httpBackend.expectPOST('/test').respond(200, {id: 123, abc_def: 'xyz'});
+
+ Resource = factory(config);
+
+ instance = new Resource({abcDef: 'xyz'});
+
+ Resource.afterResponse(function (resource, constructor, context) {
+ expect(resource).toEqualData({id: 123, abcDef: 'xyz'});
+ expect(constructor).toEqual(Resource);
+ interceptorCalled = true;
+ });
+
+ instance.save();
+
+ $httpBackend.flush();
+
+ expect(interceptorCalled).toBeTruthy();
+ });
});
+
});