@@ -149,20 +149,51 @@ describeWithShallowAndMount('findAll', mountingMethod => {
149149 expect ( componentArr . length ) . toEqual ( 1 )
150150 } )
151151
152- it ( 'throws an error if findAllComponents selector is a CSS selector' , ( ) => {
153- const wrapper = mountingMethod ( Component )
154- const message =
155- '[vue-test-utils]: findAllComponents requires a Vue constructor or valid find object. If you are searching for DOM nodes, use `find` instead'
156- const fn = ( ) => wrapper . findAllComponents ( '#foo' )
157- expect ( fn ) . toThrow ( message )
158- } )
152+ it ( 'findAllComponents ignores DOM nodes matching same CSS selector' , ( ) => {
153+ const RootComponent = {
154+ components : { Component } ,
155+ template : '<div><Component class="foo" /><div class="foo"></div></div>'
156+ }
157+ const wrapper = mountingMethod ( RootComponent )
158+ expect ( wrapper . findAllComponents ( '.foo' ) ) . toHaveLength ( 1 )
159+ expect (
160+ wrapper
161+ . findAllComponents ( '.foo' )
162+ . at ( 0 )
163+ . is ( Component )
164+ ) . toBe ( true )
165+ } )
166+
167+ it ( 'findAllComponents returns top-level components when components are nested' , ( ) => {
168+ const DeepNestedChild = {
169+ name : 'DeepNestedChild' ,
170+ template : '<div>I am deeply nested</div>'
171+ }
172+ const NestedChild = {
173+ name : 'NestedChild' ,
174+ components : { DeepNestedChild } ,
175+ template : '<deep-nested-child class="in-child" />'
176+ }
177+ const RootComponent = {
178+ name : 'RootComponent' ,
179+ components : { NestedChild } ,
180+ template : '<div><nested-child class="in-root"></nested-child></div>'
181+ }
159182
160- it ( 'throws an error if chaining findAllComponents off a DOM element' , ( ) => {
161- const wrapper = mountingMethod ( ComponentWithChild )
162- const message =
163- '[vue-test-utils]: You cannot chain findAllComponents off a DOM element. It can only be used on Vue Components.'
164- const fn = ( ) => wrapper . find ( 'span' ) . findAllComponents ( '#foo' )
165- expect ( fn ) . toThrow ( message )
183+ const wrapper = mountingMethod ( RootComponent , { stubs : { NestedChild } } )
184+
185+ expect ( wrapper . findAllComponents ( '.in-root' ) ) . toHaveLength ( 1 )
186+ expect (
187+ wrapper . findAllComponents ( '.in-root' ) . at ( 0 ) . vm . $options . name
188+ ) . toEqual ( 'NestedChild' )
189+
190+ expect ( wrapper . findAllComponents ( '.in-child' ) ) . toHaveLength ( 1 )
191+
192+ // someone might expect DeepNestedChild here, but
193+ // we always return TOP component matching DOM element
194+ expect (
195+ wrapper . findAllComponents ( '.in-child' ) . at ( 0 ) . vm . $options . name
196+ ) . toEqual ( 'NestedChild' )
166197 } )
167198
168199 it ( 'returns correct number of Vue Wrapper when component has a v-for' , ( ) => {
0 commit comments