@@ -941,121 +941,126 @@ if ($.support.pjax) {
941941 window . iframeLoad ( )
942942 } )
943943
944- asyncTest ( "navigation through history obeys maxCacheLength" , function ( ) {
944+ asyncTest ( "hitting the back button obeys maxCacheLength" , function ( ) {
945945 var frame = this . frame
946946 var count = 0
947+ var didHitServer
947948
948949 // 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
954951
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.
955954 frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
956- navigationWasCached = false
955+ didHitServer = true
957956 } )
958957
959958 frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
960959 count ++
961960
962- // First, navigate three times .
961+ // First, navigate twice .
963962 if ( count == 1 ) {
964- equal ( frame . location . pathname , "/hello.html" )
965- navigationWasCached = true
966963 frame . $ . pjax ( { url : "env.html" , container : "#main" } )
967964 } else if ( count == 2 ) {
968- equal ( frame . location . pathname , "/env.html" )
969- equal ( navigationWasCached , false )
970- navigationWasCached = true
971965 frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
972966 } 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 ( )
977970 } 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" )
984973 frame . history . back ( )
985974 } 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 ) {
9881006 frame . history . back ( )
989- } else if ( count == 6 ) {
990- equal ( frame . location . pathname , "/env.html" )
991- equal ( navigationWasCached , true )
1007+ } else if ( count == 4 ) {
9921008 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
10041012 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" )
10081016 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" )
10121020 start ( )
10131021 }
10141022 } )
10151023
1016- equal ( frame . location . pathname , "/home.html" )
10171024 frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
10181025 } )
10191026
10201027 asyncTest ( "setting maxCacheLength to 0 disables caching" , function ( ) {
10211028 var frame = this . frame
10221029 var count = 0
1030+ var didHitServer
10231031
1032+ // Set maxCacheLength to 0 to disable caching completely.
10241033 frame . $ . pjax . defaults . maxCacheLength = 0
10251034
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.
10301037 frame . $ ( "#main" ) . on ( "pjax:beforeSend" , function ( ) {
1031- navigationWasCached = false
1038+ didHitServer = true
10321039 } )
10331040
10341041 frame . $ ( "#main" ) . on ( "pjax:end" , function ( ) {
10351042 count ++
10361043
10371044 if ( count == 1 ) {
1038- equal ( frame . location . pathname , "/hello.html" )
1039- navigationWasCached = true
1045+ didHitServer = false
10401046 frame . $ . pjax ( { url : "env.html" , container : "#main" } )
10411047 } 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
10451051 frame . history . back ( )
10461052 } 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
10501056 frame . history . forward ( )
10511057 } 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" )
10541060 start ( )
10551061 }
10561062 } )
10571063
1058- equal ( frame . location . pathname , "/home.html" )
10591064 frame . $ . pjax ( { url : "hello.html" , container : "#main" } )
10601065 } )
10611066
0 commit comments