Skip to content

Commit 7885d27

Browse files
committed
Test case for calling getInitialAsyncState
1 parent 13fe784 commit 7885d27

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

tests/browser.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,16 @@ describe('Routing with async components', function() {
290290
var aboutWasInLoadingState;
291291
var mainSeenPendingUpdate;
292292

293+
var mainAsyncStateCount;
294+
var aboutAsyncStateCount;
295+
293296
var App = React.createClass({
294297

295298
render: function() {
296299
return Router.Locations({ref: 'router', className: 'App'},
297300
Router.Location({path: '/__zuul', handler: Main, ref: 'main'}),
298-
Router.Location({path: '/__zuul/about', handler: About, ref: 'about'})
301+
Router.Location({path: '/__zuul/about', handler: About, ref: 'about'}),
302+
Router.Location({path: '/__zuul/about2', handler: About, ref: 'about2'})
299303
);
300304
}
301305
});
@@ -304,6 +308,7 @@ describe('Routing with async components', function() {
304308
mixins: [ReactAsync.Mixin, Router.NavigatableMixin],
305309

306310
getInitialStateAsync: function(cb) {
311+
mainAsyncStateCount += 1;
307312
setTimeout(function() {
308313
cb(null, {message: 'main'});
309314
}, 20);
@@ -325,6 +330,7 @@ describe('Routing with async components', function() {
325330
mixins: [ReactAsync.Mixin],
326331

327332
getInitialStateAsync: function(cb) {
333+
aboutAsyncStateCount += 1;
328334
delay(function() {
329335
cb(null, {message: 'about'});
330336
});
@@ -343,25 +349,30 @@ describe('Routing with async components', function() {
343349
aboutWasInLoadingState = false;
344350

345351
mainSeenPendingUpdate = false;
352+
353+
mainAsyncStateCount = 0;
354+
aboutAsyncStateCount = 0;
346355
});
347356

348357
beforeEach(setUp(App));
349358
afterEach(cleanUp);
350359

351360
it('renders async component', function(done) {
352361
assertRendered('loading...');
353-
setTimeout(function() {
362+
delay(50, function() {
354363
assertRendered('main');
355364
assert(!mainSeenPendingUpdate);
356365
assert(mainWasInLoadingState);
366+
assert.equal(mainAsyncStateCount, 1);
367+
assert.equal(aboutAsyncStateCount, 0);
357368
done();
358-
}, 50);
369+
});
359370
});
360371

361372
it('renders async component and navigates', function(done) {
362373
assertRendered('loading...');
363374

364-
setTimeout(function() {
375+
delay(100, function() {
365376
assertRendered('main');
366377

367378
router.navigate('/__zuul/about', function(err) {
@@ -370,15 +381,17 @@ describe('Routing with async components', function() {
370381
assert(mainWasInLoadingState);
371382
assert(mainSeenPendingUpdate);
372383
assert(!aboutWasInLoadingState);
384+
assert.equal(mainAsyncStateCount, 1);
385+
assert.equal(aboutAsyncStateCount, 1);
373386
done();
374387
});
375-
}, 100);
388+
});
376389
});
377390

378391
it('cancels pending update on navigate', function(done) {
379392
assertRendered('loading...');
380393

381-
setTimeout(function() {
394+
delay(100, function() {
382395
assertRendered('main');
383396

384397
router.navigate('/__zuul/about', function(err) {
@@ -393,10 +406,37 @@ describe('Routing with async components', function() {
393406
assert(mainSeenPendingUpdate);
394407
assert(mainWasInLoadingState);
395408
assert(!aboutWasInLoadingState);
409+
assert.equal(mainAsyncStateCount, 1);
410+
assert.equal(aboutAsyncStateCount, 1);
396411
done();
397412
});
398413

399-
}, 100);
414+
});
415+
});
416+
417+
it('does not trigger async state load if type of the next component is the same', function(done) {
418+
assertRendered('loading...');
419+
420+
delay(100, function() {
421+
assertRendered('main');
422+
423+
router.navigate('/__zuul/about', function(err) {
424+
if (err) return done(err);
425+
assertRendered('about');
426+
427+
router.navigate('/__zuul/about2', function(err) {
428+
if (err) return done(err);
429+
assertRendered('about');
430+
assert(mainWasInLoadingState);
431+
assert(mainSeenPendingUpdate);
432+
assert(!aboutWasInLoadingState);
433+
assert.equal(mainAsyncStateCount, 1);
434+
assert.equal(aboutAsyncStateCount, 1);
435+
done();
436+
});
437+
});
438+
});
439+
400440
});
401441
});
402442

0 commit comments

Comments
 (0)