|
|
@ -1,43 +1,27 @@ |
|
|
|
|
|
|
|
const _ = require('lodash'); |
|
|
|
|
|
|
|
|
|
|
|
const sortResponseData = require('../../../middleware/sortResponseData'); |
|
|
|
const sortResponseData = require('../../../middleware/sortResponseData'); |
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests = {}; |
|
|
|
module.exports.tests = {}; |
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.doIt = (test, common) => { |
|
|
|
module.exports.tests.should_execute_failure = (test, common) => { |
|
|
|
test('{} should be passed to comparator when req is unavailable', (t) => { |
|
|
|
test('should_execute returning false should call next w/o invoking comparator', (t) => { |
|
|
|
const comparator = (clean) => { |
|
|
|
t.plan(2, 'this ensures that should_execute was invoked'); |
|
|
|
t.deepEquals(clean, { }); |
|
|
|
|
|
|
|
return () => { |
|
|
|
|
|
|
|
throw Error('should not have been called'); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const res = { |
|
|
|
|
|
|
|
data: [ {} ] |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sort(undefined, res, () => { |
|
|
|
const comparator = () => { |
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('{} should be passed to comparator when req.clean is unavailable', (t) => { |
|
|
|
|
|
|
|
const comparator = (clean) => { |
|
|
|
|
|
|
|
t.deepEquals(clean, { }); |
|
|
|
|
|
|
|
return () => { |
|
|
|
|
|
|
|
throw Error('should not have been called'); |
|
|
|
throw Error('should not have been called'); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const should_execute = (req, res) => { |
|
|
|
|
|
|
|
t.deepEquals(req, { a: 1 }); |
|
|
|
|
|
|
|
t.deepEquals(res, { b: 2 }); |
|
|
|
|
|
|
|
return false; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
const sort = sortResponseData(comparator, should_execute); |
|
|
|
|
|
|
|
|
|
|
|
const req = {}; |
|
|
|
const req = { a: 1 }; |
|
|
|
|
|
|
|
const res = { b: 2 }; |
|
|
|
const res = { |
|
|
|
|
|
|
|
data: [ {} ] |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sort(req, res, () => { |
|
|
|
sort(req, res, () => { |
|
|
|
t.end(); |
|
|
|
t.end(); |
|
|
@ -45,7 +29,12 @@ module.exports.tests.doIt = (test, common) => { |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.general_tests = (test, common) => { |
|
|
|
test('req.clean should be passed to sort', (t) => { |
|
|
|
test('req.clean should be passed to sort', (t) => { |
|
|
|
|
|
|
|
t.plan(1, 'this ensures that comparator was invoked'); |
|
|
|
|
|
|
|
|
|
|
|
const comparator = (clean) => { |
|
|
|
const comparator = (clean) => { |
|
|
|
t.deepEquals(clean, { a: 1 }); |
|
|
|
t.deepEquals(clean, { a: 1 }); |
|
|
|
return () => { |
|
|
|
return () => { |
|
|
@ -53,7 +42,7 @@ module.exports.tests.doIt = (test, common) => { |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
const sort = sortResponseData(comparator, _.constant(true)); |
|
|
|
|
|
|
|
|
|
|
|
const req = { |
|
|
|
const req = { |
|
|
|
clean: { |
|
|
|
clean: { |
|
|
@ -71,62 +60,55 @@ module.exports.tests.doIt = (test, common) => { |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
test('undefined res should return without interacting with comparator', (t) => { |
|
|
|
|
|
|
|
const comparator = () => { |
|
|
|
|
|
|
|
throw Error('should not have been called'); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sort(undefined, undefined, () => { |
|
|
|
|
|
|
|
t.end(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test('undefined res.data should return without interacting with comparator', (t) => { |
|
|
|
test('undefined res.data should return without interacting with comparator', (t) => { |
|
|
|
const comparator = () => { |
|
|
|
const comparator = () => { |
|
|
|
throw Error('should not have been called'); |
|
|
|
throw Error('should not have been called'); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
const sort = sortResponseData(comparator, _.constant(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const req = {}; |
|
|
|
const res = {}; |
|
|
|
const res = {}; |
|
|
|
|
|
|
|
|
|
|
|
sort(undefined, res, () => { |
|
|
|
sort(req, res, () => { |
|
|
|
t.deepEquals(res, {}); |
|
|
|
t.deepEquals(res, {}); |
|
|
|
t.end(); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
test('empty res.data should not cause problems', (t) => { |
|
|
|
test('empty res.data should return without interacting with comparator', (t) => { |
|
|
|
const comparator = () => { |
|
|
|
const comparator = () => { |
|
|
|
throw Error('should not have been called'); |
|
|
|
throw Error('should not have been called'); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
const sort = sortResponseData(comparator, _.constant(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const req = {}; |
|
|
|
const res = { |
|
|
|
const res = { |
|
|
|
data: [] |
|
|
|
data: [] |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
sort(undefined, res, () => { |
|
|
|
sort(req, res, () => { |
|
|
|
t.deepEquals(res.data, [], 'res.data should still be empty'); |
|
|
|
t.deepEquals(res.data, [], 'res.data should still be empty'); |
|
|
|
t.end(); |
|
|
|
t.end(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
test('comparator should be consulted for sorting res.data when defined', (t) => { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.tests.successful_sort = (test, common) => { |
|
|
|
|
|
|
|
test('comparator should be sort res.data', (t) => { |
|
|
|
const comparator = () => { |
|
|
|
const comparator = () => { |
|
|
|
return (a, b) => { |
|
|
|
return (a, b) => { |
|
|
|
return a.key > b.key; |
|
|
|
return a.key > b.key; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const sort = sortResponseData(comparator); |
|
|
|
const sort = sortResponseData(comparator, _.constant(true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const req = {}; |
|
|
|
const res = { |
|
|
|
const res = { |
|
|
|
data: [ |
|
|
|
data: [ |
|
|
|
{ key: 3 }, |
|
|
|
{ key: 3 }, |
|
|
@ -135,7 +117,7 @@ module.exports.tests.doIt = (test, common) => { |
|
|
|
] |
|
|
|
] |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
sort(undefined, res, () => { |
|
|
|
sort(req, res, () => { |
|
|
|
t.deepEquals(res.data.shift(), { key: 1 }); |
|
|
|
t.deepEquals(res.data.shift(), { key: 1 }); |
|
|
|
t.deepEquals(res.data.shift(), { key: 2 }); |
|
|
|
t.deepEquals(res.data.shift(), { key: 2 }); |
|
|
|
t.deepEquals(res.data.shift(), { key: 3 }); |
|
|
|
t.deepEquals(res.data.shift(), { key: 3 }); |
|
|
|