Skip to content

Commit d35646e

Browse files
committed
feat: add toHaveProp matcher
1 parent 005d07f commit d35646e

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ describe('MyComponent', () => {
6262
expect('input[type=text]').toHaveValue('plop')
6363
expect('input[type=text]').not.toHaveValue('not plop')
6464
```
65+
* toHaveProp(propName)
66+
```js
67+
expect(wrapper).toHaveProp('propName')
68+
expect(wrapper).not.toHaveProp('not-propName')
69+
```
6570
* toEmit(eventName)
6671
```js
6772
expect(wrapper).toEmit('eventName')

src/components/Questions.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ describe('Questions', () => {
6666
})
6767
})
6868

69+
describe('toHaveProp', () => {
70+
const prop = 'name'
71+
72+
it('can success', () => {
73+
expect(wrapper).toHaveProp(prop)
74+
75+
const { pass } = matchers(wrapper).toHaveProp(wrapper, prop)
76+
expect(pass).toBeTruthy()
77+
})
78+
79+
it('can fail', () => {
80+
const { pass } = matchers(wrapper).toHaveProp(wrapper, `not-${prop}`)
81+
expect(pass).toBeFalsy()
82+
})
83+
84+
it('can reverse', () => {
85+
expect(wrapper).not.toHaveProp(`not-${prop}`)
86+
})
87+
})
88+
6989
describe('toEmit', () => {
7090
const eventName = 'isEditing'
7191
const selector = '.edit'

src/components/Questions.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export default {
3030
data: () => ({
3131
editing: false
3232
}),
33+
props: {
34+
name: {
35+
type: String
36+
},
37+
plop: {
38+
type: String
39+
}
40+
},
3341
methods: {
3442
update () {
3543
this.$emit('applied')

src/matchers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const toHaveAttribute = (selector, attr, value) =>
3232
const toHaveValue = (selector, value) =>
3333
matcher.toBe(w.find(selector).element.value, value)
3434

35+
const toHaveProp = (selector, propName) =>
36+
matcher.toHaveProperty(selector.props(), propName)
37+
3538
const toEmit = (selector = w, event) =>
3639
matcher.toBeTruthy(selector.emitted()[event])
3740

@@ -66,6 +69,7 @@ const matchers = wrapper => {
6669
toHaveAClass,
6770
toHaveAttribute,
6871
toHaveValue,
72+
toHaveProp,
6973
toEmit,
7074
toEmitWith
7175
}

0 commit comments

Comments
 (0)