Skip to content

Commit

Permalink
Implement maxDepth() on SingleValueFormatter
Browse files Browse the repository at this point in the history
Fix for avajs/ava#1433. The react plugin
increases value indentation for properties. Combined with `maxDepth`
this can lead to property values exceeding the max depth. The
`maxDepth()` function is required on `SingleValueFormatter` so the
property name is still included in the formatted output.
  • Loading branch information
novemberborn committed Jul 13, 2017
1 parent f7f64cb commit f127100
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ function diffDescriptors (lhs, rhs, options) {

let formatted = record.formatter.finalize()
if (record.exceedsMaxDepth && !formatted.hasGutter) {
// The record exceeds the max depth, but contains no actual dif.
// The record exceeds the max depth, but contains no actual diff.
// Discard the potentially deep formatting and format just the
// original subject.
const subject = lhsRecord.subject
Expand Down
7 changes: 6 additions & 1 deletion lib/formatUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class ObjectFormatter {
exports.ObjectFormatter = ObjectFormatter

class SingleValueFormatter {
constructor (finalizeFn, increaseIndent) {
constructor (theme, finalizeFn, increaseIndent) {
this.theme = theme
this.finalizeFn = finalizeFn
this.hasValue = false
this.increaseIndent = increaseIndent === true
Expand All @@ -114,5 +115,9 @@ class SingleValueFormatter {

return this.finalizeFn(this.value)
}

maxDepth () {
return this.finalizeFn(lineBuilder.single(this.theme.maxDepth))
}
}
exports.SingleValueFormatter = SingleValueFormatter
2 changes: 1 addition & 1 deletion lib/metaDescriptors/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ComplexItem {

formatShallow (theme, indent) {
const increaseValueIndent = theme.item.increaseValueIndent === true
return new formatUtils.SingleValueFormatter(value => {
return new formatUtils.SingleValueFormatter(theme, value => {
if (typeof theme.item.customFormat === 'function') {
return theme.item.customFormat(theme, indent, value)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/metaDescriptors/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ComplexProperty extends Property {

formatShallow (theme, indent) {
const increaseValueIndent = theme.property.increaseValueIndent === true
return new formatUtils.SingleValueFormatter(value => {
return new formatUtils.SingleValueFormatter(theme, value => {
if (typeof theme.property.customFormat === 'function') {
return theme.property.customFormat(theme, indent, this.key, value)
}
Expand Down
15 changes: 15 additions & 0 deletions test/max-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ test('diff() respects maxDepth option for equal parts', t => {
t.snapshot(diff(Object.assign({unequal: 1}, deep), Object.assign({unequal: 2}, deep), {maxDepth: 3}))
t.snapshot(diff({one: {two: {three: 'baz', three2: ['quux']}}}, {one: {two: {three: 'qux', three2: ['quux']}}}, {maxDepth: 1}))
})

test('properties with increased indentation respect the maxDepth when formatted', t => {
t.snapshot(format({
foo: {
bar: {}
}
}, {
maxDepth: 1,
theme: {
property: {
increaseValueIndent: true
}
}
}))
})
8 changes: 8 additions & 0 deletions test/snapshots/max-depth.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ Generated by [AVA](https://ava.li).
},␊
},␊
}`

## properties with increased indentation respect the maxDepth when formatted

> Snapshot 1
`{␊
foo: …,␊
}`
Binary file modified test/snapshots/max-depth.js.snap
Binary file not shown.

0 comments on commit f127100

Please sign in to comment.