@@ -941,121 +941,126 @@ if ($.support.pjax) {
941
941
window . iframeLoad ( )
942
942
} )
943
943
944
- asyncTest ( "navigation through history obeys maxCacheLength" , function ( ) {
944
+ asyncTest ( "hitting the back button obeys maxCacheLength" , function ( ) {
945
945
var frame = this . frame
946
946
var count = 0
947
+ var didHitServer
947
948
948
949
// Reduce the maxCacheLength for this spec to make it easier to test.
949
- frame . $ . pjax . defaults . maxCacheLength = 2
950
-
951
- // We can determine whether a navigation event was fulfilled by the cache
952
- // or the server by checking whether pjax:beforeSend fired before pjax:end.
953
- var navigationWasCached
950
+ frame . $ . pjax . defaults . maxCacheLength = 1
954
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.
955
954
frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
956
- navigationWasCached = false
955
+ didHitServer = true
957
956
} )
958
957
959
958
frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
960
959
count ++
961
960
962
- // First, navigate three times .
961
+ // First, navigate twice .
963
962
if ( count == 1 ) {
964
- equal ( frame . location . pathname , "/hello.html" )
965
- navigationWasCached = true
966
963
frame . $ . pjax ( { url : "env.html" , container : "#main" } )
967
964
} else if ( count == 2 ) {
968
- equal ( frame . location . pathname , "/env.html" )
969
- equal ( navigationWasCached , false )
970
- navigationWasCached = true
971
965
frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
972
966
} else if ( count == 3 ) {
973
- equal ( frame . location . pathname , "/hello.html" )
974
- equal ( navigationWasCached , false )
975
- navigationWasCached = true
976
- frame . $ . pjax ( { url : "env.html" , container : "#main" } )
967
+ // There should now be one item in the back cache.
968
+ didHitServer = false
969
+ frame . history . back ( )
977
970
} else if ( count == 4 ) {
978
- equal ( frame . location . pathname , "/env.html" )
979
- equal ( navigationWasCached , false )
980
- // The cache should now have two items in it, so hitting the back
981
- // button three times should pull the first two pages from cache and
982
- // the third from the server.
983
- navigationWasCached = true
971
+ equal ( frame . location . pathname , "/env.html" , "Went backward" )
972
+ equal ( didHitServer , false , "Hit cache" )
984
973
frame . history . back ( )
985
974
} else if ( count == 5 ) {
986
- equal ( frame . location . pathname , "/hello.html" )
987
- equal ( navigationWasCached , true )
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 ) {
988
1006
frame . history . back ( )
989
- } else if ( count == 6 ) {
990
- equal ( frame . location . pathname , "/env.html" )
991
- equal ( navigationWasCached , true )
1007
+ } else if ( count == 4 ) {
992
1008
frame . history . back ( )
993
- } else if ( count == 7 ) {
994
- equal ( frame . location . pathname , "/hello.html" )
995
- equal ( navigationWasCached , false )
996
- // The cache should still have two items in it, both in the forward
997
- // cache. If we hit forward three times, the first two should come from
998
- // cache and the third from the server.
999
- navigationWasCached = true
1000
- frame . history . forward ( )
1001
- } else if ( count == 8 ) {
1002
- equal ( frame . location . pathname , "/env.html" )
1003
- equal ( navigationWasCached , true )
1009
+ } else if ( count == 5 ) {
1010
+ // There should now be one item in the forward cache.
1011
+ didHitServer = false
1004
1012
frame . history . forward ( )
1005
- } else if ( count == 9 ) {
1006
- equal ( frame . location . pathname , "/hello .html" )
1007
- equal ( navigationWasCached , true )
1013
+ } else if ( count == 6 ) {
1014
+ equal ( frame . location . pathname , "/env .html" , "Went forward ")
1015
+ equal ( didHitServer , false , "Hit cache" )
1008
1016
frame . history . forward ( )
1009
- } else if ( count == 10 ) {
1010
- equal ( frame . location . pathname , "/env .html" )
1011
- equal ( navigationWasCached , false )
1017
+ } else if ( count == 7 ) {
1018
+ equal ( frame . location . pathname , "/hello .html" , "Went forward ")
1019
+ equal ( didHitServer , true , "Hit server" )
1012
1020
start ( )
1013
1021
}
1014
1022
} )
1015
1023
1016
- equal ( frame . location . pathname , "/home.html" )
1017
1024
frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
1018
1025
} )
1019
1026
1020
1027
asyncTest ( "setting maxCacheLength to 0 disables caching" , function ( ) {
1021
1028
var frame = this . frame
1022
1029
var count = 0
1030
+ var didHitServer
1023
1031
1032
+ // Set maxCacheLength to 0 to disable caching completely.
1024
1033
frame . $ . pjax . defaults . maxCacheLength = 0
1025
1034
1026
- // We can determine whether a navigation event was fulfilled by the cache
1027
- // or the server by checking whether pjax:beforeSend fired before pjax:end.
1028
- var navigationWasCached
1029
-
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.
1030
1037
frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
1031
- navigationWasCached = false
1038
+ didHitServer = true
1032
1039
} )
1033
1040
1034
1041
frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
1035
1042
count ++
1036
1043
1037
1044
if ( count == 1 ) {
1038
- equal ( frame . location . pathname , "/hello.html" )
1039
- navigationWasCached = true
1045
+ didHitServer = false
1040
1046
frame . $ . pjax ( { url : "env.html" , container : "#main" } )
1041
1047
} else if ( count == 2 ) {
1042
- equal ( frame . location . pathname , "/env.html" )
1043
- equal ( navigationWasCached , false )
1044
- navigationWasCached = true
1048
+ equal ( frame . location . pathname , "/env.html" , "Navigated to a new page" )
1049
+ equal ( didHitServer , true , "Hit server" )
1050
+ didHitServer = false
1045
1051
frame . history . back ( )
1046
1052
} else if ( count == 3 ) {
1047
- equal ( frame . location . pathname , "/hello.html" )
1048
- equal ( navigationWasCached , false )
1049
- navigationWasCached = true
1053
+ equal ( frame . location . pathname , "/hello.html" , "Went backward" )
1054
+ equal ( didHitServer , true , "Hit server" )
1055
+ didHitServer = false
1050
1056
frame . history . forward ( )
1051
1057
} else if ( count == 4 ) {
1052
- equal ( frame . location . pathname , "/env.html" )
1053
- equal ( navigationWasCached , false )
1058
+ equal ( frame . location . pathname , "/env.html" , "Went forward" )
1059
+ equal ( didHitServer , true , "Hit server" )
1054
1060
start ( )
1055
1061
}
1056
1062
} )
1057
1063
1058
- equal ( frame . location . pathname , "/home.html" )
1059
1064
frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
1060
1065
} )
1061
1066
0 commit comments