1
+ /**
2
+ * Copyright © Magento, Inc. All rights reserved.
3
+ * See COPYING.txt for license details.
4
+ */
5
+
6
+ define ( [
7
+ 'underscore' ,
8
+ 'uiRegistry' ,
9
+ 'Magento_Catalog/js/components/product-ui-select' ,
10
+ 'ko' ,
11
+ 'jquery'
12
+ ] , function ( _ , registry , Constr , ko , $ ) {
13
+ 'use strict' ;
14
+
15
+ describe ( 'Magento_Catalog/js/components/product-ui-select' , function ( ) {
16
+ var obj ,
17
+ jQueryMethodsOverridden = { } ;
18
+
19
+ beforeEach ( function ( ) {
20
+ obj = new Constr ( {
21
+ name : 'productUiSelect' ,
22
+ dataScope : '' ,
23
+ provider : 'provider' ,
24
+ options : [ ] ,
25
+ value : ''
26
+ } ) ;
27
+
28
+ jQueryMethodsOverridden = { } ;
29
+ } ) ;
30
+
31
+ afterEach ( function ( ) {
32
+ _ . each ( jQueryMethodsOverridden , function ( value , key ) {
33
+ $ . fn [ key ] = value ;
34
+ } ) ;
35
+ } ) ;
36
+
37
+ describe ( '"validateInitialValue" method' , function ( ) {
38
+ it ( 'Should be defined' , function ( ) {
39
+ expect ( obj . hasOwnProperty ( 'validateInitialValue' ) ) . toBeDefined ( ) ;
40
+ } ) ;
41
+
42
+ it ( 'Should call not call ajax if value is empty' , function ( ) {
43
+ jQueryMethodsOverridden . ajax = $ . ajax ;
44
+ spyOn ( $ , 'ajax' ) ;
45
+
46
+ spyOn ( obj , 'validationLoading' ) ;
47
+ spyOn ( obj , 'value' ) . and . returnValue ( '' ) ;
48
+
49
+ expect ( obj . validateInitialValue ( ) ) . toBeUndefined ( ) ;
50
+
51
+ expect ( $ . ajax ) . not . toHaveBeenCalled ( ) ;
52
+ expect ( obj . validationLoading ) . toHaveBeenCalledWith ( false ) ;
53
+ } ) ;
54
+
55
+ it ( 'Should call ajax if value is not empty' , function ( ) {
56
+ var successCallback ,
57
+ completeCallback ,
58
+ responseData = {
59
+ label : 'hello world' ,
60
+ value : 'hello world'
61
+ } ;
62
+
63
+ jQueryMethodsOverridden . ajax = $ . ajax ;
64
+ spyOn ( $ , 'ajax' ) . and . callFake ( function ( request ) {
65
+ successCallback = request . success . bind ( obj ) ;
66
+ completeCallback = request . complete . bind ( obj ) ;
67
+ } ) ;
68
+
69
+ spyOn ( obj , 'validationLoading' ) ;
70
+ spyOn ( obj , 'value' ) . and . returnValue ( 'hello world' ) ;
71
+ spyOn ( obj , 'options' ) ;
72
+ spyOn ( obj , 'setCaption' ) ;
73
+
74
+ expect ( obj . validateInitialValue ( ) ) . toBeUndefined ( ) ;
75
+
76
+ successCallback ( responseData ) ;
77
+ completeCallback ( ) ;
78
+
79
+ expect ( $ . ajax ) . toHaveBeenCalled ( ) ;
80
+
81
+ expect ( obj . validationLoading ) . toHaveBeenCalledWith ( false ) ;
82
+ expect ( obj . loadedOption ) . toBe ( responseData ) ;
83
+ expect ( obj . options ) . toHaveBeenCalledWith ( [ responseData ] ) ;
84
+
85
+ expect ( obj . setCaption ) . toHaveBeenCalled ( ) ;
86
+ } ) ;
87
+ } ) ;
88
+ } ) ;
89
+ } ) ;
0 commit comments