Skip to content

Commit 3c89221

Browse files
rootroot
root
authored and
root
committed
feat(test-utils): add 'debug' function
close vuejs#1461
1 parent 228cd1a commit 3c89221

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

Diff for: packages/test-utils/src/wrapper-array.js

+13
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ export default class WrapperArray implements BaseWrapper {
101101
)
102102
}
103103

104+
/**
105+
* Prety print element HTML content
106+
*/
107+
debug(): void {
108+
this.throwErrorIfWrappersIsEmpty('debug')
109+
110+
console.log(`Wrapper-Array (Length: ${this.wrappers.length}):\n`)
111+
this.wrappers.forEach((wrapper, idx) => {
112+
console.log(`(At ${idx})`)
113+
wrapper.debug()
114+
})
115+
}
116+
104117
html(): void {
105118
this.throwErrorIfWrappersIsEmpty('html')
106119

Diff for: packages/test-utils/src/wrapper.js

+12
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ export default class Wrapper implements BaseWrapper {
234234
return wrapperArray
235235
}
236236

237+
/**
238+
* Pretty print HTML of element with useful
239+
* information for debugging
240+
*/
241+
debug(): void {
242+
const pretyHtml = pretty(this.element.outerHTML)
243+
const title = 'Wrapper'
244+
245+
console.log(`${title}:`)
246+
console.log(`${pretyHtml}\n`)
247+
}
248+
237249
/**
238250
* Returns HTML of element as a string
239251
*/

Diff for: test/specs/vue-wrapper.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,28 @@ describeWithShallowAndMount('VueWrapper', mountingMethod => {
1313
.with.property('message', message)
1414
})
1515
})
16+
17+
describe('debug function', () => {
18+
const sandbox = sinon.createSandbox()
19+
20+
beforeEach(() => {
21+
sandbox.spy(console, 'log')
22+
})
23+
24+
it('writes to the console formated html content of the vue-wrapper', () => {
25+
const basicComponent = { template: '<div><p>Debug me please</p></div>' }
26+
const wrapper = mountingMethod(basicComponent)
27+
28+
wrapper.debug()
29+
30+
expect(console.log).to.have.been.calledWith('Wrapper:')
31+
expect(console.log).to.have.been.calledWith(
32+
'<div>\n' + ' <p>Debug me please</p>\n' + '</div>\n'
33+
)
34+
})
35+
36+
afterEach(() => {
37+
sandbox.restore()
38+
})
39+
})
1640
})

Diff for: test/specs/wrapper-array.spec.js

+53
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,57 @@ describeWithShallowAndMount('WrapperArray', mountingMethod => {
268268
wrapperArray.destroy()
269269
expect(destroy.calledTwice).to.equal(true)
270270
})
271+
272+
describe('debug function', () => {
273+
const sandbox = sinon.createSandbox()
274+
275+
beforeEach(() => {
276+
sandbox.spy(console, 'log')
277+
})
278+
279+
it('calls debug on each wrapper with aditional information', () => {
280+
function getNestedElement(text, level = 1) {
281+
const nestElement = element => {
282+
const div = document.createElement('div')
283+
div.appendChild(element)
284+
return div
285+
}
286+
let element = document.createElement('p')
287+
element.textContent = text
288+
289+
for (let i = 0; i < level; i++) {
290+
element = nestElement(element)
291+
}
292+
return element
293+
}
294+
const wrapperArray = getWrapperArray([
295+
new Wrapper(getNestedElement('One level', 1)),
296+
new Wrapper(getNestedElement('Two levels', 2))
297+
])
298+
299+
wrapperArray.debug()
300+
301+
expect(console.log).to.have.been.calledWith(
302+
'Wrapper-Array (Length: 2):\n'
303+
)
304+
expect(console.log).to.have.been.calledWith('(At 0)')
305+
expect(console.log).to.have.been.calledWith('Wrapper:')
306+
expect(console.log).to.have.been.calledWith(
307+
'<div>\n' + ' <p>One level</p>\n' + '</div>\n'
308+
)
309+
expect(console.log).to.have.been.calledWith('(At 1)')
310+
expect(console.log).to.have.been.calledWith('Wrapper:')
311+
expect(console.log).to.have.been.calledWith(
312+
'<div>\n' +
313+
' <div>\n' +
314+
' <p>Two levels</p>\n' +
315+
' </div>\n' +
316+
'</div>\n'
317+
)
318+
})
319+
320+
afterEach(() => {
321+
sandbox.restore()
322+
})
323+
})
271324
})

Diff for: test/specs/wrapper.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ describeWithShallowAndMount('Wrapper', mountingMethod => {
5353
expect(() => enableAutoDestroy(noop)).to.throw()
5454
})
5555
})
56+
57+
describe('debug function', () => {
58+
const sandbox = sinon.createSandbox()
59+
60+
beforeEach(() => {
61+
sandbox.spy(console, 'log')
62+
})
63+
64+
it('writes to the console formated html content of the wrapper', () => {
65+
const basicComponent = { template: '<div><p>Debug me please</p></div>' }
66+
const wrapper = mountingMethod(basicComponent)
67+
68+
wrapper.debug()
69+
70+
expect(console.log).to.have.been.calledWith('Wrapper:')
71+
expect(console.log).to.have.been.calledWith(
72+
'<div>\n' + ' <p>Debug me please</p>\n' + '</div>\n'
73+
)
74+
})
75+
76+
afterEach(() => {
77+
sandbox.restore()
78+
})
79+
})
5680
})

0 commit comments

Comments
 (0)