Skip to content

Commit 4b0c5c9

Browse files
SergiCLroot
and
root
authoredApr 8, 2020
feat(test-utils): add 'overview' function (#1491)
* feat(test-utils): add 'overview' function close #1461 * feat(test-utils): add error wrapper overview * fix(test-utils): fix flow errors * fix(test-utils): compatibility with older versions * fix(test utils): flow error * feat: overview function PR comments Co-authored-by: root <root@LAPTOP-VOQ3N65A.localdomain>
1 parent 9f93d5c commit 4b0c5c9

File tree

8 files changed

+565
-0
lines changed

8 files changed

+565
-0
lines changed
 

‎docs/api/wrapper/overview.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## overview
2+
3+
Prints a simple overview of the `Wrapper`.
4+
5+
- **Example:**
6+
7+
```js
8+
import { mount } from '@vue/test-utils'
9+
import Component from './Component.vue'
10+
11+
const wrapper = mount(Component)
12+
wrapper.overview()
13+
14+
// Console output
15+
/*
16+
Wrapper (Visible):
17+
18+
Html:
19+
<div class="test">
20+
<p>My name is Tess Ting</p>
21+
</div>
22+
23+
Data: {
24+
firstName: Tess,
25+
lastName: Ting
26+
}
27+
28+
Computed: {
29+
fullName: Tess Ting'
30+
}
31+
32+
Emitted: {',
33+
foo: [',
34+
0: [ hello, world ],
35+
1: [ bye, world ]'
36+
],
37+
bar: [
38+
0: [ hey ]'
39+
]
40+
}
41+
42+
*/
43+
```

‎flow/wrapper.flow.js

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ declare interface BaseWrapper {
2626
isVisible(): boolean | void;
2727
isVueInstance(): boolean | void;
2828
name(): string | void;
29+
overview(): void;
2930
props(key?: string): { [name: string]: any } | any | void;
3031
text(): string | void;
3132
selector: Selector | void;

‎packages/test-utils/src/error-wrapper.js

+8
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ export default class ErrorWrapper implements BaseWrapper {
187187
)
188188
}
189189

190+
overview(): void {
191+
throwError(
192+
`find did not return ${buildSelectorString(
193+
this.selector
194+
)}, cannot call overview() on empty Wrapper`
195+
)
196+
}
197+
190198
props(): void {
191199
throwError(
192200
`find did not return ${buildSelectorString(

‎packages/test-utils/src/wrapper-array.js

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ export default class WrapperArray implements BaseWrapper {
143143
)
144144
}
145145

146+
overview(): void {
147+
this.throwErrorIfWrappersIsEmpty('overview()')
148+
149+
throwError(
150+
`overview() must be called on a single wrapper, use at(i) ` +
151+
`to access a wrapper`
152+
)
153+
}
154+
146155
props(): void {
147156
this.throwErrorIfWrappersIsEmpty('props')
148157

‎packages/test-utils/src/wrapper.js

+71
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,77 @@ export default class Wrapper implements BaseWrapper {
324324
return this.vnode.tag
325325
}
326326

327+
/**
328+
* Prints a simple overview of the wrapper current state
329+
* with useful information for debugging
330+
*/
331+
overview(): void {
332+
if (!this.isVueInstance()) {
333+
throwError(`wrapper.overview() can only be called on a Vue instance`)
334+
}
335+
336+
const identation = 4
337+
const formatJSON = (json: any, replacer: Function | null = null) =>
338+
JSON.stringify(json, replacer, identation).replace(/"/g, '')
339+
340+
const visibility = this.isVisible() ? 'Visible' : 'Not visible'
341+
342+
const html = this.html()
343+
? this.html().replace(/^(?!\s*$)/gm, ' '.repeat(identation)) + '\n'
344+
: ''
345+
346+
// $FlowIgnore
347+
const data = formatJSON(this.vm.$data)
348+
349+
/* eslint-disable operator-linebreak */
350+
// $FlowIgnore
351+
const computed = this.vm._computedWatchers
352+
? formatJSON(
353+
// $FlowIgnore
354+
...Object.keys(this.vm._computedWatchers).map(computedKey => ({
355+
// $FlowIgnore
356+
[computedKey]: this.vm[computedKey]
357+
}))
358+
)
359+
: // $FlowIgnore
360+
this.vm.$options.computed
361+
? formatJSON(
362+
// $FlowIgnore
363+
...Object.entries(this.vm.$options.computed).map(([key, value]) => ({
364+
// $FlowIgnore
365+
[key]: value()
366+
}))
367+
)
368+
: '{}'
369+
/* eslint-enable operator-linebreak */
370+
371+
const emittedJSONReplacer = (key, value) =>
372+
value instanceof Array
373+
? value.map((calledWith, index) => {
374+
const callParams = calledWith.map(param =>
375+
typeof param === 'object'
376+
? JSON.stringify(param)
377+
.replace(/"/g, '')
378+
.replace(/,/g, ', ')
379+
: param
380+
)
381+
382+
return `${index}: [ ${callParams.join(', ')} ]`
383+
})
384+
: value
385+
386+
const emitted = formatJSON(this.emitted(), emittedJSONReplacer)
387+
388+
console.log(
389+
'\n' +
390+
`Wrapper (${visibility}):\n\n` +
391+
`Html:\n${html}\n` +
392+
`Data: ${data}\n\n` +
393+
`Computed: ${computed}\n\n` +
394+
`Emitted: ${emitted}\n`
395+
)
396+
}
397+
327398
/**
328399
* Returns an Object containing the prop name/value pairs on the element
329400
*/

‎test/specs/error-wrapper.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describeWithShallowAndMount('ErrorWrapper', mountingMethod => {
2222
'isVisible',
2323
'isVueInstance',
2424
'name',
25+
'overview',
2526
'props',
2627
'setComputed',
2728
'setMethods',
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describeWithShallowAndMount } from '~resources/utils'
2+
import { compileToFunctions } from 'vue-template-compiler'
3+
import '@vue/test-utils'
4+
5+
describeWithShallowAndMount('overview', mountingMethod => {
6+
it('throws error if wrapper array contains no items', () => {
7+
const wrapper = mountingMethod(compileToFunctions('<div />'))
8+
const message = '[vue-test-utils]: overview() cannot be called on 0 items'
9+
10+
expect(() => wrapper.findAll('p').overview())
11+
.to.throw()
12+
.with.property('message', message)
13+
})
14+
15+
it('throws error when called on a WrapperArray', () => {
16+
const wrapper = mountingMethod(compileToFunctions('<div><div /></div>'))
17+
const message =
18+
'[vue-test-utils]: overview() must be called on a single wrapper, use at(i) to access a wrapper'
19+
20+
expect(() => wrapper.findAll('div').overview())
21+
.to.throw()
22+
.with.property('message', message)
23+
})
24+
})

‎test/specs/wrapper/overview.spec.js

+408
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.