Skip to content

Commit

Permalink
add test for serverSync
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Schreck committed Feb 7, 2014
1 parent 34e4338 commit e1271d9
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 48 deletions.
1 change: 1 addition & 0 deletions shared/syncer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function serverSync(method, model, options) {
}

syncer.clientSync = clientSync;
syncer.serverSync = serverSync;
syncer.sync = function sync() {
var syncMethod = isServer ? serverSync : clientSync;
return syncMethod.apply(this, arguments);
Expand Down
148 changes: 100 additions & 48 deletions test/shared/syncer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,82 +12,134 @@ chai.use(sinonChai);

describe('syncer', function() {

describe('clientSync', function () {
var model, backboneSync, options, syncErrorHandler;
describe('sync', function () {
var model, options, app;

beforeEach(function () {
model = new BaseModel({ id: 0 }, { app: new App() });
app = new App();

model = new BaseModel({ id: 0 }, { app: app });
model.urlRoot = '/listings';

options = { url: model.url() };

backboneSync = sinon.stub(Backbone, 'sync');
syncErrorHandler = sinon.spy();
});

afterEach(function () {
backboneSync.restore();
});
describe('serverSync', function () {
var request;

it('should call Backbone.sync', function () {
syncer.clientSync.call(model, 'get', model, options);
beforeEach(function () {
request = sinon.stub();
app.req = { dataAdapter: { request: request } };

backboneSync.should.have.been.calledOnce;
backboneSync.should.have.been.calledWithExactly('get', model, options);
});
model.api = 'foo'
});

it('should get the prefixed API url', function () {
syncer.clientSync.call(model, 'get', model, options);
backboneSync.should.have.been.calledWithExactly('get', model, { url: '/api/-' + model.url() });
});
it('should send a GET request with the configured dataAdapter', function () {
var expectedRequestOptions = {
method: 'GET',
path: '/listings/0',
query: {},
api: 'foo',
body: {}
};

it('should wrap the error handler', function () {
options.error = syncErrorHandler;
syncer.serverSync.call(model, 'read', model, options);

syncer.clientSync.call(model, 'get', model, options);
request.should.have.been.calledOnce;
request.should.have.been.calledWith(model.app.req, expectedRequestOptions)
});

it('should send the correct payload on PUT or POST requests', function () {
var expectedRequestOptions = {
method: 'PUT',
path: '/listings/0',
query: {},
api: 'foo',
body: { foo: 'bar', bar: 'foo' }
};

model.set('foo', 'bar');
model.set('bar', 'foo');

syncErrorHandler.should.be.not.equal(options.error);
options.error.should.be.a('function');
options.error.should.have.length(1);
syncer.serverSync.call(model, 'update', model, options);

request.should.have.been.calledOnce;
request.should.have.been.calledWith(model.app.req, expectedRequestOptions)
});
});

describe('wrappedErrorHandler', function () {
var fakeXhr;
describe('clientSync', function () {
var backboneSync, syncErrorHandler;

beforeEach(function () {
fakeXhr = {
responseText: '{"foo": "bar"}',
status: 418,
getResponseHeader: sinon.stub()
};
options.error = syncErrorHandler;
backboneSync.yieldsTo('error', fakeXhr);
backboneSync = sinon.stub(Backbone, 'sync');
syncErrorHandler = sinon.spy();
});

it('should call the original error handler with status and body', function () {
var expectedResponse = {
body: fakeXhr.responseText,
status: fakeXhr.status
};
afterEach(function () {
backboneSync.restore();
});

it('should call Backbone.sync', function () {
syncer.clientSync.call(model, 'get', model, options);

syncErrorHandler.should.have.been.calledOnce;
syncErrorHandler.should.have.been.calledWithExactly(expectedResponse);
backboneSync.should.have.been.calledOnce;
backboneSync.should.have.been.calledWithExactly('get', model, options);
});

it('should parse the payload if content-type is "application/json"', function () {
var expectedResponse = {
body: JSON.parse(fakeXhr.responseText),
status: fakeXhr.status
};
it('should get the prefixed API url', function () {
syncer.clientSync.call(model, 'get', model, options);
backboneSync.should.have.been.calledWithExactly('get', model, { url: '/api/-' + model.url() });
});

fakeXhr.getResponseHeader.withArgs('content-type').returns('application/json');
it('should wrap the error handler', function () {
options.error = syncErrorHandler;

syncer.clientSync.call(model, 'get', model, options);

syncErrorHandler.should.have.been.calledOnce;
syncErrorHandler.should.have.been.calledWithExactly(expectedResponse);
syncErrorHandler.should.be.not.equal(options.error);
options.error.should.be.a('function');
options.error.should.have.length(1);
});

describe('wrappedErrorHandler', function () {
var fakeXhr;

beforeEach(function () {
fakeXhr = {
responseText: '{"foo": "bar"}',
status: 418,
getResponseHeader: sinon.stub()
};
options.error = syncErrorHandler;
backboneSync.yieldsTo('error', fakeXhr);
});

it('should call the original error handler with status and body', function () {
var expectedResponse = {
body: fakeXhr.responseText,
status: fakeXhr.status
};

syncer.clientSync.call(model, 'get', model, options);

syncErrorHandler.should.have.been.calledOnce;
syncErrorHandler.should.have.been.calledWithExactly(expectedResponse);
});

it('should parse the payload if content-type is "application/json"', function () {
var expectedResponse = {
body: JSON.parse(fakeXhr.responseText),
status: fakeXhr.status
};

fakeXhr.getResponseHeader.withArgs('content-type').returns('application/json');

syncer.clientSync.call(model, 'get', model, options);

syncErrorHandler.should.have.been.calledOnce;
syncErrorHandler.should.have.been.calledWithExactly(expectedResponse);
});
});
});
});
Expand Down

0 comments on commit e1271d9

Please sign in to comment.