Skip to content

Commit

Permalink
fix(data-point): fix request.params.inspect (#283)
Browse files Browse the repository at this point in the history
inspect was adding extra logging, this offloads the entire behaviour to the custom function

closes #263
  • Loading branch information
acatl authored May 11, 2018
1 parent 0d953ef commit 633b514
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
12 changes: 11 additions & 1 deletion packages/data-point/lib/entity-types/entity-request/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ module.exports.resolveUrl = resolveUrl
* @param {Accumulator} acc
*/
function inspect (acc) {
if (acc.params && acc.params.inspect) {
const paramInspect = acc.params && acc.params.inspect

if (typeof paramInspect === 'function') {
paramInspect(acc)
return true
}

if (paramInspect === true) {
utils.inspect(acc, {
options: acc.options,
value: acc.value
})
return true
}

return false
}

module.exports.inspect = inspect
Expand Down
40 changes: 29 additions & 11 deletions packages/data-point/lib/entity-types/entity-request/resolve.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ResolveEntity = require('../base-entity/resolve')
const FixtureStore = require('../../../test/utils/fixture-store')

const helpers = require('../../helpers')
const utils = require('../../utils')

let dataPoint
let resolveReducerBound
Expand Down Expand Up @@ -274,24 +275,41 @@ describe('inspect', () => {
afterEach(() => {
console.info = consoleInfo
})
test('It should not execute utils.inspect', () => {
console.info = jest.fn()
test('It should not execute custom inspect or utils.inspect', () => {
const utilsIinspectSpy = jest.spyOn(utils, 'inspect')
const acc = getAcc()
acc.params.inspect = undefined

Resolve.inspect(acc)
expect(console.info).not.toBeCalled()
expect(utilsIinspectSpy).not.toBeCalled()

utilsIinspectSpy.mockReset()
utilsIinspectSpy.mockRestore()
})
test('It should not execute utils.inspect', () => {
console.info = jest.fn()
Resolve.inspect(getAcc())
expect(console.info.mock.calls[0]).toContain('test:test')
test('It should execute custom inspect when provided, not execute utils.inspect', () => {
const utilsIinspectSpy = jest.spyOn(utils, 'inspect')
const acc = getAcc()
acc.params.inspect = jest.fn()

Resolve.inspect(acc)
expect(utilsIinspectSpy).not.toBeCalled()
expect(acc.params.inspect).toBeCalledWith(acc)

utilsIinspectSpy.mockReset()
utilsIinspectSpy.mockRestore()
})
test('It should output options', () => {
console.info = jest.fn()
test('It should execute utils.inspect when params.inspect === true', () => {
const utilsIinspectSpy = jest
.spyOn(utils, 'inspect')
.mockImplementation(() => true)
const acc = getAcc()
_.set(acc, 'options', { options: 1 })
acc.params.inspect = true

Resolve.inspect(acc)
expect(console.info.mock.calls[0]).toContain('\noptions:')
expect(utilsIinspectSpy).toBeCalled()

utilsIinspectSpy.mockReset()
utilsIinspectSpy.mockRestore()
})
})

Expand Down
5 changes: 0 additions & 5 deletions packages/data-point/lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ function inspect (acc, data) {
}

console.info.apply(null, log)

if (typeof acc.params.inspect === 'function') {
console.info('\ncustom:')
_.attempt(acc.params.inspect, acc)
}
}

module.exports.inspect = inspect
Expand Down
8 changes: 0 additions & 8 deletions packages/data-point/lib/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ describe('inspect', () => {
expect(console.info.mock.calls[0]).toContain('\nfoo:')
expect(console.info.mock.calls[0]).toContain('"bar"')
})
test('It should execute custom inspect if function', () => {
console.info = jest.fn()
const acc = getAcc()
acc.params.inspect = () => console.info('baz')
utils.inspect(acc)
expect(console.info.mock.calls[1]).toContain('\ncustom:')
expect(console.info.mock.calls[2]).toContain('baz')
})
})

describe('inspectProperties', () => {
Expand Down

0 comments on commit 633b514

Please sign in to comment.