|
| 1 | +import { compileToFunctions } from 'vue-template-compiler' |
| 2 | +import Vue from 'vue' |
| 3 | +import { mount, shallowMount } from '~vue/test-utils' |
| 4 | +import Component from '~resources/components/component.vue' |
| 5 | +import ComponentWithChild from '~resources/components/component-with-child.vue' |
| 6 | +import ComponentWithNestedChildren from '~resources/components/component-with-nested-children.vue' |
| 7 | +import ComponentWithLifecycleHooks from '~resources/components/component-with-lifecycle-hooks.vue' |
| 8 | +import ComponentWithoutName from '~resources/components/component-without-name.vue' |
| 9 | +import ComponentAsAClassWithChild from '~resources/components/component-as-a-class-with-child.vue' |
| 10 | +import RecursiveComponent from '~resources/components/recursive-component.vue' |
| 11 | +import { vueVersion, describeIf } from '~resources/utils' |
| 12 | + |
| 13 | +describeIf(process.env.TEST_ENV !== 'node', |
| 14 | + 'shallow', () => { |
| 15 | + let info |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + info = sinon.stub(console, 'info') |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + info.restore() |
| 23 | + }) |
| 24 | + |
| 25 | + it('returns new VueWrapper of Vue localVue if no options are passed', () => { |
| 26 | + const compiled = compileToFunctions('<div><input /></div>') |
| 27 | + const wrapper = shallowMount(compiled) |
| 28 | + expect(wrapper.isVueComponent).to.equal(true) |
| 29 | + expect(wrapper.vm).to.be.an('object') |
| 30 | + }) |
| 31 | + |
| 32 | + it('returns new VueWrapper of Vue localVue with all children stubbed', () => { |
| 33 | + const wrapper = shallowMount(ComponentWithNestedChildren) |
| 34 | + expect(wrapper.isVueComponent).to.equal(true) |
| 35 | + expect(wrapper.findAll(Component).length).to.equal(0) |
| 36 | + expect(wrapper.findAll(ComponentWithChild).length).to.equal(1) |
| 37 | + }) |
| 38 | + |
| 39 | + it('returns new VueWrapper of Vue localVue with all children stubbed', () => { |
| 40 | + const wrapper = shallowMount(ComponentWithNestedChildren) |
| 41 | + expect(wrapper.isVueComponent).to.equal(true) |
| 42 | + expect(wrapper.findAll(Component).length).to.equal(0) |
| 43 | + expect(wrapper.findAll(ComponentWithChild).length).to.equal(1) |
| 44 | + }) |
| 45 | + |
| 46 | + it('does not modify component directly', () => { |
| 47 | + const wrapper = shallowMount(ComponentWithNestedChildren) |
| 48 | + expect(wrapper.findAll(Component).length).to.equal(0) |
| 49 | + const mountedWrapper = mount(ComponentWithNestedChildren) |
| 50 | + expect(mountedWrapper.findAll(Component).length).to.equal(1) |
| 51 | + }) |
| 52 | + |
| 53 | + it('stubs globally registered components when options.localVue is provided', () => { |
| 54 | + const localVue = Vue.extend() |
| 55 | + localVue.component('registered-component', ComponentWithLifecycleHooks) |
| 56 | + const Component = { |
| 57 | + render: h => h('registered-component') |
| 58 | + } |
| 59 | + shallowMount(Component, { localVue }) |
| 60 | + mount(Component, { localVue }) |
| 61 | + |
| 62 | + expect(info.callCount).to.equal(4) |
| 63 | + }) |
| 64 | + |
| 65 | + it('stubs globally registered components', () => { |
| 66 | + Vue.component('registered-component', ComponentWithLifecycleHooks) |
| 67 | + const Component = { |
| 68 | + render: h => h('registered-component') |
| 69 | + } |
| 70 | + shallowMount(Component) |
| 71 | + mount(Component) |
| 72 | + |
| 73 | + expect(info.callCount).to.equal(4) |
| 74 | + }) |
| 75 | + |
| 76 | + it('does not call stubbed children lifecycle hooks', () => { |
| 77 | + shallowMount(ComponentWithNestedChildren) |
| 78 | + expect(info.called).to.equal(false) |
| 79 | + }) |
| 80 | + |
| 81 | + it('stubs extended components', () => { |
| 82 | + const ComponentWithPTag = { |
| 83 | + template: `<p></p>` |
| 84 | + } |
| 85 | + const BaseComponent = { |
| 86 | + template: ` |
| 87 | + <div> |
| 88 | + <component-with-p-tag /> |
| 89 | + </div> |
| 90 | + `, |
| 91 | + components: { |
| 92 | + ComponentWithPTag |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const TestComponent = { |
| 97 | + extends: BaseComponent |
| 98 | + } |
| 99 | + |
| 100 | + const wrapper = shallowMount(TestComponent) |
| 101 | + expect(wrapper.find(ComponentWithPTag).exists()).to.equal(true) |
| 102 | + expect(wrapper.find('p').exists()).to.equal(false) |
| 103 | + }) |
| 104 | + |
| 105 | + it('stubs nested extended components', () => { |
| 106 | + const ComponentWithPTag = { |
| 107 | + template: `<p></p>` |
| 108 | + } |
| 109 | + const BaseComponent = { |
| 110 | + template: ` |
| 111 | + <div> |
| 112 | + <component-with-p-tag /> |
| 113 | + </div> |
| 114 | + `, |
| 115 | + components: { |
| 116 | + ComponentWithPTag |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const ExtendedBaseComponent = { |
| 121 | + extends: BaseComponent |
| 122 | + } |
| 123 | + |
| 124 | + const TestComponent = { |
| 125 | + extends: ExtendedBaseComponent |
| 126 | + } |
| 127 | + |
| 128 | + const wrapper = shallowMount(TestComponent) |
| 129 | + expect(wrapper.find(ComponentWithPTag).exists()).to.equal(true) |
| 130 | + expect(wrapper.find('p').exists()).to.equal(false) |
| 131 | + }) |
| 132 | + |
| 133 | + it('stubs Vue class component children', () => { |
| 134 | + if (vueVersion < 2.3) { |
| 135 | + return |
| 136 | + } |
| 137 | + const wrapper = shallowMount(ComponentAsAClassWithChild) |
| 138 | + expect(wrapper.find(Component).exists()).to.equal(true) |
| 139 | + expect(wrapper.findAll('div').length).to.equal(1) |
| 140 | + }) |
| 141 | + |
| 142 | + it('works correctly with find, contains, findAll, and is on unnamed components', () => { |
| 143 | + const TestComponent = { |
| 144 | + template: ` |
| 145 | + <div> |
| 146 | + <component-without-name /> |
| 147 | + </div> |
| 148 | + `, |
| 149 | + components: { |
| 150 | + ComponentWithoutName |
| 151 | + } |
| 152 | + } |
| 153 | + const wrapper = shallowMount(TestComponent) |
| 154 | + expect(wrapper.contains(ComponentWithoutName)).to.equal(true) |
| 155 | + expect(wrapper.find(ComponentWithoutName).exists()).to.equal(true) |
| 156 | + expect(wrapper.findAll(ComponentWithoutName).length).to.equal(1) |
| 157 | + }) |
| 158 | + |
| 159 | + it('works correctly with find, contains, findAll, and is on named components', () => { |
| 160 | + const TestComponent = { |
| 161 | + template: ` |
| 162 | + <div> |
| 163 | + <a-component /> |
| 164 | + </div> |
| 165 | + `, |
| 166 | + components: { |
| 167 | + AComponent: Component |
| 168 | + } |
| 169 | + } |
| 170 | + const wrapper = shallowMount(TestComponent) |
| 171 | + expect(wrapper.contains(Component)).to.equal(true) |
| 172 | + expect(wrapper.find(Component).exists()).to.equal(true) |
| 173 | + expect(wrapper.findAll(Component).length).to.equal(1) |
| 174 | + }) |
| 175 | + |
| 176 | + it('works correctly with find on recursive components', () => { |
| 177 | + // this is for a bug that I've been unable to replicate. |
| 178 | + // Sometimes components mutate their components, in this line— |
| 179 | + RecursiveComponent.components = { |
| 180 | + RecursiveComponent: { render: h => h('div') } |
| 181 | + } |
| 182 | + |
| 183 | + expect(shallowMount(RecursiveComponent, { |
| 184 | + propsData: { |
| 185 | + items: ['', ''] |
| 186 | + } |
| 187 | + }).findAll(RecursiveComponent).length).to.equal(2) |
| 188 | + RecursiveComponent.components = { |
| 189 | + 'recursive-component': { render: h => h('div') } |
| 190 | + } |
| 191 | + expect(shallowMount(RecursiveComponent, { |
| 192 | + propsData: { |
| 193 | + items: ['', ''] |
| 194 | + } |
| 195 | + }).findAll(RecursiveComponent).length).to.equal(2) |
| 196 | + }) |
| 197 | + |
| 198 | + it('throws an error when the component fails to mount', () => { |
| 199 | + expect(() => shallowMount({ |
| 200 | + template: '<div></div>', |
| 201 | + mounted: function () { |
| 202 | + throw (new Error('Error')) |
| 203 | + } |
| 204 | + })).to.throw() |
| 205 | + }) |
| 206 | + }) |
0 commit comments