@@ -891,7 +891,7 @@ if ($.support.pjax) {
891
891
// Test is fragile
892
892
asyncTest ( "no initial pjax:popstate event" , function ( ) {
893
893
var frame = this . frame
894
- var count = 0 ;
894
+ var count = 0
895
895
896
896
window . iframeLoad = function ( ) {
897
897
count ++
@@ -941,6 +941,129 @@ if ($.support.pjax) {
941
941
window . iframeLoad ( )
942
942
} )
943
943
944
+ asyncTest ( "hitting the back button obeys maxCacheLength" , function ( ) {
945
+ var frame = this . frame
946
+ var count = 0
947
+ var didHitServer
948
+
949
+ // Reduce the maxCacheLength for this spec to make it easier to test.
950
+ frame . $ . pjax . defaults . maxCacheLength = 1
951
+
952
+ // This event will fire only when we request a page from the server, so we
953
+ // can use it to detect a cache miss.
954
+ frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
955
+ didHitServer = true
956
+ } )
957
+
958
+ frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
959
+ count ++
960
+
961
+ // First, navigate twice.
962
+ if ( count == 1 ) {
963
+ frame . $ . pjax ( { url : "env.html" , container : "#main" } )
964
+ } else if ( count == 2 ) {
965
+ frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
966
+ } else if ( count == 3 ) {
967
+ // There should now be one item in the back cache.
968
+ didHitServer = false
969
+ frame . history . back ( )
970
+ } else if ( count == 4 ) {
971
+ equal ( frame . location . pathname , "/env.html" , "Went backward" )
972
+ equal ( didHitServer , false , "Hit cache" )
973
+ frame . history . back ( )
974
+ } else if ( count == 5 ) {
975
+ equal ( frame . location . pathname , "/hello.html" , "Went backward" )
976
+ equal ( didHitServer , true , "Hit server" )
977
+ start ( )
978
+ }
979
+ } )
980
+
981
+ frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
982
+ } )
983
+
984
+ asyncTest ( "hitting the forward button obeys maxCacheLength" , function ( ) {
985
+ var frame = this . frame
986
+ var count = 0
987
+ var didHitServer
988
+
989
+ // Reduce the maxCacheLength for this spec to make it easier to test.
990
+ frame . $ . pjax . defaults . maxCacheLength = 1
991
+
992
+ // This event will fire only when we request a page from the server, so we
993
+ // can use it to detect a cache miss.
994
+ frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
995
+ didHitServer = true
996
+ } )
997
+
998
+ frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
999
+ count ++
1000
+
1001
+ if ( count == 1 ) {
1002
+ frame . $ . pjax ( { url : "env.html" , container : "#main" } )
1003
+ } else if ( count == 2 ) {
1004
+ frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
1005
+ } else if ( count == 3 ) {
1006
+ frame . history . back ( )
1007
+ } else if ( count == 4 ) {
1008
+ frame . history . back ( )
1009
+ } else if ( count == 5 ) {
1010
+ // There should now be one item in the forward cache.
1011
+ didHitServer = false
1012
+ frame . history . forward ( )
1013
+ } else if ( count == 6 ) {
1014
+ equal ( frame . location . pathname , "/env.html" , "Went forward" )
1015
+ equal ( didHitServer , false , "Hit cache" )
1016
+ frame . history . forward ( )
1017
+ } else if ( count == 7 ) {
1018
+ equal ( frame . location . pathname , "/hello.html" , "Went forward" )
1019
+ equal ( didHitServer , true , "Hit server" )
1020
+ start ( )
1021
+ }
1022
+ } )
1023
+
1024
+ frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
1025
+ } )
1026
+
1027
+ asyncTest ( "setting maxCacheLength to 0 disables caching" , function ( ) {
1028
+ var frame = this . frame
1029
+ var count = 0
1030
+ var didHitServer
1031
+
1032
+ // Set maxCacheLength to 0 to disable caching completely.
1033
+ frame . $ . pjax . defaults . maxCacheLength = 0
1034
+
1035
+ // This event will fire only when we request a page from the server, so we
1036
+ // can use it to detect a cache miss.
1037
+ frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
1038
+ didHitServer = true
1039
+ } )
1040
+
1041
+ frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
1042
+ count ++
1043
+
1044
+ if ( count == 1 ) {
1045
+ didHitServer = false
1046
+ frame . $ . pjax ( { url : "env.html" , container : "#main" } )
1047
+ } else if ( count == 2 ) {
1048
+ equal ( frame . location . pathname , "/env.html" , "Navigated to a new page" )
1049
+ equal ( didHitServer , true , "Hit server" )
1050
+ didHitServer = false
1051
+ frame . history . back ( )
1052
+ } else if ( count == 3 ) {
1053
+ equal ( frame . location . pathname , "/hello.html" , "Went backward" )
1054
+ equal ( didHitServer , true , "Hit server" )
1055
+ didHitServer = false
1056
+ frame . history . forward ( )
1057
+ } else if ( count == 4 ) {
1058
+ equal ( frame . location . pathname , "/env.html" , "Went forward" )
1059
+ equal ( didHitServer , true , "Hit server" )
1060
+ start ( )
1061
+ }
1062
+ } )
1063
+
1064
+ frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
1065
+ } )
1066
+
944
1067
asyncTest ( "popstate preserves GET data" , function ( ) {
945
1068
var frame = this . frame
946
1069
0 commit comments