Wow! Problem solved! Thank you very much! Didn't realize the event
should be bound asynchronously. : -/

On Oct 24, 6:02 pm, Ricardo Tomasi <ricardob...@gmail.com> wrote:
> Keep the 100ms timer in mind. When you change location.hash at the
> beginning of a test, you need to allow for the time it takes for it to
> catch up with the change. Notice how in your tests the first one fails
> if you access it with a hash 
> likehttp://dl.getdropbox.com/u/1402827/hashchange/index.html#ohno
> ? That's because it's not seeing you change it to empty. So
>
> location.hash = '';
> $(window).bind('hashchange', fn...);
> location.hash = '#test';
>
> gives the same results as
>
> location.hash = '';
> location.hash = '#test';
> $(window).bind(...);
>
> The first test only passes because luckily you already have an empty
> location.hash. Think of changing location.hash as an asynchronous
> operation which takes at least 100ms. Wrapping the second change in a
> timeout does the trick:
>
> location.hash = '';
> setTimeout(function(){
>    $(window).bind(..);
>    location.hash = '#new';
>
> }, 200); // just to be safe
>
> All tests passed:http://ff6600.org/j/hashchange/
>
> -- ricardo
>
> On Oct 23, 10:29 pm, gMinuses <gminu...@gmail.com> wrote:
>
> > Thank you! This really does the trick, but now the real problem is to
> > using a setInterval to check location.hash to implement an
> > "onhashchange" event. These events are all asynchronous, and I can't
> > get my tests passed, here is the test case:
>
> >http://dl.getdropbox.com/u/1402827/hashchange/index.html
>
> > I didn't make any ajax call.
>
> > On Oct 24, 5:04 am, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > > Just use sync: true in all tests and put an end to this discussion :)
>
> > > On Oct 22, 9:26 pm, gMinuses <gminu...@gmail.com> wrote:
>
> > > > I'd like to test the following code to make sure:
>
> > > > 1. setTimeout calls the function
> > > > 2. ajax success callback is called
>
> > > > setTimeout(function() {
>
> > > >         $.ajax({
> > > >                 url: './index.html',
> > > >                 success: function() {}
> > > >         })
>
> > > > }, 100);
>
> > > > If I only have one nested asynchronous test like this, it works:
>
> > > > asyncTest('asyncTest', function() {
> > > >         setTimeout(function() {
> > > >                 console.log(1);
> > > >                 ok(true, 'success');
> > > >                 start();
>
> > > >                 asyncTest('nested asyncTest', function() {
> > > >                         $.ajax({
> > > >                                 url: './index.html',
> > > >                                 success: function() {
> > > >                                         console.log(2);
> > > >                                         ok(true, 'ajax success');
> > > >                                         start();
> > > >                                 }
> > > >                         })
> > > >                 })
>
> > > >         }, 100)
>
> > > > })
>
> > > > But if there are multiple tests, it works, but run out of order:
>
> > > > asyncTest('asyncTest', function() {
> > > >         setTimeout(function() {
> > > >                 console.log(1);
> > > >                 ok(true, 'success');
> > > >                 start();
>
> > > >                 asyncTest('nested asyncTest', function() {
> > > >                         $.ajax({
> > > >                                 url: './index.html',
> > > >                                 success: function() {
> > > >                                         console.log(2);
> > > >                                         ok(true, 'ajax success');
> > > >                                         start();
> > > >                                 }
> > > >                         })
> > > >                 })
>
> > > >         }, 100)
>
> > > > })
>
> > > > asyncTest('another asyncTest', function() {
> > > >         setTimeout(function() {
> > > >                 console.log(3);
> > > >                 ok(true, 'another success');
> > > >                 start();
>
> > > >                 asyncTest('another nested asyncTest', function() {
> > > >                         $.ajax({
> > > >                                 url: './index.html',
> > > >                                 success: function() {
> > > >                                         console.log(4);
> > > >                                         ok(true, 'another ajax 
> > > > success');
> > > >                                         start();
> > > >                                 }
> > > >                         })
> > > >                 })
>
> > > >         }, 100)
>
> > > > })
>
> > > > If you open firebug, you can see the order is "1,3,2,4", which can
> > > > cause disasters.
>
> > > > The real world problem is that I use setInterval to periodically check
> > > > location.hash to implement a cross-browser "onhashchange" event, and I
> > > > bind a function to this event, when the function gets called, it will
> > > > make an ajax call. I set a different value to location.hash in every
> > > > test to trigger the event, which is actually asynchronous. So how do I
> > > > use qunit to test this code?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to