1
Fork 0

Added basic UNTESTED tests

This commit is contained in:
Jake Howard 2015-12-17 18:34:49 +00:00
parent 9625876e2a
commit 75891079b5
4 changed files with 132 additions and 1 deletions

View file

@ -4,7 +4,7 @@
"description": "Client side helper for Django-Client-Reverse",
"main": "src/app.js",
"scripts": {
"test": "npm run lint",
"test": "jest && npm run lint",
"lint": "eslint -c node_modules/eslint-config/.eslintrc 'src'",
"create-build-dirs": "mkdir -p build",
"build-js": "browserify -t [ babelify --presets [ es2015 ] ] src/app.js -o build/app.js"

View file

@ -0,0 +1,91 @@
/* global jest */
var superagent = jest.genMockFromModule('superagent');
var MockResponse = jest.genMockFunction().mockImplementation(function () {
this.status = 200;
this.ok = true;
});
MockResponse.prototype.send = jest.genMockFunction();
MockResponse.prototype.toError = jest.genMockFunction();
var MockRequest = jest.genMockFunction().mockImplementation(function (method, url) {
this.method = method;
this.url = url;
});
MockRequest.prototype.accept = jest.genMockFunction().mockReturnThis();
MockRequest.prototype.set = jest.genMockFunction().mockReturnThis();
MockRequest.prototype.send = jest.genMockFunction().mockReturnThis();
MockRequest.prototype.field = jest.genMockFunction().mockReturnThis();
MockRequest.prototype.query = jest.genMockFunction().mockReturnThis();
MockRequest.prototype.end = jest.genMockFunction().mockImplementation(function (callback) {
if (superagent.mockDelay) {
this.delayTimer = setTimeout(callback, 0, superagent.mockError, superagent.mockResponse);
return;
}
callback(superagent.mockError, superagent.mockResponse);
});
MockRequest.prototype.abort = jest.genMockFunction().mockImplementation(function () {
this.aborted = true;
if (this.delayTimer) {
clearTimeout(this.delayTimer);
}
});
superagent.Request = MockRequest;
superagent.Response = MockResponse;
superagent.mockResponse = new MockResponse();
superagent.mockError = null;
superagent.mockDelay = false;
function __setResponse (options) {
var status = typeof options.status !== 'undefined' ? options.status : 200;
var ok = typeof options.ok !== 'undefined' ? options.ok : true;
var body = typeof options.body !== 'undefined' ? options.body : {};
var error = typeof options.error !== 'undefined' ? options.error : {};
var mockedResponse = jest.genMockFunction().mockImplementation(function () {
this.status = status;
this.ok = ok;
this.body = body;
});
superagent.mockError = {
response: {
body: error
}
};
superagent.mockResponse = new mockedResponse();
}
//module.exports = superagent;
module.exports = {
Response: MockResponse,
Request: MockRequest,
_requests: [],
_newRequest: function () {
var r = new MockRequest();
this._requests.push(r);
return r;
},
post: function () {
return this._newRequest();
},
get: function () {
return this._newRequest();
},
put: function () {
return this._newRequest();
},
__setResponse: __setResponse
};

View file

@ -0,0 +1,14 @@
/*global describe, it, expect, beforeEach */
describe('Reverser', function () {
var apiRequests;
beforeEach(function () {
apiRequests = require('./../api-requests');
});
it('should exist', function () {
expect(apiRequests).toBeTruthy();
});
});

26
src/__tests__/app.js Normal file
View file

@ -0,0 +1,26 @@
/*global jest, describe, it, expect, beforeEach, spyOn */
jest.dontMock('./../app');
jest.dontMock('./../api-requests');
describe('Reverser', function () {
var superagent, Reverse, apiRequests;
beforeEach(function () {
Reverse = require('./../app');
apiRequests = require('./../api-requests');
superagent = require('superagent');
});
it('should get results', function () {
var data = 'thing:thing';
superagent.__setResponse({status: 200, ok: 'ok', body: data});
spyOn(apiRequests, 'post').andCallThrough();
Reverse(data);
jest.runAllTimers();
expect(apiRequests.post).toHaveBeenCalledWith('/reverse/');
});
});