@@ -27,15 +27,17 @@ var capitalize = function (str) { return str.charAt(0).toUpperCase() + str.slice
27
27
var hyphenateRE = / \B ( [ A - Z ] ) / g;
28
28
var hyphenate = function ( str ) { return str . replace ( hyphenateRE , '-$1' ) . toLowerCase ( ) ; } ;
29
29
30
- if ( typeof window === 'undefined' ) {
31
- throwError (
32
- 'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
33
- 'You can run the tests in node using jsdom + jsdom-global.\n' +
34
- 'See https://vue-test-utils.vuejs.org/en/guides/common-tips.html for more details.'
35
- ) ;
30
+ function warnIfNoWindow ( ) {
31
+ if ( typeof window === 'undefined' ) {
32
+ throwError (
33
+ 'window is undefined, vue-test-utils needs to be run in a browser environment.\n' +
34
+ 'You can run the tests in node using jsdom + jsdom-global.\n' +
35
+ 'See https://vue-test-utils.vuejs.org/en/guides/common-tips.html for more details.'
36
+ ) ;
37
+ }
36
38
}
37
39
38
- if ( ! Element . prototype . matches ) {
40
+ if ( typeof Element !== 'undefined' && ! Element . prototype . matches ) {
39
41
Element . prototype . matches =
40
42
Element . prototype . matchesSelector ||
41
43
Element . prototype . mozMatchesSelector ||
@@ -122,13 +124,11 @@ function componentNeedsCompiling (component) {
122
124
}
123
125
124
126
function isRefSelector ( refOptionsObject ) {
125
- if ( typeof refOptionsObject !== 'object' || ! Object . keys ( refOptionsObject || { } ) . length ) {
127
+ if ( typeof refOptionsObject !== 'object' || Object . keys ( refOptionsObject || { } ) . length !== 1 ) {
126
128
return false
127
129
}
128
130
129
- return Object
130
- . keys ( refOptionsObject )
131
- . every ( function ( key ) { return [ 'ref' ] . includes ( key ) && typeof refOptionsObject [ key ] === 'string' ; } )
131
+ return typeof refOptionsObject . ref === 'string'
132
132
}
133
133
134
134
function isNameSelector ( nameOptionsObject ) {
@@ -1457,13 +1457,16 @@ function addSlotToVm (vm, slotName, slotValue) {
1457
1457
if ( ! vueTemplateCompiler . compileToFunctions ) {
1458
1458
throwError ( 'vueTemplateCompiler is undefined, you must pass components explicitly if vue-template-compiler is undefined' ) ;
1459
1459
}
1460
+ if ( typeof window === 'undefined' ) {
1461
+ throwError ( 'the slots string option does not support strings in server-test-uitls.' ) ;
1462
+ }
1460
1463
if ( window . navigator . userAgent . match ( / P h a n t o m J S / i) ) {
1461
- throwError ( 'option. slots does not support strings in PhantomJS. Please use Puppeteer, or pass a component' ) ;
1464
+ throwError ( 'the slots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component. ' ) ;
1462
1465
}
1463
1466
var domParser = new window . DOMParser ( ) ;
1464
- var document = domParser . parseFromString ( slotValue , 'text/html' ) ;
1467
+ var _document = domParser . parseFromString ( slotValue , 'text/html' ) ;
1465
1468
var _slotValue = slotValue . trim ( ) ;
1466
- if ( _slotValue [ 0 ] === '<' && _slotValue [ _slotValue . length - 1 ] === '>' && document . body . childElementCount === 1 ) {
1469
+ if ( _slotValue [ 0 ] === '<' && _slotValue [ _slotValue . length - 1 ] === '>' && _document . body . childElementCount === 1 ) {
1467
1470
elem = vm . $createElement ( vueTemplateCompiler . compileToFunctions ( slotValue ) ) ;
1468
1471
} else {
1469
1472
var compiledResult = vueTemplateCompiler . compileToFunctions ( ( "<div>" + slotValue + "{{ }}</div>" ) ) ;
@@ -1505,6 +1508,21 @@ function addSlots (vm, slots) {
1505
1508
1506
1509
//
1507
1510
1511
+ function addScopedSlots ( vm , scopedSlots ) {
1512
+ Object . keys ( scopedSlots ) . forEach ( function ( key ) {
1513
+ var template = scopedSlots [ key ] . trim ( ) ;
1514
+ if ( template . substr ( 0 , 9 ) === '<template' ) {
1515
+ throwError ( 'the scopedSlots option does not support a template tag as the root element.' ) ;
1516
+ }
1517
+ var domParser = new window . DOMParser ( ) ;
1518
+ var _document = domParser . parseFromString ( template , 'text/html' ) ;
1519
+ vm . $_vueTestUtils_scopedSlots [ key ] = vueTemplateCompiler . compileToFunctions ( template ) . render ;
1520
+ vm . $_vueTestUtils_slotScopes [ key ] = _document . body . firstChild . getAttribute ( 'slot-scope' ) ;
1521
+ } ) ;
1522
+ }
1523
+
1524
+ //
1525
+
1508
1526
function addMocks ( mockedProperties , Vue$$1 ) {
1509
1527
Object . keys ( mockedProperties ) . forEach ( function ( key ) {
1510
1528
try {
@@ -1850,6 +1868,19 @@ function createFunctionalComponent (component, mountingOptions) {
1850
1868
1851
1869
//
1852
1870
1871
+ function isDestructuringSlotScope ( slotScope ) {
1872
+ return slotScope [ 0 ] === '{' && slotScope [ slotScope . length - 1 ] === '}'
1873
+ }
1874
+
1875
+ function getVueTemplateCompilerHelpers ( proxy ) {
1876
+ var helpers = { } ;
1877
+ var names = [ '_c' , '_o' , '_n' , '_s' , '_l' , '_t' , '_q' , '_i' , '_m' , '_f' , '_k' , '_b' , '_v' , '_e' , '_u' , '_g' ] ;
1878
+ names . forEach ( function ( name ) {
1879
+ helpers [ name ] = proxy [ name ] ;
1880
+ } ) ;
1881
+ return helpers
1882
+ }
1883
+
1853
1884
function createInstance (
1854
1885
component ,
1855
1886
options ,
@@ -1892,6 +1923,41 @@ function createInstance (
1892
1923
addAttrs ( vm , options . attrs ) ;
1893
1924
addListeners ( vm , options . listeners ) ;
1894
1925
1926
+ if ( options . scopedSlots ) {
1927
+ if ( window . navigator . userAgent . match ( / P h a n t o m J S / i) ) {
1928
+ throwError ( 'the scopedSlots option does not support PhantomJS. Please use Puppeteer, or pass a component.' ) ;
1929
+ }
1930
+ var vueVersion = Number ( ( ( Vue . version . split ( '.' ) [ 0 ] ) + "." + ( Vue . version . split ( '.' ) [ 1 ] ) ) ) ;
1931
+ if ( vueVersion >= 2.5 ) {
1932
+ vm . $_vueTestUtils_scopedSlots = { } ;
1933
+ vm . $_vueTestUtils_slotScopes = { } ;
1934
+ var renderSlot = vm . _renderProxy . _t ;
1935
+
1936
+ vm . _renderProxy . _t = function ( name , feedback , props , bindObject ) {
1937
+ var scopedSlotFn = vm . $_vueTestUtils_scopedSlots [ name ] ;
1938
+ var slotScope = vm . $_vueTestUtils_slotScopes [ name ] ;
1939
+ if ( scopedSlotFn ) {
1940
+ props = Object . assign ( { } , bindObject , props ) ;
1941
+ var helpers = getVueTemplateCompilerHelpers ( vm . _renderProxy ) ;
1942
+ var proxy = Object . assign ( { } , helpers ) ;
1943
+ if ( isDestructuringSlotScope ( slotScope ) ) {
1944
+ proxy = Object . assign ( { } , helpers , props ) ;
1945
+ } else {
1946
+ proxy [ slotScope ] = props ;
1947
+ }
1948
+ return scopedSlotFn . call ( proxy )
1949
+ } else {
1950
+ return renderSlot . call ( vm . _renderProxy , name , feedback , props , bindObject )
1951
+ }
1952
+ } ;
1953
+
1954
+ // $FlowIgnore
1955
+ addScopedSlots ( vm , options . scopedSlots ) ;
1956
+ } else {
1957
+ throwError ( 'the scopedSlots option is only supported in [email protected] +.' ) ;
1958
+ }
1959
+ }
1960
+
1895
1961
if ( options . slots ) {
1896
1962
addSlots ( vm , options . slots ) ;
1897
1963
}
@@ -4502,14 +4568,14 @@ function createLocalVue () {
4502
4568
4503
4569
//
4504
4570
4505
- function getStubs ( optionStubs , config ) {
4506
- if ( optionStubs ||
4507
- ( config . stubs && Object . keys ( config . stubs ) . length > 0 ) ) {
4508
- if ( Array . isArray ( optionStubs ) ) {
4509
- return optionStubs . concat ( Object . keys ( config . stubs || { } ) )
4571
+ function getOptions ( key , options , config ) {
4572
+ if ( options ||
4573
+ ( config [ key ] && Object . keys ( config [ key ] ) . length > 0 ) ) {
4574
+ if ( Array . isArray ( options ) ) {
4575
+ return options . concat ( Object . keys ( config [ key ] || { } ) )
4510
4576
} else {
4511
- return Object . assign ( { } , config . stubs ,
4512
- optionStubs )
4577
+ return Object . assign ( { } , config [ key ] ,
4578
+ options )
4513
4579
}
4514
4580
}
4515
4581
}
@@ -4519,7 +4585,9 @@ function mergeOptions (
4519
4585
config
4520
4586
) {
4521
4587
return Object . assign ( { } , options ,
4522
- { stubs : getStubs ( options . stubs , config ) } )
4588
+ { stubs : getOptions ( 'stubs' , options . stubs , config ) ,
4589
+ mocks : getOptions ( 'mocks' , options . mocks , config ) ,
4590
+ methods : getOptions ( 'methods' , options . methods , config ) } )
4523
4591
}
4524
4592
4525
4593
//
@@ -4668,7 +4736,9 @@ var config = {
4668
4736
stubs : {
4669
4737
transition : TransitionStub ,
4670
4738
'transition-group' : TransitionGroupStub
4671
- }
4739
+ } ,
4740
+ mocks : { } ,
4741
+ methods : { }
4672
4742
}
4673
4743
4674
4744
//
@@ -4680,6 +4750,7 @@ Vue.config.errorHandler = errorHandler;
4680
4750
function mount ( component , options ) {
4681
4751
if ( options === void 0 ) options = { } ;
4682
4752
4753
+ warnIfNoWindow ( ) ;
4683
4754
// Remove cached constructor
4684
4755
delete component . _Ctor ;
4685
4756
var vueClass = options . localVue || createLocalVue ( ) ;
0 commit comments