Skip to content

Add debug function #1469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/test-utils/src/wrapper-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ export default class WrapperArray implements BaseWrapper {
)
}

/**
* Prety print element HTML content
*/
debug(): void {
this.throwErrorIfWrappersIsEmpty('debug')

console.log(`Wrapper-Array (Length: ${this.wrappers.length}):\n`)
this.wrappers.forEach((wrapper, idx) => {
console.log(`(At ${idx})`)
wrapper.debug()
})
}

html(): void {
this.throwErrorIfWrappersIsEmpty('html')

Expand Down
12 changes: 12 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ export default class Wrapper implements BaseWrapper {
return wrapperArray
}

/**
* Pretty print HTML of element with useful
* information for debugging
*/
debug(): void {
const pretyHtml = pretty(this.element.outerHTML)
const title = 'Wrapper'

console.log(`${title}:`)
console.log(`${pretyHtml}\n`)
}

/**
* Returns HTML of element as a string
*/
Expand Down
24 changes: 24 additions & 0 deletions test/specs/vue-wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,28 @@ describeWithShallowAndMount('VueWrapper', mountingMethod => {
.with.property('message', message)
})
})

describe('debug function', () => {
const sandbox = sinon.createSandbox()

beforeEach(() => {
sandbox.spy(console, 'log')
})

it('writes to the console formated html content of the vue-wrapper', () => {
const basicComponent = { template: '<div><p>Debug me please</p></div>' }
const wrapper = mountingMethod(basicComponent)

wrapper.debug()

expect(console.log).to.have.been.calledWith('Wrapper:')
expect(console.log).to.have.been.calledWith(
'<div>\n' + ' <p>Debug me please</p>\n' + '</div>\n'
)
})

afterEach(() => {
sandbox.restore()
})
})
})
53 changes: 53 additions & 0 deletions test/specs/wrapper-array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,57 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => {
wrapperArray.destroy()
expect(destroy.calledTwice).to.equal(true)
})

describe('debug function', () => {
const sandbox = sinon.createSandbox()

beforeEach(() => {
sandbox.spy(console, 'log')
})

it('calls debug on each wrapper with aditional information', () => {
function getNestedElement(text, level = 1) {
const nestElement = element => {
const div = document.createElement('div')
div.appendChild(element)
return div
}
let element = document.createElement('p')
element.textContent = text

for (let i = 0; i < level; i++) {
element = nestElement(element)
}
return element
}
const wrapperArray = getWrapperArray([
new Wrapper(getNestedElement('One level', 1)),
new Wrapper(getNestedElement('Two levels', 2))
])

wrapperArray.debug()

expect(console.log).to.have.been.calledWith(
'Wrapper-Array (Length: 2):\n'
)
expect(console.log).to.have.been.calledWith('(At 0)')
expect(console.log).to.have.been.calledWith('Wrapper:')
expect(console.log).to.have.been.calledWith(
'<div>\n' + ' <p>One level</p>\n' + '</div>\n'
)
expect(console.log).to.have.been.calledWith('(At 1)')
expect(console.log).to.have.been.calledWith('Wrapper:')
expect(console.log).to.have.been.calledWith(
'<div>\n' +
' <div>\n' +
' <p>Two levels</p>\n' +
' </div>\n' +
'</div>\n'
)
})

afterEach(() => {
sandbox.restore()
})
})
})
24 changes: 24 additions & 0 deletions test/specs/wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,28 @@ describeWithShallowAndMount('Wrapper', mountingMethod => {
expect(() => enableAutoDestroy(noop)).to.throw()
})
})

describe('debug function', () => {
const sandbox = sinon.createSandbox()

beforeEach(() => {
sandbox.spy(console, 'log')
})

it('writes to the console formated html content of the wrapper', () => {
const basicComponent = { template: '<div><p>Debug me please</p></div>' }
const wrapper = mountingMethod(basicComponent)

wrapper.debug()

expect(console.log).to.have.been.calledWith('Wrapper:')
expect(console.log).to.have.been.calledWith(
'<div>\n' + ' <p>Debug me please</p>\n' + '</div>\n'
)
})

afterEach(() => {
sandbox.restore()
})
})
})