Skip to content

Commit 8a46240

Browse files
committed
[New] Handle find and findAll error
1 parent a7b7c83 commit 8a46240

File tree

5 files changed

+93
-16
lines changed

5 files changed

+93
-16
lines changed

Diff for: flow/wrapper.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import type Wrapper from '../src/Wrapper'
44
import type WrapperArray from '../src/WrapperArray'
55

66
declare interface BaseWrapper { // eslint-disable-line no-undef
7-
at(index: number): Wrapper,
8-
contains(selector: String | Component): boolean,
9-
hasAttribute(attribute: string, value: string): boolean,
10-
hasClass(className: string): boolean,
11-
hasProp(prop: string, value: string): boolean,
12-
hasStyle(style: string, value: string): boolean,
7+
at(index: number): Wrapper | void,
8+
contains(selector: String | Component): boolean | void,
9+
hasAttribute(attribute: string, value: string): boolean | void,
10+
hasClass(className: string): boolean | void,
11+
hasProp(prop: string, value: string): boolean | void,
12+
hasStyle(style: string, value: string): boolean | void,
1313
find(selector: string | Component): Wrapper | void,
1414
findAll(selector: string | Component): WrapperArray | void,
1515
html(): string | void,
16-
is(selector: string | Component): boolean,
17-
isEmpty(): boolean,
18-
isVueInstance(): boolean,
16+
is(selector: string | Component): boolean | void,
17+
isEmpty(): boolean | void,
18+
isVueInstance(): boolean | void,
1919
name(): string | void,
2020
text(): string | void,
2121
setData(data: Object): void,
@@ -25,5 +25,6 @@ declare interface BaseWrapper { // eslint-disable-line no-undef
2525
}
2626

2727
declare type WrapperOptions = { // eslint-disable-line no-undef
28-
attachedToDocument: boolean
28+
attachedToDocument: boolean,
29+
error?: string
2930
}

Diff for: src/ErrorWrapper.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// @flow
2+
3+
export default class ErrorWrapper implements BaseWrapper {
4+
5+
at (): void {
6+
}
7+
8+
contains (): void {
9+
}
10+
11+
hasAttribute (): void {
12+
13+
}
14+
15+
hasClass (): void {
16+
}
17+
18+
hasProp (): void {
19+
}
20+
21+
hasStyle (): void {
22+
}
23+
24+
findAll (): void {
25+
}
26+
27+
find (): void {
28+
}
29+
30+
html (): void {
31+
}
32+
33+
is (): void {
34+
}
35+
36+
isEmpty (): void {
37+
}
38+
39+
isVueInstance (): void {
40+
}
41+
42+
name (): void {
43+
}
44+
45+
text (): void {
46+
47+
}
48+
49+
setData (): void {
50+
}
51+
52+
setProps (): void {
53+
}
54+
55+
trigger (): void {
56+
}
57+
58+
update (): void {
59+
}
60+
}

Diff for: src/Wrapper.js

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import findVueComponents from './lib/findVueComponents'
66
import findMatchingVNodes from './lib/findMatchingVNodes'
77
import VueWrapper from './VueWrapper'
88
import WrapperArray from './WrapperArray'
9+
import ErrorWrapper from './ErrorWrapper'
910

1011
export default class Wrapper implements BaseWrapper {
1112
vnode: VNode;
@@ -136,11 +137,17 @@ export default class Wrapper implements BaseWrapper {
136137
}
137138
const vm = this.vm || this.vnode.context.$root
138139
const components = findVueComponents(vm, selector.name)
140+
if (components.length === 0) {
141+
return new ErrorWrapper()
142+
}
139143
return new VueWrapper(components[0], this.options)
140144
}
141145

142146
const nodes = findMatchingVNodes(this.vnode, selector)
143147

148+
if (nodes.length === 0) {
149+
return new ErrorWrapper()
150+
}
144151
return new Wrapper(nodes[0], this.update, this.options)
145152
}
146153

Diff for: test/unit/specs/mount/find.spec.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ComponentWithSlots from '../../../resources/components/component-with-slo
66
import ComponentWithVFor from '../../../resources/components/component-with-v-for.vue'
77
import Component from '../../../resources/components/component.vue'
88
import Wrapper from '../../../../src/Wrapper'
9+
import ErrorWrapper from '../../../../src/ErrorWrapper'
910

1011
describe('find', () => {
1112
it('returns an array of Wrappers of elements matching tag selector passed', () => {
@@ -102,10 +103,17 @@ describe('find', () => {
102103
expect(() => wrapper.find(ComponentWithoutName)).to.throw(Error, message)
103104
})
104105

105-
it.skip('returns an empty Wrapper if no nodes matching selector are found', () => {
106+
it('returns empty Wrapper with error if no nodes are found', () => {
106107
const wrapper = mount(Component)
107-
const secondChildComponents = wrapper.find('pre')
108-
expect(secondChildComponents.length).to.equal(0)
108+
const selector = 'pre'
109+
const error = wrapper.find(selector)
110+
expect(error).to.be.instanceOf(ErrorWrapper)
111+
})
112+
113+
it('returns empty Wrapper with error if no nodes are found when passed a component', () => {
114+
const wrapper = mount(Component)
115+
const error = wrapper.find(ComponentWithChildComponent)
116+
expect(error).to.be.instanceOf(ErrorWrapper)
109117
})
110118

111119
it('throws an error if selector is not a valid selector', () => {

Diff for: test/unit/specs/mount/findAll.spec.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ describe('findAll', () => {
133133
expect(() => wrapper.findAll(ComponentWithoutName)).to.throw(Error, message)
134134
})
135135

136-
it.skip('returns an empty array if no nodes matching selector are found', () => {
136+
it('returns VueWrapper with length 0 if no nodes matching selector are found', () => {
137137
const wrapper = mount(Component)
138-
const secondChildComponents = wrapper.findAll('pre')
139-
expect(secondChildComponents.length).to.equal(0)
138+
const preArray = wrapper.findAll('pre')
139+
expect(preArray.length).to.equal(0)
140+
expect(preArray.wrappers).to.deep.equal([])
140141
})
141142

142143
it('throws an error if selector is not a valid selector', () => {

0 commit comments

Comments
 (0)