Skip to content

Commit

Permalink
feat: add toHaveProp matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
14nrv committed Oct 23, 2018
1 parent 005d07f commit d35646e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ describe('MyComponent', () => {
expect('input[type=text]').toHaveValue('plop')
expect('input[type=text]').not.toHaveValue('not plop')
```
* toHaveProp(propName)
```js
expect(wrapper).toHaveProp('propName')
expect(wrapper).not.toHaveProp('not-propName')
```
* toEmit(eventName)
```js
expect(wrapper).toEmit('eventName')
Expand Down
20 changes: 20 additions & 0 deletions src/components/Questions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ describe('Questions', () => {
})
})

describe('toHaveProp', () => {
const prop = 'name'

it('can success', () => {
expect(wrapper).toHaveProp(prop)

const { pass } = matchers(wrapper).toHaveProp(wrapper, prop)
expect(pass).toBeTruthy()
})

it('can fail', () => {
const { pass } = matchers(wrapper).toHaveProp(wrapper, `not-${prop}`)
expect(pass).toBeFalsy()
})

it('can reverse', () => {
expect(wrapper).not.toHaveProp(`not-${prop}`)
})
})

describe('toEmit', () => {
const eventName = 'isEditing'
const selector = '.edit'
Expand Down
8 changes: 8 additions & 0 deletions src/components/Questions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export default {
data: () => ({
editing: false
}),
props: {
name: {
type: String
},
plop: {
type: String
}
},
methods: {
update () {
this.$emit('applied')
Expand Down
4 changes: 4 additions & 0 deletions src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const toHaveAttribute = (selector, attr, value) =>
const toHaveValue = (selector, value) =>
matcher.toBe(w.find(selector).element.value, value)

const toHaveProp = (selector, propName) =>
matcher.toHaveProperty(selector.props(), propName)

const toEmit = (selector = w, event) =>
matcher.toBeTruthy(selector.emitted()[event])

Expand Down Expand Up @@ -66,6 +69,7 @@ const matchers = wrapper => {
toHaveAClass,
toHaveAttribute,
toHaveValue,
toHaveProp,
toEmit,
toEmitWith
}
Expand Down

0 comments on commit d35646e

Please sign in to comment.