forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request facebook#1556 from joshma/flux-dispatcher-docs
Update Flux Dispatcher.dispatch and waitFor examples
- Loading branch information
Showing
4 changed files
with
127 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
examples/todomvc-flux/js/dispatcher/__tests__/AppDispatcher-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"use strict"; | ||
|
||
jest.autoMockOff(); | ||
|
||
describe('AppDispatcher', function() { | ||
var AppDispatcher; | ||
|
||
beforeEach(function() { | ||
AppDispatcher = require('../AppDispatcher.js'); | ||
}); | ||
|
||
it('sends actions to subscribers', function() { | ||
var listener = jest.genMockFunction(); | ||
AppDispatcher.register(listener); | ||
|
||
var payload = {}; | ||
AppDispatcher.dispatch(payload); | ||
expect(listener.mock.calls.length).toBe(1); | ||
expect(listener.mock.calls[0][0]).toBe(payload); | ||
}); | ||
|
||
it.only('waits with chained dependencies properly', function() { | ||
var payload = {}; | ||
|
||
var listener1Done = false; | ||
var listener1 = function(pl) { | ||
return AppDispatcher.waitFor([index2, index4], function() { | ||
// Second, third, and fourth listeners should have now been called | ||
expect(listener2Done).toBe(true); | ||
expect(listener3Done).toBe(true); | ||
expect(listener4Done).toBe(true); | ||
listener1Done = true; | ||
}); | ||
}; | ||
var index1 = AppDispatcher.register(listener1); | ||
|
||
var listener2Done = false; | ||
var listener2 = function(pl) { | ||
return AppDispatcher.waitFor([index3], function() { | ||
expect(listener3Done).toBe(true); | ||
listener2Done = true; | ||
}); | ||
}; | ||
var index2 = AppDispatcher.register(listener2); | ||
|
||
var listener3Done = false; | ||
var listener3 = function(pl) { | ||
listener3Done = true; | ||
return true; | ||
}; | ||
var index3 = AppDispatcher.register(listener3); | ||
|
||
var listener4Done = false; | ||
var listener4 = function(pl) { | ||
return AppDispatcher.waitFor([index3], function() { | ||
expect(listener3Done).toBe(true); | ||
listener4Done = true; | ||
}); | ||
}; | ||
var index4 = AppDispatcher.register(listener4); | ||
|
||
runs(function() { | ||
AppDispatcher.dispatch(payload); | ||
}); | ||
|
||
waitsFor(function() { | ||
return listener1Done; | ||
}, "Not all subscribers were properly called", 500); | ||
|
||
runs(function() { | ||
expect(listener1Done).toBe(true); | ||
expect(listener2Done).toBe(true); | ||
expect(listener3Done).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters