forked from facebookarchive/flux
-
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.
- Loading branch information
1 parent
abcf802
commit d1db952
Showing
3 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
examples/flux-chat/js/stores/__tests__/UnreadThreadStore-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,44 @@ | ||
/** | ||
* This file is provided by Facebook for testing and evaluation purposes | ||
* only. Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
jest.dontMock('../UnreadThreadStore'); | ||
jest.dontMock('react/lib/merge'); | ||
|
||
describe('UnreadThreadStore', function() { | ||
|
||
var ChatAppDispatcher; | ||
var UnreadThreadStore; | ||
var callback; | ||
|
||
beforeEach(function() { | ||
ChatAppDispatcher = require('../../dispatcher/ChatAppDispatcher'); | ||
UnreadThreadStore = require('../UnreadThreadStore'); | ||
callback = ChatAppDispatcher.register.mock.calls[0][0]; | ||
}); | ||
|
||
it('registers a callback with the dispatcher', function() { | ||
expect(ChatAppDispatcher.register.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('provides the unread thread count', function() { | ||
var ThreadStore = require('../ThreadStore'); | ||
ThreadStore.getAll.mockReturnValueOnce( | ||
{ | ||
foo: {lastMessage: {isRead: false}}, | ||
bar: {lastMessage: {isRead: false}}, | ||
baz: {lastMessage: {isRead: true}} | ||
} | ||
); | ||
expect(UnreadThreadStore.getCount()).toBe(2); | ||
}); | ||
|
||
}); |
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
99 changes: 99 additions & 0 deletions
99
examples/flux-todomvc/js/stores/__tests__/TodoStore-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,99 @@ | ||
/* | ||
* Copyright (c) 2014, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* TodoStore-test | ||
*/ | ||
|
||
jest.dontMock('../../constants/TodoConstants'); | ||
jest.dontMock('../TodoStore'); | ||
jest.dontMock('react/lib/merge'); | ||
|
||
describe('TodoStore', function() { | ||
|
||
var TodoConstants = require('../../constants/TodoConstants'); | ||
|
||
// mock actions inside dispatch payloads | ||
var actionTodoCreate = { | ||
source: 'VIEW_ACTION', | ||
action: { | ||
actionType: TodoConstants.TODO_CREATE, | ||
text: 'foo' | ||
} | ||
}; | ||
var actionTodoDestroy = { | ||
source: 'VIEW_ACTION', | ||
action: { | ||
actionType: TodoConstants.TODO_DESTROY, | ||
id: 'replace me in test' | ||
} | ||
}; | ||
|
||
beforeEach(function() { | ||
AppDispatcher = require('../../dispatcher/AppDispatcher'); | ||
TodoStore = require('../TodoStore'); | ||
callback = AppDispatcher.register.mock.calls[0][0]; | ||
}); | ||
|
||
it('should register a callback with the dispatcher', function() { | ||
expect(AppDispatcher.register.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('should initialize with no to-do items', function() { | ||
var all = TodoStore.getAll(); | ||
expect(all).toEqual({}); | ||
}); | ||
|
||
it('creates a to-do item', function() { | ||
callback(actionTodoCreate); | ||
var all = TodoStore.getAll(); | ||
var keys = Object.keys(all); | ||
expect(keys.length).toBe(1); | ||
expect(all[keys[0]].text).toEqual('foo'); | ||
}); | ||
|
||
it('destroys a to-do item', function() { | ||
callback(actionTodoCreate); | ||
var all = TodoStore.getAll(); | ||
var keys = Object.keys(all); | ||
expect(keys.length).toBe(1); | ||
actionTodoDestroy.action.id = keys[0]; | ||
callback(actionTodoDestroy); | ||
expect(all[keys[0]]).toBeUndefined(); | ||
}); | ||
|
||
it('can determine whether all to-do items are complete', function() { | ||
var i = 0; | ||
for (; i < 3; i++) { | ||
callback(actionTodoCreate); | ||
} | ||
expect(Object.keys(TodoStore.getAll()).length).toBe(3); | ||
expect(TodoStore.areAllComplete()).toBe(false); | ||
|
||
var all = TodoStore.getAll(); | ||
for (key in all) { | ||
callback({ | ||
source: 'VIEW_ACTION', | ||
action: { | ||
actionType: TodoConstants.TODO_COMPLETE, | ||
id: key | ||
} | ||
}); | ||
} | ||
expect(TodoStore.areAllComplete()).toBe(true); | ||
|
||
callback({ | ||
source: 'VIEW_ACTION', | ||
action: { | ||
actionType: TodoConstants.TODO_UNDO_COMPLETE, | ||
id: key | ||
} | ||
}); | ||
expect(TodoStore.areAllComplete()).toBe(false); | ||
}); | ||
|
||
}); |