@@ -8,13 +8,14 @@ define([
8
8
'underscore' ,
9
9
'uiRegistry' ,
10
10
'Magento_Ui/js/form/element/ui-select' ,
11
- 'ko'
12
- ] , function ( _ , registry , Constr , ko ) {
11
+ 'ko' ,
12
+ 'jquery'
13
+ ] , function ( _ , registry , Constr , ko , $ ) {
13
14
'use strict' ;
14
15
15
16
describe ( 'Magento_Ui/js/form/element/ui-select' , function ( ) {
16
-
17
- var obj ;
17
+ var obj ,
18
+ jQueryMethodsOverridden = { } ;
18
19
19
20
beforeEach ( function ( ) {
20
21
obj = new Constr ( {
@@ -25,6 +26,14 @@ define([
25
26
26
27
obj . value = ko . observableArray ( [ ] ) ;
27
28
obj . cacheOptions . plain = [ ] ;
29
+
30
+ jQueryMethodsOverridden = { } ;
31
+ } ) ;
32
+
33
+ afterEach ( function ( ) {
34
+ _ . each ( jQueryMethodsOverridden , function ( value , key ) {
35
+ $ . fn [ key ] = value ;
36
+ } ) ;
28
37
} ) ;
29
38
30
39
describe ( '"initialize" method' , function ( ) {
@@ -575,26 +584,30 @@ define([
575
584
} ) ;
576
585
} ) ;
577
586
describe ( '"isSearchKeyCached" method' , function ( ) {
578
- it ( 'Should return true if searchKey has already been cached and currentPage <= lastPage' , function ( ) {
587
+ it ( 'Should return false if searchKey has already been cached and total covers > 1 page' , function ( ) {
588
+ obj . deviation = 30 ;
579
589
obj . cachedSearchResults = {
580
590
cake : {
581
- options : [ ] ,
582
- lastPage : 1
591
+ options : [ /** 50 options **/ ] ,
592
+ lastPage : 1 ,
593
+ total : 50
583
594
}
584
595
} ;
585
596
586
- expect ( obj . isSearchKeyCached ( 'cake' , 1 ) ) . toBe ( true ) ;
597
+ expect ( obj . isSearchKeyCached ( 'cake' ) ) . toBe ( false ) ;
587
598
} ) ;
588
599
589
- it ( 'Should return false if searchKey has already been cached and currentPage > lastPage' , function ( ) {
600
+ it ( 'Should return true if searchKey has already been cached and total only covers 1 page' , function ( ) {
601
+ obj . deviation = 30 ;
590
602
obj . cachedSearchResults = {
591
603
cake : {
592
- options : [ ] ,
593
- lastPage : 1
604
+ options : [ /** 29 options **/ ] ,
605
+ lastPage : 1 ,
606
+ total : 29
594
607
}
595
608
} ;
596
609
597
- expect ( obj . isSearchKeyCached ( 'cake' , 2 ) ) . toBe ( false ) ;
610
+ expect ( obj . isSearchKeyCached ( 'cake' ) ) . toBe ( true ) ;
598
611
} ) ;
599
612
600
613
it ( 'Should return false if searchKey is not cached' , function ( ) {
@@ -607,12 +620,41 @@ define([
607
620
value : 'delicious'
608
621
} ] ;
609
622
610
- obj . setCachedSearchResults ( 'cake' , options , 1 ) ;
623
+ obj . setCachedSearchResults ( 'cake' , options , 1 , 1 ) ;
611
624
612
625
expect ( obj . getCachedSearchResults ( 'cake' ) ) . toEqual ( {
613
626
options : options ,
614
- lastPage : 1
627
+ lastPage : 1 ,
628
+ total : 1
629
+ } ) ;
630
+ } ) ;
631
+ } ) ;
632
+ describe ( '"processRequest" method' , function ( ) {
633
+ it ( 'Should store options successfully fetched from ajax request' , function ( ) {
634
+ var ajaxRequest ,
635
+ successfulAjaxResponse = {
636
+ options : {
637
+ '2053' : {
638
+ value : '2053' ,
639
+ label : 'testProductName5a8ddfd933b5c' ,
640
+ 'is_active' : 1 ,
641
+ path : 'testSku5a8ddfd933b5c' ,
642
+ optgroup : false
643
+ }
644
+ }
645
+ } ;
646
+
647
+ jQueryMethodsOverridden . ajax = $ . ajax ;
648
+
649
+ spyOn ( $ , 'ajax' ) . and . callFake ( function ( request ) {
650
+ ajaxRequest = request . success . bind ( obj ) ;
615
651
} ) ;
652
+
653
+ expect ( obj . processRequest ( ) ) . toBeUndefined ( ) ;
654
+
655
+ ajaxRequest ( successfulAjaxResponse ) ;
656
+
657
+ expect ( obj . options ( ) ) . toEqual ( [ successfulAjaxResponse . options [ '2053' ] ] ) ;
616
658
} ) ;
617
659
} ) ;
618
660
} ) ;
0 commit comments