From 75891079b5a7f6ab74a95d388badcbca24e070ec Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 17 Dec 2015 18:34:49 +0000 Subject: [PATCH] Added basic UNTESTED tests --- package.json | 2 +- src/__tests__/__mocks__/superagent.js | 91 +++++++++++++++++++++++++++ src/__tests__/api-requests.js | 14 +++++ src/__tests__/app.js | 26 ++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/__mocks__/superagent.js create mode 100644 src/__tests__/api-requests.js create mode 100644 src/__tests__/app.js diff --git a/package.json b/package.json index 5e59e45..2abbb93 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/__tests__/__mocks__/superagent.js b/src/__tests__/__mocks__/superagent.js new file mode 100644 index 0000000..03ae8e6 --- /dev/null +++ b/src/__tests__/__mocks__/superagent.js @@ -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 +}; diff --git a/src/__tests__/api-requests.js b/src/__tests__/api-requests.js new file mode 100644 index 0000000..ad92adc --- /dev/null +++ b/src/__tests__/api-requests.js @@ -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(); + }); +}); diff --git a/src/__tests__/app.js b/src/__tests__/app.js new file mode 100644 index 0000000..d189422 --- /dev/null +++ b/src/__tests__/app.js @@ -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/'); + }); +});