Skip to content

Commit

Permalink
auto mixin ajax or managed ajax to base classes depending on availabi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
baccigalupi committed Aug 30, 2012
1 parent 078456c commit 051aef5
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 119 deletions.
178 changes: 73 additions & 105 deletions spec/javascripts/mixins/ajax_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,148 +4,116 @@ describe("Wheel.Mixins.Ajax", function () {
beforeEach(function() {
spyOn($, 'ajax');

Sender = Wheel.View.subclass({
init: function () {
this.url = 'http://csenderity.com';
},
// since the base class auto mixes in ManagedAjax,
// because the mixin is available, tests are going
// to the real Wheel._Class creator
Sender = Wheel._Class.subclass({
url: 'http://csenderity.com',
send: function (overrides) {
this.bar = 'foo';
this._super(overrides);
},
data: function() { return {}; },
onSuccess: function(response) { this.response = response; },
onSuccess: function(response) { this.response = response },
onCompletion: function(response) { this.response = response },
onError: function(response) { this.response = response }
},{
template: function() {
return "<a class='click' href='http://csenderity.com'>Csender</a>"
}
onError: function(response) { this.response = response }
});
Sender.mixin(Wheel.Mixins.Ajax);

sender = new Sender();
});

describe("send", function () {
describe('setting defaults', function() {
describe("requests", function() {
var args, data;
var sendWith = function(httpMethod) {
sender.httpMethod = httpMethod;
sender.send();
args = $.ajax.mostRecentCall.args[0];
};

beforeEach(function() {
sender.httpMethod = 'get';
sender.dataType = 'xml';
data = {foo: 'bar'};
sender.data = function() {return data};
});

describe('basic ajax attributes', function() {
beforeEach(function() {
sender.send();
sendWith('get');
});

describe("httpMethod", function () {
it("can be customized with initialization values", function () {
sender = new Sender({httpMethod: 'put'});
expect(sender.httpMethod).toBe('put');
});

it("can be set on the subclass", function () {
Sender.prototype.httpMethod = 'delete';
sender = new Sender();

expect(sender.httpMethod).toBe('delete');
});
it("calls _super", function () {
expect(sender.bar).toBe('foo');
});

describe("dataType", function () {
it("can be customized", function () {
sender = new Sender({dataType: 'xml'});
expect(sender.dataType).toBe('xml');
});
it("calls the request queue's send method", function () {
expect($.ajax).toHaveBeenCalled();
});
});

describe("requests", function() {
var args, data;
var sendWith = function(httpMethod) {
sender.httpMethod = httpMethod;
sender.send();
args = $.ajax.mostRecentCall.args[0];
};

beforeEach(function() {
sender.httpMethod = 'get';
sender.dataType = 'xml';
data = {foo: 'bar'};
sender.data = function() {return data};
it("uses the right url", function() {
expect(args.url).toBe('http://csenderity.com');
});

describe('basic ajax attributes', function() {
beforeEach(function() {
sendWith('get');
});

it("calls _super", function () {
expect(sender.bar).toBe('foo');
});

it("calls the request queue's send method", function () {
expect($.ajax).toHaveBeenCalled();
});
it("user the dataType", function () {
expect(args.dataType).toBe('xml');
});
});

it("uses the right url", function() {
expect(args.url).toBe('http://csenderity.com');
describe('with arguments', function() {
it('arguments overwrite options sent', function() {
sender.send({
type: 'HEAD',
async: false
});

it("user the dataType", function () {
expect(args.dataType).toBe('xml');
});
args = $.ajax.mostRecentCall.args[0];
expect(args.type).toBe('HEAD');
expect(args.async).toBe(false);
});

describe('with arguments', function() {
it('arguments overwrite options sent', function() {
sender.send({
type: 'HEAD',
async: false
});

args = $.ajax.mostRecentCall.args[0];
expect(args.type).toBe('HEAD');
expect(args.async).toBe(false);
it('will convert an httpMethod argument', function() {
sender.send({
httpMethod: 'HEAD',
async: false
});

it('will convert an httpMethod argument', function() {
sender.send({
httpMethod: 'HEAD',
async: false
});
args = $.ajax.mostRecentCall.args[0];
expect(args.type).toBe('HEAD');
expect(args.async).toBe(false);
});

args = $.ajax.mostRecentCall.args[0];
expect(args.type).toBe('HEAD');
expect(args.async).toBe(false);
it('will convert data arguments', function() {
sender.send({
data: {foo: 'bar'}
});

it('will convert data arguments', function() {
sender.send({
data: {foo: 'bar'}
});

args = $.ajax.mostRecentCall.args[0];
expect(args.data).toEqual({foo: 'bar'});
});
args = $.ajax.mostRecentCall.args[0];
expect(args.data).toEqual({foo: 'bar'});
});
});

describe('response handling', function() {
beforeEach(function() {
sendWith('get');
});
describe('response handling', function() {
beforeEach(function() {
sendWith('get');
});

it("binds the success handler", function () {
args.success('success response');
expect(sender.response).toBe('success response');
});
it("binds the success handler", function () {
args.success('success response');
expect(sender.response).toBe('success response');
});

it("registers an error handler", function () {
args.error({
responseText: '"error response"',
statusCode: function() { return 404; }
});
expect(sender.response).toBe('error response');
it("registers an error handler", function () {
args.error({
responseText: '"error response"',
statusCode: function() { return 404; }
});
expect(sender.response).toBe('error response');
});

it("registers a complete handler", function () {
args.complete('complete response');
expect(sender.response).toBe('complete response');
});
it("registers a complete handler", function () {
args.complete('complete response');
expect(sender.response).toBe('complete response');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/model_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('Wheel.Model', function() {
});

it('mixes in Ajax', function() {
expect(Task.prototype.send).toBe(Wheel.Mixins.Ajax.send);
expect(Task.prototype.send).toBe(Wheel.Mixins.ManagedAjax.send);
});

it('mashes in Events', function() {
Expand Down
4 changes: 0 additions & 4 deletions spec/javascripts/widgeteria/ajax_form_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ describe('Wheel.Widgeteria.AjaxForm', function() {
expect(form instanceof Wheel.Widgeteria.AjaxForm).toBe(true);
});

it("mixes in Ajax", function () {
expect(form.send).toBe(Wheel.Mixins.Ajax.send);
})

describe("onSubmit", function () {
var args, spy, data;
beforeEach(function() {
Expand Down
4 changes: 0 additions & 4 deletions spec/javascripts/widgeteria/ajax_link_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ describe("Wheel.Widgeteria.AjaxLink", function () {
expect(link instanceof Wheel.Widgeteria.AjaxLink).toBe(true);
});

it("mixes in Ajax", function () {
expect(link.send).toBe(Wheel.Mixins.Ajax.send);
})

describe("onClick", function () {
var args, spy, data;
beforeEach(function() {
Expand Down
6 changes: 6 additions & 0 deletions src/class/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ Wheel._Class.subclass('Wheel.Base', {

Wheel.Base.mixin(Wheel.Mixins.Events);

if (Wheel.Mixins['ManagedAjax']) {
Wheel.Base.mixin(Wheel.Mixins.ManagedAjax);
} else {
Wheel.Base.mixin(Wheel.Mixins.Ajax);
}

Wheel.Class = function(x, y, z) {
return Wheel.Base.subclass(x, y, z);
};
1 change: 0 additions & 1 deletion src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,4 @@ Wheel.Base.subclass('Wheel.Model', {
return models;
}
});
Wheel.Model.mixin(Wheel.Mixins.ManagedAjax);
Wheel.Model.mashin(Wheel.Mixins.Events);
2 changes: 0 additions & 2 deletions src/widgeteria/ajax_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ Wheel.Widgeteria.Form.subclass('Wheel.Widgeteria.AjaxForm', {
this.send();
}
});

Wheel.Widgeteria.AjaxForm.mixin(Wheel.Mixins.Ajax);
2 changes: 0 additions & 2 deletions src/widgeteria/ajax_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ Wheel.Widgeteria.Link.subclass('Wheel.Widgeteria.AjaxLink', {
this.send();
}
});

Wheel.Widgeteria.AjaxLink.mixin(Wheel.Mixins.Ajax);

0 comments on commit 051aef5

Please sign in to comment.