Browse Source

removed require's for mock backend/query

pull/782/head
Stephen Hess 8 years ago
parent
commit
d5ff417bf5
  1. 60
      test/unit/controller/search.js

60
test/unit/controller/search.js

@ -1,9 +1,7 @@
'use strict'; 'use strict';
var setup = require('../../../controller/search'), const setup = require('../../../controller/search');
mockBackend = require('../mock/backend'), const proxyquire = require('proxyquire').noCallThru();
mockQuery = require('../mock/query');
var proxyquire = require('proxyquire').noCallThru();
module.exports.tests = {}; module.exports.tests = {};
@ -62,7 +60,7 @@ module.exports.tests.success = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
errors: [], errors: [],
@ -126,7 +124,7 @@ module.exports.tests.success = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
errors: [], errors: [],
@ -190,7 +188,7 @@ module.exports.tests.success = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
errors: [], errors: [],
@ -270,7 +268,7 @@ module.exports.tests.success = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
errors: [], errors: [],
@ -348,7 +346,7 @@ module.exports.tests.timeout = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.equal(searchServiceCallCount, 3+1); t.equal(searchServiceCallCount, 3+1);
t.ok(infoMesssages.indexOf('request timed out on attempt 1, retrying') !== -1); t.ok(infoMesssages.indexOf('request timed out on attempt 1, retrying') !== -1);
@ -399,7 +397,7 @@ module.exports.tests.timeout = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.equal(searchServiceCallCount, 17+1); t.equal(searchServiceCallCount, 17+1);
t.end(); t.end();
}; };
@ -439,7 +437,7 @@ module.exports.tests.timeout = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.equal(searchServiceCallCount, 1); t.equal(searchServiceCallCount, 1);
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
@ -480,7 +478,7 @@ module.exports.tests.timeout = function(test, common) {
const req = { clean: { }, errors: [], warnings: [] }; const req = { clean: { }, errors: [], warnings: [] };
const res = {}; const res = {};
var next = function() { const next = () => {
t.equal(searchServiceCallCount, 1); t.equal(searchServiceCallCount, 1);
t.deepEqual(req, { t.deepEqual(req, {
clean: {}, clean: {},
@ -498,17 +496,20 @@ module.exports.tests.timeout = function(test, common) {
module.exports.tests.existing_errors = function(test, common) { module.exports.tests.existing_errors = function(test, common) {
test('req with errors should not call backend', function(t) { test('req with errors should not call backend', function(t) {
var esclient = function() { const esclient = () => {
throw new Error('esclient should not have been called'); throw new Error('esclient should not have been called');
}; };
var controller = setup( {}, esclient, mockQuery() ); const query = () => {
throw new Error('query should not have been called');
};
const controller = setup( {}, esclient, query );
// the existence of `errors` means that a sanitizer detected an error, // the existence of `errors` means that a sanitizer detected an error,
// so don't call the esclient // so don't call the esclient
var req = { const req = {
errors: ['error'] errors: ['error']
}; };
var res = { }; const res = { };
t.doesNotThrow(() => { t.doesNotThrow(() => {
controller(req, res, () => {}); controller(req, res, () => {});
@ -521,17 +522,20 @@ module.exports.tests.existing_errors = function(test, common) {
module.exports.tests.existing_results = function(test, common) { module.exports.tests.existing_results = function(test, common) {
test('res with existing data should not call backend', function(t) { test('res with existing data should not call backend', function(t) {
var esclient = function() { const esclient = () => {
throw new Error('backend should not have been called'); throw new Error('esclient should not have been called');
};
const query = () => {
throw new Error('query should not have been called');
}; };
var controller = setup( {}, esclient, mockQuery() ); const controller = setup( {}, esclient, query );
var req = { }; const req = { };
// the existence of `data` means that there are already results so // the existence of `data` means that there are already results so
// don't call the backend/query // don't call the backend/query
var res = { data: [{}] }; const res = { data: [{}] };
var next = function() { const next = function() {
t.deepEqual(res, {data: [{}]}); t.deepEqual(res, {data: [{}]});
t.end(); t.end();
}; };
@ -544,18 +548,20 @@ module.exports.tests.existing_results = function(test, common) {
module.exports.tests.undefined_query = function(test, common) { module.exports.tests.undefined_query = function(test, common) {
test('query returning undefined should not call service', function(t) { test('query returning undefined should not call service', function(t) {
// a function that returns undefined // a function that returns undefined
var query = function () { return; }; const query = () => {
return undefined;
};
var search_service_was_called = false; let search_service_was_called = false;
var controller = proxyquire('../../../controller/search', { const controller = proxyquire('../../../controller/search', {
'../service/search': function() { '../service/search': function() {
search_service_was_called = true; search_service_was_called = true;
throw new Error('search service should not have been called'); throw new Error('search service should not have been called');
} }
})(undefined, undefined, query); })(undefined, undefined, query);
var next = function() { const next = () => {
t.notOk(search_service_was_called, 'should have returned before search service was called'); t.notOk(search_service_was_called, 'should have returned before search service was called');
t.end(); t.end();
}; };
@ -571,7 +577,7 @@ module.exports.all = function (tape, common) {
return tape('GET /search ' + name, testFunction); return tape('GET /search ' + name, testFunction);
} }
for( var testCase in module.exports.tests ){ for( const testCase in module.exports.tests ){
module.exports.tests[testCase](test, common); module.exports.tests[testCase](test, common);
} }
}; };

Loading…
Cancel
Save