|
1 | | -import { ComponentPublicInstance } from 'vue' |
| 1 | +import { ComponentPublicInstance, h } from 'vue' |
2 | 2 |
|
3 | 3 | import { mount, config, VueWrapper } from '../../src' |
4 | 4 |
|
@@ -92,3 +92,102 @@ describe('Plugin#install', () => { |
92 | 92 | ) |
93 | 93 | }) |
94 | 94 | }) |
| 95 | + |
| 96 | +describe('createStubs', () => { |
| 97 | + const Child1 = { |
| 98 | + name: 'child1', |
| 99 | + render: () => h('div', 'real child 1') |
| 100 | + } |
| 101 | + const Child2 = { |
| 102 | + name: 'child2', |
| 103 | + render: () => h('div', 'real child 2') |
| 104 | + } |
| 105 | + |
| 106 | + const Parent = { |
| 107 | + render: () => h('div', [ |
| 108 | + h(Child1), |
| 109 | + h(Child1), |
| 110 | + h(Child2) |
| 111 | + ]) |
| 112 | + } |
| 113 | + |
| 114 | + const customCreateStub = jest.fn(({ name }) => h(`${name}-custom-stub`)) |
| 115 | + beforeAll(() => { |
| 116 | + config.plugins.createStubs = customCreateStub |
| 117 | + }) |
| 118 | + |
| 119 | + afterAll(() => { |
| 120 | + config.plugins.createStubs = undefined |
| 121 | + }) |
| 122 | + |
| 123 | + beforeEach(() => { |
| 124 | + customCreateStub.mockClear() |
| 125 | + }) |
| 126 | + |
| 127 | + it('should be called for every stub once', () => { |
| 128 | + const wrapper = mount(Parent, { |
| 129 | + shallow: true |
| 130 | + }) |
| 131 | + |
| 132 | + expect(wrapper.html()).toBe('<div>\n' + |
| 133 | + ' <child1-custom-stub></child1-custom-stub>\n' + |
| 134 | + ' <child1-custom-stub></child1-custom-stub>\n' + |
| 135 | + ' <child2-custom-stub></child2-custom-stub>\n' + |
| 136 | + '</div>') |
| 137 | + |
| 138 | + expect(customCreateStub).toHaveBeenCalledTimes(2) |
| 139 | + expect(customCreateStub).toHaveBeenCalledWith({ name: 'child1', component: Child1 }) |
| 140 | + expect(customCreateStub).toHaveBeenCalledWith({ name: 'child2', component: Child2 }) |
| 141 | + }) |
| 142 | + |
| 143 | + it('should be called only for stubbed components', () => { |
| 144 | + const wrapper = mount(Parent, { |
| 145 | + global: { |
| 146 | + stubs: { |
| 147 | + child2: true |
| 148 | + } |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + expect(wrapper.html()).toBe('<div>\n' + |
| 153 | + ' <div>real child 1</div>\n' + |
| 154 | + ' <div>real child 1</div>\n' + |
| 155 | + ' <child2-custom-stub></child2-custom-stub>\n' + |
| 156 | + '</div>') |
| 157 | + |
| 158 | + expect(customCreateStub).toHaveBeenCalledTimes(1) |
| 159 | + expect(customCreateStub).toHaveBeenCalledWith({ name: 'child2', component: Child2 }) |
| 160 | + }) |
| 161 | + |
| 162 | + it('should not be called for no stubs', () => { |
| 163 | + const wrapper = mount(Parent) |
| 164 | + |
| 165 | + expect(wrapper.html()).toBe('<div>\n' + |
| 166 | + ' <div>real child 1</div>\n' + |
| 167 | + ' <div>real child 1</div>\n' + |
| 168 | + ' <div>real child 2</div>\n' + |
| 169 | + '</div>') |
| 170 | + |
| 171 | + expect(customCreateStub).not.toHaveBeenCalled() |
| 172 | + }) |
| 173 | + |
| 174 | + it('should not be called for manual stubs', () => { |
| 175 | + const wrapper = mount(Parent, { |
| 176 | + shallow: true, |
| 177 | + global: { |
| 178 | + stubs: { |
| 179 | + child2: () => h('div', 'Child 2 stub') |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
| 183 | + |
| 184 | + expect(wrapper.html()).toBe('<div>\n' + |
| 185 | + ' <child1-custom-stub></child1-custom-stub>\n' + |
| 186 | + ' <child1-custom-stub></child1-custom-stub>\n' + |
| 187 | + ' <div>Child 2 stub</div>\n' + |
| 188 | + '</div>') |
| 189 | + |
| 190 | + expect(customCreateStub).toHaveBeenCalledTimes(1) |
| 191 | + expect(customCreateStub).toHaveBeenCalledWith({ name: 'child1', component: Child1 }) |
| 192 | + }) |
| 193 | +}) |
0 commit comments