Skip to content

Commit

Permalink
feat(test-utils): add 'overview' function
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Mar 28, 2020
1 parent 228cd1a commit 61ee2c9
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/api/wrapper/overview.md
@@ -0,0 +1,43 @@
## overview

Prints a simple overview of the `Wrapper`.

- **Example:**

```js
import { mount } from '@vue/test-utils'
import Component from './Component.vue'

const wrapper = mount(Component)
wrapper.overview()

// Console output
/*
Wrapper (Visible):
Html:
<div class="test">
<p>My name is Tess Ting</p>
</div>
Data: {
firstName: Tess,
lastName: Ting
}
Computed: {
fullName: Tess Ting'
}
Emitted: {',
foo: [',
0: [ hello, world ],
1: [ bye, world ]'
],
bar: [
0: [ hey ]'
]
}
*/
```
1 change: 1 addition & 0 deletions flow/wrapper.flow.js
Expand Up @@ -26,6 +26,7 @@ declare interface BaseWrapper {
isVisible(): boolean | void;
isVueInstance(): boolean | void;
name(): string | void;
overview(): void;
props(key?: string): { [name: string]: any } | any | void;
text(): string | void;
selector: Selector | void;
Expand Down
9 changes: 9 additions & 0 deletions packages/test-utils/src/wrapper-array.js
Expand Up @@ -143,6 +143,15 @@ export default class WrapperArray implements BaseWrapper {
)
}

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

throwError(
`overview() must be called on a single wrapper, use at(i) ` +
`to access a wrapper`
)
}

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

Expand Down
47 changes: 47 additions & 0 deletions packages/test-utils/src/wrapper.js
Expand Up @@ -324,6 +324,53 @@ export default class Wrapper implements BaseWrapper {
return this.vnode.tag
}

/**
* Prints a simple overview of the wrapper current state
* with useful information for debugging
*/
overview(): void {
if (!this.isVueInstance()) {
throwError(`wrapper.overview() can only be called on a Vue instance`)
}
const identation = 4
const formatJSON = (json: string, replacer: Function = null) =>
JSON.stringify(json, replacer, identation).replace(/"/g, '')

const visibility = this.isVisible() ? 'Visible' : 'Not visible'

const html = this.html()
? this.html().replace(/^(?!\s*$)/gm, ' '.repeat(identation)) + '\n'
: ''

const data = formatJSON(this.vm.$data)

const computed = this.vm._computedWatchers
? formatJSON(
...Object.keys(this.vm._computedWatchers).map(computedKey => ({
[computedKey]: this.vm[computedKey]
}))
)
: '{}'

const emittedJSONReplacer = (key, value) =>
value instanceof Array
? value.map(
(calledWith, index) => `${index}: [ ${calledWith.join(', ')} ]`
)
: value

const emitted = formatJSON(this.emitted(), emittedJSONReplacer)

console.log(
'\n' +
`Wrapper (${visibility}):\n\n` +
`Html:\n${html}\n` +
`Data: ${data}\n\n` +
`Computed: ${computed}\n\n` +
`Emitted: ${emitted}\n`
)
}

/**
* Returns an Object containing the prop name/value pairs on the element
*/
Expand Down
24 changes: 24 additions & 0 deletions test/specs/wrapper-array/overview.spec.js
@@ -0,0 +1,24 @@
import { describeWithShallowAndMount } from '~resources/utils'
import { compileToFunctions } from 'vue-template-compiler'
import '@vue/test-utils'

describeWithShallowAndMount('overview', mountingMethod => {
it('throws error if wrapper array contains no items', () => {
const wrapper = mountingMethod(compileToFunctions('<div />'))
const message = '[vue-test-utils]: overview() cannot be called on 0 items'

expect(() => wrapper.findAll('p').overview())
.to.throw()
.with.property('message', message)
})

it('throws error when called on a WrapperArray', () => {
const wrapper = mountingMethod(compileToFunctions('<div><div /></div>'))
const message =
'[vue-test-utils]: overview() must be called on a single wrapper, use at(i) to access a wrapper'

expect(() => wrapper.findAll('div').overview())
.to.throw()
.with.property('message', message)
})
})

0 comments on commit 61ee2c9

Please sign in to comment.