|
1 | 1 | // @flow
|
2 | 2 |
|
| 3 | +import Vue from 'vue' |
3 | 4 | import { addSlots } from './add-slots'
|
| 5 | +import { addScopedSlots } from './add-scoped-slots' |
4 | 6 | import addMocks from './add-mocks'
|
5 | 7 | import addAttrs from './add-attrs'
|
6 | 8 | import addListeners from './add-listeners'
|
@@ -57,6 +59,40 @@ export default function createInstance (
|
57 | 59 | addAttrs(vm, options.attrs)
|
58 | 60 | addListeners(vm, options.listeners)
|
59 | 61 |
|
| 62 | + if (options.scopedSlots) { |
| 63 | + if (window.navigator.userAgent.match(/PhantomJS/i)) { |
| 64 | + throwError('the scopedSlots option does not support PhantomJS. Please use Puppeteer, or pass a component.') |
| 65 | + } |
| 66 | + const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`) |
| 67 | + if (vueVersion >= 2.5) { |
| 68 | + vm.$_vueTestUtils_scopedSlots = {} |
| 69 | + vm.$_vueTestUtils_slotScopes = {} |
| 70 | + const renderSlot = vm._renderProxy._t |
| 71 | + |
| 72 | + vm._renderProxy._t = function (name, feedback, props, bindObject) { |
| 73 | + const scopedSlotFn = vm.$_vueTestUtils_scopedSlots[name] |
| 74 | + const slotScope = vm.$_vueTestUtils_slotScopes[name] |
| 75 | + if (scopedSlotFn) { |
| 76 | + props = { ...bindObject, ...props } |
| 77 | + const proxy = {} |
| 78 | + const helpers = ['_c', '_o', '_n', '_s', '_l', '_t', '_q', '_i', '_m', '_f', '_k', '_b', '_v', '_e', '_u', '_g'] |
| 79 | + helpers.forEach((key) => { |
| 80 | + proxy[key] = vm._renderProxy[key] |
| 81 | + }) |
| 82 | + proxy[slotScope] = props |
| 83 | + return scopedSlotFn.call(proxy) |
| 84 | + } else { |
| 85 | + return renderSlot.call(vm._renderProxy, name, feedback, props, bindObject) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // $FlowIgnore |
| 90 | + addScopedSlots(vm, options.scopedSlots) |
| 91 | + } else { |
| 92 | + throwError('the scopedSlots option is only supported in [email protected]+.') |
| 93 | + } |
| 94 | + } |
| 95 | + |
60 | 96 | if (options.slots) {
|
61 | 97 | addSlots(vm, options.slots)
|
62 | 98 | }
|
|
0 commit comments