@@ -54,7 +54,7 @@ test('mounts on a specific element', () => {
54
54
attachTo: document .getElementById (' app' )
55
55
})
56
56
57
- console .log (document .body .innerHTML )
57
+ console .log (document .body .innerHTML )
58
58
/*
59
59
* <div>
60
60
* <h1>Non Vue app</h1>
@@ -695,23 +695,23 @@ export default {
695
695
``` js
696
696
import Foo from ' @/Foo.vue'
697
697
698
- test (' find ' , () => {
698
+ test (' findComponent ' , () => {
699
699
const wrapper = mount (Component)
700
700
701
701
// All the following queries would return a VueWrapper
702
702
703
703
// Using a standard querySelector query
704
- wrapper .find (' .foo' )
705
- wrapper .find (' [data-test="foo"]' )
704
+ wrapper .findComponent (' .foo' )
705
+ wrapper .findComponent (' [data-test="foo"]' )
706
706
707
707
// Using component's name
708
- wrapper .find ({ name: ' Foo' })
708
+ wrapper .findComponent ({ name: ' Foo' })
709
709
710
710
// Using ref attribute. Can be used only on direct children of the mounted component
711
- wrapper .find ({ ref: ' foo' })
711
+ wrapper .findComponent ({ ref: ' foo' })
712
712
713
713
// Using imported component
714
- wrapper .find (Foo)
714
+ wrapper .findComponent (Foo)
715
715
})
716
716
```
717
717
@@ -750,6 +750,92 @@ test('findAllComponents', () => {
750
750
})
751
751
```
752
752
753
+
754
+ ### ` get `
755
+
756
+ Similar to ` find ` , ` get ` looks for an element and returns a ` DOMWrapper ` if one is found. Otherwise it throws an error.
757
+
758
+ ` Component.vue ` :
759
+
760
+ ``` vue
761
+ <template>
762
+ <span>Span</span>
763
+ <span data-test="span">Span</span>
764
+ </template>
765
+ ```
766
+
767
+ ` Component.spec.js ` :
768
+
769
+ ``` js
770
+ test (' get' , () => {
771
+ const wrapper = mount (Component)
772
+
773
+ wrapper .get (' span' ) // => found; returns DOMWrapper
774
+ wrapper .get (' [data-test="span"]' ) // => found; returns DOMWrapper
775
+
776
+ expect (() => wrapper .getComponent (' p' )).toThrowError ()
777
+ })
778
+ ```
779
+
780
+
781
+ ### ` getComponent `
782
+
783
+ Similar to ` findComponent ` , ` getComponent ` looks for a Vue Component instance and returns a ` VueWrapper ` if one is found. Otherwise it throws an error.
784
+
785
+ ** Supported syntax:**
786
+
787
+ * ** querySelector** - ` getComponent('.component') ` - Matches standard query selector.
788
+ * ** Name** - ` getComponent({ name: 'myComponent' }) ` - matches PascalCase, snake-case, camelCase
789
+ * ** ref** - ` getComponent({ ref: 'dropdown' }) ` - Can be used only on direct ref children of mounted component
790
+ * ** SFC** - ` getComponent(ImportedComponent) ` - Pass an imported component directly.
791
+
792
+ ` Foo.vue `
793
+
794
+ ``` vue
795
+ <template>
796
+ <div class="foo">
797
+ Foo
798
+ </div>
799
+ </template>
800
+
801
+ <script>
802
+ export default {
803
+ name: 'Foo'
804
+ }
805
+ </script>
806
+ ```
807
+
808
+ ` Component.vue ` :
809
+
810
+ ``` vue
811
+ <template>
812
+ <Foo />
813
+ </template>
814
+
815
+ <script>
816
+ import Foo from '@/Foo'
817
+
818
+ export default {
819
+ components: { Foo },
820
+ }
821
+ </script>
822
+ ```
823
+
824
+ ` Component.spec.js `
825
+
826
+ ``` js
827
+ import Foo from ' @/Foo.vue'
828
+
829
+ test (' getComponent' , () => {
830
+ const wrapper = mount (Component)
831
+
832
+ wrapper .getComponent ({ name: ' foo' }) // returns a VueWrapper
833
+ wrapper .getComponent (Foo) // returns a VueWrapper
834
+
835
+ expect (() => wrapper .getComponent (' .not-there' )).toThrowError ()
836
+ })
837
+ ```
838
+
753
839
### ` html `
754
840
755
841
Returns the HTML (via ` outerHTML ` ) of an element. Useful for debugging.
@@ -814,7 +900,7 @@ test('props', () => {
814
900
const wrapper = mount (Component, {
815
901
global: { stubs: [' Foo' ] }
816
902
})
817
- const foo = wrapper .findComponent ({ name: ' Foo' })
903
+ const foo = wrapper .getComponent ({ name: ' Foo' })
818
904
819
905
expect (foo .props (' truthy' )).toBe (true )
820
906
expect (foo .props (' object' )).toEqual ({})
0 commit comments