Skip to content

Commit 14595ab

Browse files
committed
build: server-test-utils 1.0.0-beta.16
1 parent 7f2fa66 commit 14595ab

File tree

1 file changed

+70
-43
lines changed

1 file changed

+70
-43
lines changed

Diff for: packages/server-test-utils/dist/vue-server-test-utils.js

+70-43
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,46 @@ function validateSlots (slots) {
5656

5757
//
5858

59+
function isSingleElement (slotValue) {
60+
var _slotValue = slotValue.trim();
61+
if (_slotValue[0] !== '<' || _slotValue[_slotValue.length - 1] !== '>') {
62+
return false
63+
}
64+
var domParser = new window.DOMParser();
65+
var _document = domParser.parseFromString(slotValue, 'text/html');
66+
return _document.body.childElementCount === 1
67+
}
68+
69+
// see https://github.com/vuejs/vue-test-utils/pull/274
70+
function createVNodes (vm, slotValue) {
71+
var compiledResult = vueTemplateCompiler.compileToFunctions(("<div>" + slotValue + "{{ }}</div>"));
72+
var _staticRenderFns = vm._renderProxy.$options.staticRenderFns;
73+
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns;
74+
var elem = compiledResult.render.call(vm._renderProxy, vm.$createElement).children;
75+
vm._renderProxy.$options.staticRenderFns = _staticRenderFns;
76+
return elem
77+
}
78+
79+
function validateEnvironment () {
80+
if (!vueTemplateCompiler.compileToFunctions) {
81+
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined');
82+
}
83+
if (typeof window === 'undefined') {
84+
throwError('the slots string option does not support strings in server-test-uitls.');
85+
}
86+
if (window.navigator.userAgent.match(/PhantomJS/i)) {
87+
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.');
88+
}
89+
}
90+
5991
function addSlotToVm (vm, slotName, slotValue) {
6092
var elem;
6193
if (typeof slotValue === 'string') {
62-
if (!vueTemplateCompiler.compileToFunctions) {
63-
throwError('vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined');
64-
}
65-
if (typeof window === 'undefined') {
66-
throwError('the slots string option does not support strings in server-test-uitls.');
67-
}
68-
if (window.navigator.userAgent.match(/PhantomJS/i)) {
69-
throwError('the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.');
70-
}
71-
var domParser = new window.DOMParser();
72-
var _document = domParser.parseFromString(slotValue, 'text/html');
73-
var _slotValue = slotValue.trim();
74-
if (_slotValue[0] === '<' && _slotValue[_slotValue.length - 1] === '>' && _document.body.childElementCount === 1) {
94+
validateEnvironment();
95+
if (isSingleElement(slotValue)) {
7596
elem = vm.$createElement(vueTemplateCompiler.compileToFunctions(slotValue));
7697
} else {
77-
var compiledResult = vueTemplateCompiler.compileToFunctions(("<div>" + slotValue + "{{ }}</div>"));
78-
var _staticRenderFns = vm._renderProxy.$options.staticRenderFns;
79-
vm._renderProxy.$options.staticRenderFns = compiledResult.staticRenderFns;
80-
elem = compiledResult.render.call(vm._renderProxy, vm.$createElement).children;
81-
vm._renderProxy.$options.staticRenderFns = _staticRenderFns;
98+
elem = createVNodes(vm, slotValue);
8299
}
83100
} else {
84101
elem = vm.$createElement(slotValue);
@@ -217,9 +234,15 @@ function compileTemplate (component) {
217234
}
218235
});
219236
}
237+
220238
if (component.extends) {
221239
compileTemplate(component.extends);
222240
}
241+
242+
if (component.extendOptions && !component.options.render) {
243+
compileTemplate(component.options);
244+
}
245+
223246
if (component.template) {
224247
Object.assign(component, vueTemplateCompiler.compileToFunctions(component.template));
225248
}
@@ -339,25 +362,6 @@ function createComponentStubs (originalComponents, stubs) {
339362
return components
340363
}
341364

342-
//
343-
344-
function compileTemplate$1 (component) {
345-
if (component.components) {
346-
Object.keys(component.components).forEach(function (c) {
347-
var cmp = component.components[c];
348-
if (!cmp.render) {
349-
compileTemplate$1(cmp);
350-
}
351-
});
352-
}
353-
if (component.extends) {
354-
compileTemplate$1(component.extends);
355-
}
356-
if (component.template) {
357-
Object.assign(component, vueTemplateCompiler.compileToFunctions(component.template));
358-
}
359-
}
360-
361365
function deleteMountingOptions (options) {
362366
delete options.attachToDocument;
363367
delete options.mocks;
@@ -459,7 +463,7 @@ function createInstance (
459463
}
460464

461465
if (componentNeedsCompiling(component)) {
462-
compileTemplate$1(component);
466+
compileTemplate(component);
463467
}
464468

465469
addEventLogger(vue);
@@ -468,12 +472,29 @@ function createInstance (
468472

469473
var instanceOptions = Object.assign({}, options);
470474
deleteMountingOptions(instanceOptions);
475+
// $FlowIgnore
476+
var stubComponents = createComponentStubs(component.components, options.stubs);
477+
471478
if (options.stubs) {
472479
instanceOptions.components = Object.assign({}, instanceOptions.components,
473480
// $FlowIgnore
474-
createComponentStubs(component.components, options.stubs));
481+
stubComponents);
475482
}
476483

484+
Object.keys(component.components || {}).forEach(function (c) {
485+
if (component.components[c].extendOptions &&
486+
!instanceOptions.components[c]) {
487+
if (options.logModifiedComponents) {
488+
warn(("an extended child component " + c + " has been modified to ensure it has the correct instance properties. This means it is not possible to find the component with a component selector. To find the component, you must stub it manually using the mocks mounting option."));
489+
}
490+
instanceOptions.components[c] = vue.extend(component.components[c]);
491+
}
492+
});
493+
494+
Object.keys(stubComponents).forEach(function (c) {
495+
vue.component(c, stubComponents[c]);
496+
});
497+
477498
var vm = new Constructor(instanceOptions);
478499

479500
addAttrs(vm, options.attrs);
@@ -526,11 +547,15 @@ function createInstance (
526547
function getOptions (key, options, config) {
527548
if (options ||
528549
(config[key] && Object.keys(config[key]).length > 0)) {
529-
if (Array.isArray(options)) {
550+
if (options instanceof Function) {
551+
return options
552+
} else if (Array.isArray(options)) {
530553
return options.concat( Object.keys(config[key] || {}))
531-
} else {
554+
} else if (!(config[key] instanceof Function)) {
532555
return Object.assign({}, config[key],
533556
options)
557+
} else {
558+
throw new Error("Config can't be a Function.")
534559
}
535560
}
536561
}
@@ -540,9 +565,11 @@ function mergeOptions (
540565
config
541566
) {
542567
return Object.assign({}, options,
543-
{stubs: getOptions('stubs', options.stubs, config),
568+
{logModifiedComponents: config.logModifiedComponents,
569+
stubs: getOptions('stubs', options.stubs, config),
544570
mocks: getOptions('mocks', options.mocks, config),
545-
methods: getOptions('methods', options.methods, config)})
571+
methods: getOptions('methods', options.methods, config),
572+
provide: getOptions('provide', options.provide, config)})
546573
}
547574

548575
var config = testUtils.config

0 commit comments

Comments
 (0)