Skip to content

Commit 3cd8ba1

Browse files
authored
refactor: add checks for wrapper.element
* refactor: check for element in hasClass * fix: check for element in hasAttribute * fix: check for element in text * fix: check for element in trigger * fix: return boolean in hasAttribute
1 parent dcd6a9d commit 3cd8ba1

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

src/wrappers/wrapper.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class Wrapper implements BaseWrapper {
6666
throwError('wrapper.hasAttribute() must be passed value as a string')
6767
}
6868

69-
return this.element && this.element.getAttribute(attribute) === value
69+
return !!(this.element && this.element.getAttribute(attribute) === value)
7070
}
7171

7272
/**
@@ -77,7 +77,8 @@ export default class Wrapper implements BaseWrapper {
7777
throwError('wrapper.hasClass() must be passed a string')
7878
}
7979

80-
return this.element.className.split(' ').indexOf(className) !== -1
80+
return !!(this.element &&
81+
this.element.className.split(' ').indexOf(className) !== -1)
8182
}
8283

8384
/**
@@ -216,7 +217,10 @@ export default class Wrapper implements BaseWrapper {
216217
}
217218
return vmCtorMatchesName(this.vm, selector.name)
218219
}
219-
return this.element.getAttribute && this.element.matches(selector)
220+
221+
return !!(this.element &&
222+
this.element.getAttribute &&
223+
this.element.matches(selector))
220224
}
221225

222226
/**
@@ -308,6 +312,10 @@ export default class Wrapper implements BaseWrapper {
308312
* Return text of wrapper element
309313
*/
310314
text (): string {
315+
if (!this.element) {
316+
throwError('cannot call wrapper.text() on a wrapper without an element')
317+
}
318+
311319
return this.element.textContent
312320
}
313321

@@ -319,6 +327,10 @@ export default class Wrapper implements BaseWrapper {
319327
throwError('wrapper.trigger() must be passed a string')
320328
}
321329

330+
if (!this.element) {
331+
throwError('cannot call wrapper.trigger() on a wrapper without an element')
332+
}
333+
322334
const modifiers = {
323335
enter: 13,
324336
tab: 9,

test/unit/specs/mount/Wrapper/hasAttribute.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ describe('hasAttribute', () => {
1616
expect(wrapper.hasAttribute('attribute', 'value')).to.equal(false)
1717
})
1818

19+
it('returns false if wrapper element is null', () => {
20+
const compiled = compileToFunctions('<div />')
21+
const wrapper = mount(compiled)
22+
wrapper.element = null
23+
expect(wrapper.hasAttribute('attribute', 'value')).to.equal(false)
24+
})
25+
1926
it('throws an error if attribute is not a string', () => {
2027
const compiled = compileToFunctions('<div />')
2128
const wrapper = mount(compiled)

test/unit/specs/mount/Wrapper/hasClass.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ describe('hasClass', () => {
1414
expect(wrapper.hasClass('not-class-name')).to.equal(false)
1515
})
1616

17+
it('returns false if wrapper includes class name in string, but not as a seperate class', () => {
18+
const compiled = compileToFunctions('<div class="class-name-together"/>')
19+
const wrapper = mount(compiled)
20+
expect(wrapper.hasClass('class-name')).to.equal(false)
21+
})
22+
23+
it('returns false if wrapper does not have an element', () => {
24+
const compiled = compileToFunctions('<div />')
25+
const wrapper = mount(compiled)
26+
wrapper.element = null
27+
expect(wrapper.hasClass('not-class-name')).to.equal(false)
28+
})
29+
1730
it('throws an error if selector is not a string', () => {
1831
const compiled = compileToFunctions('<div />')
1932
const wrapper = mount(compiled)

test/unit/specs/mount/Wrapper/is.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ describe('is', () => {
2222
expect(wrapper.is('#div')).to.equal(true)
2323
})
2424

25+
it('returns false if wrapper does not contain element', () => {
26+
const wrapper = mount(ComponentWithChild)
27+
wrapper.element = null
28+
expect(wrapper.is('a')).to.equal(false)
29+
})
30+
2531
it('returns true if root node matches Vue Component selector', () => {
2632
const wrapper = mount(ComponentWithChild)
2733
const component = wrapper.findAll(Component).at(0)

test/unit/specs/mount/Wrapper/text.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,13 @@ describe('text', () => {
99

1010
expect(wrapper.text()).to.equal(text)
1111
})
12+
13+
it('throws error if wrapper does not contain elememnt', () => {
14+
const compiled = compileToFunctions(`<div />`)
15+
const wrapper = mount(compiled)
16+
wrapper.element = null
17+
const fn = () => wrapper.text()
18+
const message = '[vue-test-utils]: cannot call wrapper.text() on a wrapper without an element'
19+
expect(fn).to.throw().with.property('message', message)
20+
})
1221
})

test/unit/specs/mount/Wrapper/trigger.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ describe('trigger', () => {
7474
expect(info.calledWith(true)).to.equal(true)
7575
})
7676

77+
it('throws error if wrapper does not contain eleemnt', () => {
78+
const wrapper = mount({ render: () => {} })
79+
wrapper.element = null
80+
const fn = () => wrapper.trigger('click')
81+
const message = '[vue-test-utils]: cannot call wrapper.trigger() on a wrapper without an element'
82+
expect(fn).to.throw().with.property('message', message)
83+
})
84+
7785
it('throws an error if type is not a string', () => {
7886
const wrapper = mount(ComponentWithEvents)
7987
const invalidSelectors = [

0 commit comments

Comments
 (0)