Skip to content

Commit

Permalink
Added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PVince81 committed Mar 2, 2017
1 parent fc7519d commit 4c73f79
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function(config) {

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
frameworks: ['jasmine', 'jasmine-sinon'],


// list of files / patterns to load in the browser
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"url": "https://github.com/evert/davclient.js/issues"
},
"devDependencies": {
"jasmine-core": "^2.3.4",
"karma": "^0.13.9",
"jasmine-core": "^2.5.2",
"jasmine-sinon": "~0.4.0",
"karma": "^1.5.0",
"karma-chrome-launcher": "^0.2.0",
"karma-firefox-launcher": "^0.1.6",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^0.2.1",
"phantomjs": "^1.9.18"
"karma-jasmine": "^1.1.0",
"karma-jasmine-sinon": "^1.0.4",
"karma-phantomjs-launcher": "^0.2.1"
}
}
93 changes: 91 additions & 2 deletions tests/client.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,99 @@

/* global dav */
describe("client", function() {

it("should be defined", function() {
var client;
var fakeServer;
var oldPromise;

beforeEach(function() {
client = new dav.Client({
baseUrl: 'http://example.org/',
userName: 'user',
password: 'password'
});

client.xmlNamespaces['http://example.org/ns'] = 'e';

expect(dav.Client).toBeDefined();
oldPromise = window.Promise;
window.Promise = sinon.stub();

fakeServer = sinon.fakeServer.create();
});

afterEach(function() {
client = null;
fakeServer.restore();
fakeServer = null;
window.Promise = oldPromise;
});

describe('request', function() {
it('sends the given request', function() {

client.request(
'PUT',
'/rel/path',
{
'CustomHeader': 'Value',
'CustomHeader2': 'Value2'
},
'body'
);

expect(fakeServer.requests.length).toEqual(1);
var request = fakeServer.requests[0];

expect(request.method).toEqual('PUT');
expect(request.url).toEqual('http://example.org/rel/path');

expect(request.requestHeaders).toEqual({
CustomHeader: 'Value',
CustomHeader2: 'Value2',
'Content-Type': 'text/plain;charset=utf-8',
Authorization: 'Basic dXNlcjpwYXNzd29yZA==',
});

expect(request.requestBody).toEqual('body');
});

it('processes response', function() {
client.request(
'GET',
'/rel/path',
{
'CustomHeader': 'Value',
'CustomHeader2': 'Value2'
},
'body'
);

expect(fakeServer.requests.length).toEqual(1);
var request = fakeServer.requests[0];

expect(window.Promise.calledOnce).toEqual(true);

var promise = window.Promise.getCall(0).args[0];
var fulfillStub = sinon.stub();
var rejectStub = sinon.stub();
promise(fulfillStub, rejectStub);

expect(fulfillStub.notCalled).toEqual(true);
expect(rejectStub.notCalled).toEqual(true);

request.respond(
200,
{ 'Content-Type': 'text/plain' },
'responsebody'
);

expect(fulfillStub.calledOnce).toEqual(true);
expect(fulfillStub.getCall(0).args[0].body).toEqual('responsebody');
expect(fulfillStub.getCall(0).args[0].status).toEqual(200);
expect(fulfillStub.getCall(0).args[0].xhr).toBeDefined();

expect(rejectStub.notCalled).toEqual(true);
});
});

});

0 comments on commit 4c73f79

Please sign in to comment.